Guest User

No Delay - Not working

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