Guest User

Multi-Mode Strip

a guest
Apr 12th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. //#define DEBUG
  4. #define NUM_LEDS 10
  5. #define DATA_PIN 12
  6. #define BUTTON_PIN  3
  7. #define POT_PIN A0
  8.  
  9. CRGB leds[NUM_LEDS];
  10.  
  11. int stripState = 0;
  12. int caseMax = 7;
  13. int buttonState = LOW;
  14. int lastButtonState = LOW;
  15. long lastDebounceTime = 0;
  16. int debounceDelay = 50;
  17. long modeTime;
  18. int modeDelay = 10000;
  19.  
  20. void setup() {
  21.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  22.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  23.   Serial.begin(9600);
  24. }
  25.  
  26. void loop() {
  27.   Serial.println(millis());
  28.   button();
  29.   modeTimer();
  30.   mode();
  31.   FastLED.show();
  32. }
  33.  
  34. void potBrightness() {
  35.   FastLED.setBrightness(map(analogRead(POT_PIN), 0, 1023, 0, 255));
  36. }
  37.  
  38. void button() {
  39.   int buttonReading = digitalRead(BUTTON_PIN);
  40.   if (buttonReading != lastButtonState) {
  41.     lastDebounceTime = millis();
  42.   }
  43.   if ((millis() - lastDebounceTime) > debounceDelay) {
  44.     if (buttonReading != buttonState) {
  45.       buttonState = buttonReading;
  46.       if (buttonState == LOW) {
  47.         stripState = (stripState + 1) % caseMax;
  48.         modeTime = millis();
  49.       }
  50.     }
  51.   }
  52.   lastButtonState = buttonReading;
  53. }
  54.  
  55. void modeTimer() {
  56.   if (millis() > modeTime + modeDelay) {
  57.     modeTime = millis();
  58.     stripState = (stripState + 1) % caseMax;
  59.  
  60.   }
  61. }
  62. void debug() {
  63. #ifdef DEBUG
  64.   Serial.print("Case: ");
  65.   Serial.print(stripState);
  66.   Serial.print("\t");
  67.   Serial.print("Millis: ");
  68.   Serial.print(millis());
  69.   Serial.println("\t");
  70.   Serial.print("Last Switch:");
  71.   Serial.println(modeTime);
  72.   Serial.print("\t");
  73.   Serial.print("Brightness");
  74.   Serial.println(map(analogRead(POT_PIN), 0, 1000, 0, 255));
  75. #endif
  76. }
  77.  
  78. void mode() {
  79.   switch (stripState) {
  80.     case 0:
  81.       debug();
  82.       changeCRGBPixels(CRGB::Black, NUM_LEDS);
  83.       break;
  84.  
  85.     case 1:
  86.       debug();
  87.       FastLED.setBrightness(50);
  88.       changeCRGBPixels(CRGB::White, NUM_LEDS);
  89.       break;
  90.  
  91.     case 2:
  92.       debug();
  93.       changeCRGBPixels(CRGB::White, NUM_LEDS);
  94.       potBrightness();
  95.       break;
  96.  
  97.     case 3:
  98.       debug();
  99.       changeCRGBPixels(CRGB::Blue, NUM_LEDS);
  100.       potBrightness();
  101.       break;
  102.  
  103.     case 4:
  104.       debug();
  105.       changeCRGBPixels(CRGB::Green, NUM_LEDS);
  106.       potBrightness();
  107.       break;
  108.  
  109.     case 5:
  110.       debug();
  111.       changeCHSVPixels(map(analogRead(POT_PIN), 0, 1023, 0, 255), 255, 255);
  112.       break;
  113.  
  114.     case 6:
  115.       debug();
  116.       int ledsOn = map(analogRead(POT_PIN), 0, 1000, 0, NUM_LEDS);
  117.       if (ledsOn < NUM_LEDS) {
  118.         changeCRGBPixels(CRGB::Black, NUM_LEDS);
  119.       }
  120.       changeCRGBPixels(CRGB::Cyan, ledsOn);
  121.       break;
  122.   }
  123. }
  124.  
  125. void changeCRGBPixels(uint32_t color, int numLEDs) {
  126.   for (int i = 0; i < numLEDs; i++) {
  127.     leds[i] = color;
  128.   }
  129. }
  130.  
  131. void changeCHSVPixels(int h, int s, int v) {
  132.   for (int i = 0; i < NUM_LEDS; i++) {
  133.     leds[i] = CHSV(h, s, v);
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment