I think your code will do what you want. But you don't need Serial.flush(). It is badly named and it is only relevant (and then not much) when the Arduino is sending data.
@robin2 - I think the op wants to throw away all input before the 3 seconds, hence the flush. The old spec (only on the arduino) was to clear the input buffer.
holmes4: @robin2 - I think the op wants to throw away all input before the 3 seconds, hence the flush. The old spec (only on the arduino) was to clear the input buffer.
Mark
Looking at his code, the flush statement is inside the if (3 seconds elapsed) loop. How will it flush anything before 3 seconds has elapsed?
unsigned long interval = 3000; //delay
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long currentMillis = millis(); // Get current time
while ((millis() - currentMillis) < interval) // Has interval expired?
{
while (Serial.available() > 0) // Any serial data arrived?
{
byte value = Serial.read(); // read and discard it
}
}
// Interval is up so next serial read is wanted
}