Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 30th December 2020
- * Original code by https://www.baldengineer.com/arduino-chasing-leds-with-millis.html
- *
- * Modified by (Ralph McCleery) https://www.facebook.com/macca448/
- * My changes are: #1 increased LED count to 10,
- * #2 The PIN map is for an ESP8266 Dev board
- * #3 defined PIN number,
- * #4 Invers the effect on 4 count
- */
- #include "OneButton.h"
- // Simple macros to remember which direction to shift LEDs
- #define UP true
- #define DOWN false
- #define led_count 12
- OneButton button(14, true);
- // Will be used to track how long since last event "fired"
- unsigned long previousMillis=0;
- // Delay to determine when we see the next LED
- unsigned long interval = 75;
- // Array with Arduino pins containing LEDs in sequence
- byte LEDpins[led_count] = {
- //3,15,13,12,14,2,0,4,5,16 // This is a 10 LEDs ESP8266
- 15,2,4,16,17,5,18,19,21,3,1,22}; // This is a 12 LEDs ESP32
- byte runCount = 0;
- byte runState = 0;
- // Variable to track which LED to turn on, start at 000001
- int LEDstate=0x00;
- // State variable to know which direction to shift
- boolean direction=UP;
- void setup() {
- button.attachClick(singleclick);
- // Set Pins with LEDs to OUTPUT
- for (int x=0; x < led_count; x++){
- pinMode(LEDpins[x], OUTPUT);
- }
- }
- void singleclick(){
- runState = !runState;
- for(int x=0; x<led_count; x++){
- if(runState == HIGH){
- runCount = 8;
- LEDstate = 0x01;
- }else if(runState == LOW && runCount <= 3){
- LEDstate = 0x00;
- }else if(runState == LOW && runCount > 3){
- LEDstate = 0xFFF;
- }
- }
- }
- void loop() {
- button.tick();
- //The runCount is set to 4 loops then then inverse
- if(runCount > 7){
- runCount = 0;
- }
- // Set the pins of each LED during each iteration
- // You'll only see something change when "LEDpins" gets updated
- for (int x=0; x < led_count; x++)
- if(runCount <= 3){
- digitalWrite(LEDpins[x], bitRead(LEDstate,x));
- }else if(runCount > 3){
- digitalWrite(LEDpins[x], !bitRead(LEDstate,x));
- }
- if(runState == HIGH){
- // Get current time and determine how long since last check
- unsigned long currentMillis = millis();
- if ((unsigned long)(currentMillis - previousMillis) >= interval) {
- // We've waited "interval" amount of time, so let's do some stuff!
- // "Reset" our clock
- previousMillis = currentMillis;
- if (direction==UP) {
- // Use "<<" to "bit-shift" everything to the left once
- LEDstate = LEDstate << 1;
- // 0x20 is the "last" LED, another shift makes the value 0x40
- if (LEDstate == 0x1000) {
- // turn on the one before "0x20" and reverse direction. Originally 0x10
- LEDstate = 0x400;
- direction = DOWN;
- }
- }
- else {
- // use ">>" to "bit-shift" all bits in LEDstate once to the right
- LEDstate = LEDstate >> 1;
- // This means we ran out of bits!
- if (LEDstate == 0x00) {
- runCount = runCount +1;
- // set one ahead so no extra delay
- LEDstate = 0x02;
- direction = UP;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement