Go Down

Topic: lags in serial communication (Read 408 times) previous topic - next topic

gianlucalongoni

Hi everyone!

the following sketch should work, accepting three values entered by the utilizer, blinking three different leds, with delay times equal to the values entered with serial communication.
It works! But the problem is that, every time i enter three new values and press start, several seconds pass before the command is received by arduino, and the leds start blinking withe the new delay times. What is the cause? Serial communication baud rate?

Here is the sketch:
Code: [Select]
const int NUMBER_OF_FIELDS=3;
int fieldIndex=0;
int values[NUMBER_OF_FIELDS];

int ledDelays[]={0,0,0};

void setup(){
 Serial.begin(9600);
 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
}

void loop(){

blinkIt(2,ledDelays[0]);
blinkIt(3,ledDelays[1]);
blinkIt(4,ledDelays[2]);

 
if(Serial.available()){
  char ch=Serial.read();
  if(isDigit(ch)){
    if (fieldIndex<NUMBER_OF_FIELDS){
      values[fieldIndex]=(values[fieldIndex]*10)+(ch-'0');
    }
  }
  else if(ch==','){
    fieldIndex++;
  }
  else if(ch==10){
    for(int i=0; i<min(NUMBER_OF_FIELDS, fieldIndex+1); i++){
      Serial.println(values[i]);
      ledDelays[i]=values[i];
      values[i]=0;
    }
    fieldIndex=0;
  }
}

}

void blinkIt (int led, int delayTime){
 digitalWrite(led,HIGH);
 delay(delayTime);
 digitalWrite(led,LOW);
 delay(delayTime);
}



AWOL

#1
Jan 09, 2013, 04:36 pm Last Edit: Jan 09, 2013, 04:38 pm by AWOL Reason: 1
Now why do you think that might be?

Hint:
Code: [Select]
delay(delayTime);

Quote
But the problem is that, every time i enter three new values and press start, several seconds pass before the command is received by arduino

In fact, very little time passes before the command is received by the Arduino, just over a millisecond per character.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Arrch

"Several seconds" doesn't sound like a baud rate issue unless you are transferring tons of data. I would venture to guess that the the delay() calls in all of your blink functions are what cause the latency. You're telling the processor to sit and twiddle it's thumbs despite the fact that serial data is available. You should rewrite those utilizing the technique demonstrated in the Blink Without Delay example.

gianlucalongoni

yes!
it must be the delay function lagging all the program!
Thank u!
You've been very helpful!

Go Up