Advertisement
Guest User

Single line on button press

a guest
Oct 15th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include "FastLED.h"
  2. #define NUM_LEDS 64
  3. #define PIN 2
  4. #define BUTTON_PIN 4
  5. CRGB leds [NUM_LEDS];
  6. uint8_t gHue = 0; //rotating "base" color
  7. bool buttonState = LOW;
  8. bool lastButtonState = LOW;
  9.  
  10. void setup(){
  11. pinMode(BUTTON_LEAD,INPUT_PULLUP);
  12. FastLED.addLeds<WS2812B,PIN,GRB,>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);
  13. FastLED.setBrightness(64);
  14. }
  15.  
  16. void loop(){
  17. if (buttonListener()){line();}
  18. }
  19.  
  20. void fadeall(){for(int i=0; i<NUM_LEDS;i++){leds[i].nscale8(150);}}
  21.  
  22. void line(){
  23. for(int i=0; i<NUM_LEDS+8; i++){
  24. if(buttonListener()) {i=0;}
  25. leds[i] = CHSV (gHue++,255,255);
  26. FastLED.show();
  27. fadeall();
  28. delay(3);
  29. }
  30. }
  31.  
  32. bool buttonListener(){ //monitor button press
  33. bool modeChanged = false;
  34. buttonState = digitalRead(BUTTON_PIN);
  35. if(buttonState != lastButtonState{ if (buttonState == LOW){
  36. mode++ //probably not needed here
  37. modeChanged = true;
  38. delay(150); //debouncing delay
  39. }
  40. }
  41. lastButtonState = buttonState;
  42. return modeChanged;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement