Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h"
- //#define DEBUG
- #define NUM_LEDS 10
- #define DATA_PIN 12
- #define BUTTON_PIN 3
- #define POT_PIN A0
- CRGB leds[NUM_LEDS];
- int stripState = 0;
- int caseMax = 7;
- int buttonState = LOW;
- int lastButtonState = LOW;
- long lastDebounceTime = 0;
- int debounceDelay = 50;
- long modeTime;
- int modeDelay = 10000;
- void setup() {
- FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- Serial.begin(9600);
- }
- void loop() {
- Serial.println(millis());
- button();
- modeTimer();
- mode();
- FastLED.show();
- }
- void potBrightness() {
- FastLED.setBrightness(map(analogRead(POT_PIN), 0, 1023, 0, 255));
- }
- void button() {
- int buttonReading = digitalRead(BUTTON_PIN);
- if (buttonReading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (buttonReading != buttonState) {
- buttonState = buttonReading;
- if (buttonState == LOW) {
- stripState = (stripState + 1) % caseMax;
- modeTime = millis();
- }
- }
- }
- lastButtonState = buttonReading;
- }
- void modeTimer() {
- if (millis() > modeTime + modeDelay) {
- modeTime = millis();
- stripState = (stripState + 1) % caseMax;
- }
- }
- void debug() {
- #ifdef DEBUG
- Serial.print("Case: ");
- Serial.print(stripState);
- Serial.print("\t");
- Serial.print("Millis: ");
- Serial.print(millis());
- Serial.println("\t");
- Serial.print("Last Switch:");
- Serial.println(modeTime);
- Serial.print("\t");
- Serial.print("Brightness");
- Serial.println(map(analogRead(POT_PIN), 0, 1000, 0, 255));
- #endif
- }
- void mode() {
- switch (stripState) {
- case 0:
- debug();
- changeCRGBPixels(CRGB::Black, NUM_LEDS);
- break;
- case 1:
- debug();
- FastLED.setBrightness(50);
- changeCRGBPixels(CRGB::White, NUM_LEDS);
- break;
- case 2:
- debug();
- changeCRGBPixels(CRGB::White, NUM_LEDS);
- potBrightness();
- break;
- case 3:
- debug();
- changeCRGBPixels(CRGB::Blue, NUM_LEDS);
- potBrightness();
- break;
- case 4:
- debug();
- changeCRGBPixels(CRGB::Green, NUM_LEDS);
- potBrightness();
- break;
- case 5:
- debug();
- changeCHSVPixels(map(analogRead(POT_PIN), 0, 1023, 0, 255), 255, 255);
- break;
- case 6:
- debug();
- int ledsOn = map(analogRead(POT_PIN), 0, 1000, 0, NUM_LEDS);
- if (ledsOn < NUM_LEDS) {
- changeCRGBPixels(CRGB::Black, NUM_LEDS);
- }
- changeCRGBPixels(CRGB::Cyan, ledsOn);
- break;
- }
- }
- void changeCRGBPixels(uint32_t color, int numLEDs) {
- for (int i = 0; i < numLEDs; i++) {
- leds[i] = color;
- }
- }
- void changeCHSVPixels(int h, int s, int v) {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CHSV(h, s, v);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment