Pages: [1]   Go Down
Author Topic: lags in serial communication  (Read 321 times)
0 Members and 1 Guest are viewing this topic.
Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now why do you think that might be?

Hint:
Code:
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.
« Last Edit: January 09, 2013, 10:38:34 am by AWOL » Logged

"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.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: