Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int buttonPin = 2;
  2. int current = 0;
  3. long lastEventMillis = 0;
  4.  
  5. void setup() {
  6.     pinMode(6, OUTPUT);
  7.     pinMode(7, OUTPUT);
  8.     pinMode(8, OUTPUT);
  9.     pinMode(9, OUTPUT);
  10.     pinMode (buttonPin, INPUT);
  11. }
  12.  
  13. void loop() {
  14.     long currentMillis = millis();
  15.  
  16.     // This is only true once every 1 second
  17.     if ((currentMillis - previousMillis) > 1000)
  18.     {
  19.         // Apply when last event toke place
  20.         lastEventMillis = currentMillis;
  21.  
  22.         update();
  23.     }
  24.  
  25.     // When the button is pressed
  26.     if (digitalRead(buttonPin) == HIGH)
  27.     {
  28.         // Apply when last event toke place
  29.         lastEventMillis = currentMillis;
  30.  
  31.         // Reset current position
  32.         current = 0;
  33.         update();
  34.     }
  35. }
  36.  
  37. // Updates the position of the LEDs in order
  38. void update() {
  39.     // Turn all LEDs off
  40.     digitalWrite(6, LOW);
  41.     digitalWrite(7, LOW);
  42.     digitalWrite(8, LOW);
  43.     digitalWrite(9, LOW);
  44.  
  45.     // Enable the LED that matches the current position
  46.     digitalWrite ((current % 4) + 6, LOW);
  47.  
  48.     // Update the current position
  49.     current = current + 1;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement