Why is Serial.println(count) necessary?


i have following program takes input (12 or 13), toggles led on appropriate port , returns returnmessage.

however, if remove serial.println(count) after if-part, doesn't output or anything, if it's there, works perfect, has explanation?

static boolean state[14];
static int pos;
static int fade;
int count;
int led;

void setup() {        

 pinmode(12, output);
 pinmode(13, output);  
 serial.begin(9600);  
 pos = 0;
 led = 12;
 count = 0;
 state[12] = false;
 state[13] = false;

}

void loop() {
count = 0;
pos = 0;
 while (serial.available() > 0) {

   char ch = serial.read();

   if (ch>'0' && ch<=  '9'){
     pos = pos * 10 + ch - '0';
     count++;
   }

   serial.println(count);
 }
 if (count==2){
   state[pos] = !state[pos];
   digitalwrite(pos, state[pos]);
   serial.write("pin #: ");
   serial.print(pos);
   if (state[pos]) serial.write(" on");
   if (!state[pos]) serial.write(" off");
   serial.write(".");
 }
 //count = 0;
 //pos = 0;
}


the transmission of serial relatively slow. code reading serial data assumes ready read.

when isn't, serial.println() statement adds delay while loop, while data sent out. small delay enough allow rest of data arrive.

if data being sent included end of packet marker, , code on arduino read until end of packet marker arrived, problem seeing avoided.

a small delay() mask problem, giving time data arrive. how long? depends on baud rate, , sending data. typically, 10 milliseconds per character.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Why is Serial.println(count) necessary?


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums