Advertisement
macca-nz

12_LED_Chase_with_BUTTON

Dec 29th, 2020 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * 30th December 2020
  3.  * Original code by https://www.baldengineer.com/arduino-chasing-leds-with-millis.html
  4.  *
  5.  * Modified by (Ralph McCleery) https://www.facebook.com/macca448/
  6.  * My changes are: #1 increased LED count to 10,
  7.  *                 #2 The PIN map is for an ESP8266 Dev board
  8.  *                 #3 defined PIN number,        
  9.  *                 #4 Invers the effect on 4 count
  10.  */
  11. #include "OneButton.h"
  12. // Simple macros to remember which direction to shift LEDs
  13. #define UP true
  14. #define DOWN false
  15. #define led_count 12
  16. OneButton button(14, true);
  17. // Will be used to track how long since last event "fired"
  18. unsigned long previousMillis=0;
  19.  
  20. // Delay to determine when we see the next LED
  21. unsigned long interval = 75;
  22.  
  23. // Array with Arduino pins containing LEDs in sequence
  24. byte LEDpins[led_count] = {
  25.   //3,15,13,12,14,2,0,4,5,16 // This is a 10 LEDs ESP8266
  26.   15,2,4,16,17,5,18,19,21,3,1,22}; // This is a 12 LEDs ESP32
  27. byte runCount = 0;
  28. byte runState = 0;
  29.  
  30. // Variable to track which LED to turn on, start at 000001
  31. int LEDstate=0x00;
  32.  
  33. // State variable to know which direction to shift
  34. boolean direction=UP;
  35.  
  36. void setup() {
  37.   button.attachClick(singleclick);
  38.   // Set Pins with LEDs to OUTPUT
  39.   for (int x=0; x < led_count; x++){
  40.     pinMode(LEDpins[x], OUTPUT);
  41.   }
  42. }  
  43. void singleclick(){
  44.   runState = !runState;
  45.     for(int x=0; x<led_count; x++){
  46.       if(runState == HIGH){
  47.         runCount = 8;
  48.         LEDstate = 0x01;
  49.       }else if(runState == LOW &&  runCount <= 3){
  50.         LEDstate = 0x00;
  51.       }else if(runState == LOW &&  runCount > 3){
  52.         LEDstate = 0xFFF;
  53.       }
  54.     }
  55. }
  56.  
  57. void loop() {
  58.   button.tick();
  59.   //The runCount is set to 4 loops then then inverse
  60.   if(runCount > 7){
  61.     runCount = 0;
  62.   }
  63.   // Set the pins of each LED during each iteration
  64.   // You'll only see something change when "LEDpins" gets updated
  65.   for (int x=0; x < led_count; x++)
  66.   if(runCount <= 3){
  67.     digitalWrite(LEDpins[x], bitRead(LEDstate,x));
  68.   }else if(runCount > 3){
  69.     digitalWrite(LEDpins[x], !bitRead(LEDstate,x));
  70.   }
  71.  
  72.   if(runState == HIGH){
  73.   // Get current time and determine how long since last check
  74.   unsigned long currentMillis = millis();
  75.   if ((unsigned long)(currentMillis - previousMillis) >= interval) {
  76.     // We've waited "interval" amount of time, so let's do some stuff!
  77.  
  78.     // "Reset" our clock
  79.     previousMillis = currentMillis;
  80.  
  81.     if (direction==UP) {
  82.       // Use "<<" to "bit-shift" everything to the left once
  83.       LEDstate = LEDstate << 1;
  84.       // 0x20 is the "last" LED, another shift makes the value 0x40
  85.       if (LEDstate == 0x1000) {
  86.         // turn on the one before "0x20" and reverse direction. Originally 0x10
  87.         LEDstate = 0x400;
  88.         direction = DOWN;
  89.       }
  90.     }
  91.     else {
  92.       // use ">>" to "bit-shift" all bits in LEDstate once to the right
  93.       LEDstate = LEDstate >> 1;
  94.       // This means we ran out of bits!
  95.       if (LEDstate == 0x00) {
  96.         runCount = runCount +1;
  97.         // set one ahead so no extra delay
  98.         LEDstate = 0x02;
  99.         direction = UP;
  100.        }
  101.     }
  102.   }
  103.  }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement