Go Down

Topic: delay prog. execution by waiting for keybord input (Read 866 times) previous topic - next topic

MHz

From a newbie:
This should delay program execution until key pressed. However program starts over and over again:

void setup() {
     Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
}


int incomingByte = 0;      // for incoming serial data
int NoB=0;  

void loop() {
       Serial.println("start of program");
       
// suspend prog. execution until keyboard data received
       do {
       delay(200);
       NoB=Serial.available();
       } while (NoB=0);
       
// continue prog. execution e.g. read the incoming byte:
     incomingByte = Serial.read();
     Serial.print("I received: ");
     Serial.println(incomingByte, DEC);
     
}

Thanks for any hint

PaulS

Code: [Select]
while (NoB=0);
This is an assignment statement, not an equality check. Change = to ==.

MHz


James C4S

You could shorten your "wait loop" even to this:
Code: [Select]

while(Serial.available()==0);
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

MHz

Thank you James,
I see the difference between an expert and a newbie!

Go Up