Hello,
I am trying to learn about serial control. I modified the "Knight Rider" example from arduino.cc/en/Tutorial/KnightRider. The sketch works by typing the ASCII value in the serial monitor, which sets the value of "timer". My question is how can I stop the LED's from rapidly flashing when the sketch is first run and before any value is typed in the serial monitor? I also notice if I uncomment the code that will display the typed value, the sketch runs slow enough to notice a delay in the led flash pattern. Any help would be appreciated. the code is.
/* Knight Rider 3
* --------------
*
* This example concentrates on making the visuals fluid.
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/
int pinArray[] = {2, 3, 4, 5, 6};
int count = 0;
int timer = 0;
void setup(){
Serial.begin(9600);
for (count=0;count<5;count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() {
if (Serial.available() > 0)
// read the incoming byte:
timer = Serial.read();
for (count=0;count<4;count++){
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
for (count=4;count>0;count--){
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
{
// Serial.print ("You entered ASCII value ");
// Serial.println(timer, BYTE); //print as a raw byte value
// Serial.print ("Decimal Value = ");
// Serial.println(timer, DEC); // print as an ASCII-encoded decimal
// Serial.print ("Hexidecimal Value = ");
// Serial.println(timer, HEX); // print as an ASCII-encoded hexadecimal
// Serial.print ("Octal Value = ");
// Serial.println(timer, OCT); // print as an ASCII-encoded octal
// Serial.print ("Binary Value = ");
// Serial.println(timer, BIN); // print as an ASCII-encoded binary
}
}