Hi,
Scott here from Salem. My program goal. Press 1 on the key board and an led lights up on the arduino. Press 2 and it goes off. Press anything else and the command port prints out “invalid”. I then want to clear the serial port to erase anything else, because if I press a bunch of numbers e.g. 3456, it prints out invalid several times. I would like it to print out invalid only once. I read that Serial.flush() no longer works. I have tried byte = discard Serial.read() but this does not work. Can someone help me with how to flush the serial port of the remaining data?
Here is the code;
int val = 0;
int led = 13;
void setup()
{
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop()
{
while(Serial.available()== 0); //stays on this line until something's in the buffer
val = Serial.read() - 48; // gives me the correct ascii output character
Serial.println(val);
if(val == 1)
{
digitalWrite(led,HIGH);
}
else if (val == 2)
{
digitalWrite(led,LOW);
}
else
{
Serial.println("invalid");
for (int i = 0; i < 18; i++) // This is where I want to flush any remaing data
byte discard = Serial.read();
}
}
thank you,
Scott