Advertisement
Guest User

2 untested examples

a guest
Sep 2nd, 2014
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. void loop()
  2. {
  3.     int blink = 0 /* if you initialize incomingByte somewhere else, you should probably move this one there too */
  4.  
  5.     while (1) /* loop forever */
  6.     {
  7.         if (Serial.available()) /* read and process commands on the serial line */
  8.         {
  9.             incomingByte = Serial.read();
  10.  
  11.             switch (incomingByte)
  12.             {
  13.                 case '1' : /* enable blinking */
  14.                 {
  15.                     blink = 1;
  16.                     break;
  17.                 }
  18.                 case '2' : /* disable blinking */
  19.                 {
  20.                     blink = 0;
  21.                     break;
  22.                 }
  23.             }
  24.         }
  25.  
  26.         if (blink) /* when enabled, blink leds */
  27.         {
  28.             digitalWrite(led2, HIGH);
  29.             delay(300);
  30.             digitalWrite(led2, LOW);
  31.             digitalWrite(led1, HIGH);
  32.             delay(300)
  33.             digitalWrite(led1, LOW);
  34.         }
  35.     }
  36. }
  37.  
  38. less expandable option but possibly working:
  39.  
  40. void loop()
  41. {
  42.     if (Serial.available()) /* read command on the serial line */
  43.     {
  44.         incomingByte = Serial.read();
  45.     }
  46.  
  47.     /* if there is nothing on the serial line, then the previous value is used, so if you pressed 1 then it is
  48.        still 1, anything else than a 1 will disable the show until you press 1 again */
  49.  
  50.     if (incomingByte == '1') /* when enabled, blink leds */
  51.     {
  52.         digitalWrite(led2, HIGH);
  53.         delay(300);
  54.         digitalWrite(led2, LOW);
  55.         digitalWrite(led1, HIGH);
  56.         delay(300)
  57.         digitalWrite(led1, LOW);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement