Advertisement
Guest User

ArduinoSerialWaitTest

a guest
Feb 24th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1.  
  2. int incomingByte = 0;
  3.  
  4. void setup(){
  5. Serial.begin(9600);
  6.  
  7.   while (Serial.available() <= 0) {//keep sending until we get some bytes from the pc
  8.     Serial.write(42);   // just a constant
  9.     delay(100);
  10.   }
  11.   while(Serial.available()>0) Serial.read(); //immediately after we get something from the PC clear the input buffer by reading from it until it's empty
  12. }
  13.  
  14. void loop(){
  15.  
  16.   if (Serial.available()>0) {
  17.     incomingByte=Serial.read();
  18.       delay(incomingByte*50); //simulate waiting for a physical event
  19.       Serial.write(incomingByte);//echo the input byte
  20.   }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement