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