Hi,
I'm a complete newbie, and I'm trying some stuff out.
Why doesn't this work as I expect?
What I want to do is use the serial monitor to send a number (as text) and the Arduino should blink the number of times sent. It seems to read only the first byte. If I add a delay of 50, it still does weird things... Am I supposed to read the number of bytes returned by Serial.available() instead of just looping and checking if there's more?
Thanks a million!
int outputPin = 13;
int val;
int counter;
int nrOfTimesToBlink;
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
}
void loop()
{
counter = 0;
val = 0;
nrOfTimesToBlink = 0;
while (Serial.available() > 0) {
val = Serial.read() - 48;
if (val >= 0 && val < 10)
nrOfTimesToBlink = (nrOfTimesToBlink * 10) + val;
}
while (counter < nrOfTimesToBlink) {
digitalWrite(outputPin, HIGH);
delay(200);
digitalWrite(outputPin, LOW);
delay(200);
counter++;
}
}
Arduino Diecemilla
Arduino - 0011 Alpha
Ubuntu Linux 8.10