Guest User

No Delay - Not working v2

a guest
Jul 22nd, 2022
140
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. unsigned long previousBlueMillis = 0;
  10. const long blueInterval = 35;
  11. unsigned long previousGoldMillis = 0;
  12. const long goldInterval = 35;
  13. int pixel = LOW;
  14.  
  15.  
  16. //button definitions
  17. #define BUTTON_PIN 8
  18. OneButton button(BUTTON_PIN, true);
  19. int currentAction = 1;
  20.  
  21. void setup() {
  22.  FastLED.addLeds<WS2812B, DATA_PIN, RBG>(leds, NUM_LEDS);
  23.  FastLED.setCorrection(TypicalPixelString);
  24.  
  25.  button.attachClick(singleClick);
  26. }
  27.  
  28. void loop() {
  29.   button.tick();
  30.  
  31. if (currentAction == 1){
  32.   pixelSwitch();
  33.   }
  34.  
  35. }
  36.  
  37. void pixelSwitch(){
  38.  
  39.     unsigned long currentPixMillis = millis();
  40.     if (currentPixMillis - previousPixMillis >= pixInterval) {
  41.        previousPixMillis = currentPixMillis;
  42.  
  43.     if (pixel == LOW){
  44.       blueChase();
  45.       pixel = HIGH;
  46.       }
  47.     else if (pixel == HIGH){
  48.       goldChase();
  49.       pixel = LOW;
  50.       }
  51.     }
  52. }
  53.  
  54. void blueChase(){
  55.     int b = 0;
  56.     unsigned long currentBlueMillis = millis();
  57.     if (currentBlueMillis - previousBlueMillis >= blueInterval) {
  58.       previousBlueMillis = currentBlueMillis;
  59.       b++;
  60.       leds[b]=CRGB::Blue;
  61.       FastLED.show();}
  62.     }
  63.  
  64. void goldChase(){
  65.     int g = 0;
  66.     unsigned long currentGoldMillis = millis();
  67.     if (currentGoldMillis - previousGoldMillis >= goldInterval) {
  68.       previousGoldMillis = currentGoldMillis;
  69.       g++;
  70.       leds[g]=CRGB::Gold;
  71.       FastLED.show();}
  72.     }
  73.  
  74. void singleClick() {
  75.  
  76.   if (currentAction == 1){
  77.     currentAction = 2;
  78.     fill_solid(leds, NUM_LEDS, CRGB::Gold);
  79.     FastLED.show();
  80.   }
  81.   else if (currentAction == 2){
  82.     currentAction = 3;
  83.     fill_solid(leds, NUM_LEDS, CRGB::Blue);
  84.     FastLED.show();
  85.   }
  86.   else if (currentAction == 3){
  87.     currentAction = 1;
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment