Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. const byte BUTTON = 2;
  2. const byte LED_START = 3;
  3.  
  4. const byte LEDS = 4;
  5.  
  6. boolean autoMode = true;
  7. const int autoDelay = 500;
  8. byte val = 0;
  9. int buttonState = false;
  10. int lastButton  = false;
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.  
  16.   pinMode(BUTTON, INPUT);
  17.  
  18.   for (byte i = 0; i < LEDS; ++i)
  19.     pinMode(LED_START + i, OUTPUT);
  20. }
  21.  
  22. void loop()
  23. {
  24.   buttonState = digitalRead(BUTTON);
  25.  
  26.   if ((buttonState != lastButton) || autoMode)
  27.   {
  28.     if ((buttonState == HIGH) || autoMode)
  29.     {
  30.       if (autoMode)
  31.         delay(autoDelay);
  32.        
  33.       Serial.print("Button press: ");
  34.       Serial.print("val = ");
  35.       Serial.println((int)val);
  36.       val += 1;
  37.    
  38.       Serial.println(pow(2, LEDS));
  39.       val %= (byte)(pow(2, LEDS));
  40.     }
  41.   }
  42.  
  43.   lastButton = buttonState;
  44.  
  45.   for (byte i = 0; i < LEDS; ++i)
  46.   {
  47.     Serial.print("LED");
  48.     Serial.print((int)i);
  49.     Serial.print(" = ");
  50.     Serial.println(bitRead(val, i));
  51.    
  52.     digitalWrite(LED_START + i, bitRead(val, i));
  53.   }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement