Guest User

Working Chase (with delay)

a guest
Jul 22nd, 2022
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <OneButton.h>
  2. #include <FastLED.h>
  3.  
  4. #define NUM_LEDS 44
  5. #define DATA_PIN 6
  6. CRGB leds[NUM_LEDS];
  7. unsigned long previousPixMillis = 0;
  8. const long pixInterval = 4000;
  9. int pixel = LOW;
  10.  
  11.  
  12. //button definitions
  13. #define BUTTON_PIN 8
  14. OneButton button(BUTTON_PIN, true);
  15. int currentAction = 1;
  16.  
  17. void setup() {
  18.  FastLED.addLeds<WS2812B, DATA_PIN, RBG>(leds, NUM_LEDS);
  19.  FastLED.setCorrection(TypicalPixelString);
  20.  
  21.  button.attachClick(singleClick);
  22. }
  23.  
  24. void loop() {
  25.   button.tick();
  26.  
  27. if (currentAction == 1){
  28.   pixelSwitch();
  29.   }
  30.  
  31. }
  32.  
  33. void pixelSwitch(){
  34.  
  35.     unsigned long currentPixMillis = millis();
  36.     if (currentPixMillis - previousPixMillis >= pixInterval) {
  37.        previousPixMillis = currentPixMillis;
  38.  
  39.     if (pixel == LOW){
  40.       blueChase();
  41.       pixel = HIGH;
  42.       }
  43.     else if (pixel == HIGH){
  44.       goldChase();
  45.       pixel = LOW;
  46.       }
  47.     }
  48. }
  49.  
  50. void blueChase(){
  51.         for (int i=0; i<NUM_LEDS; i++){
  52.       leds[i]=CRGB::Blue;
  53.       FastLED.show();
  54.       delay(35);
  55.         }
  56. }
  57.  
  58. void goldChase(){
  59.   for (int i=0; i<NUM_LEDS; i++){
  60.       leds[i]=CRGB::Gold;
  61.       FastLED.show();
  62.       delay(35);
  63.   }
  64. }
  65.  
  66.  
  67. void singleClick() {
  68.  
  69.   if (currentAction == 1){
  70.     currentAction = 2;
  71.     fill_solid(leds, NUM_LEDS, CRGB::Gold);
  72.     FastLED.show();
  73.   }
  74.   else if (currentAction == 2){
  75.     currentAction = 3;
  76.     fill_solid(leds, NUM_LEDS, CRGB::Blue);
  77.     FastLED.show();
  78.   }
  79.   else if (currentAction == 3){
  80.     currentAction = 1;
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment