BurningWreck

Neopixel Color Picker

Nov 3rd, 2025
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | Software | 0 0
  1. // Neopixel Color Picker
  2. // 11/2/2025
  3.  
  4. // Turn pot to change color or brightness
  5. // Press button to toggle between color and brightness
  6. // Read HSV or RGB value in Serial Monitor
  7.  
  8. #include <FastLED.h>
  9. #define LED_PIN 2
  10. #define NUM_LEDS 1
  11. #define BUTTON_PIN 4
  12. #define POT_PIN A0
  13. CRGB leds[NUM_LEDS];
  14. bool modeColor = true;  // true = color mode, false = brightness mode
  15. int buttonState;        // current stable state
  16. int lastButtonState = HIGH;
  17. unsigned long lastDebounceTime = 0;
  18. const unsigned long debounceDelay = 50;
  19. uint8_t hue = 0;
  20. float brightness = 0.5;
  21.  
  22. // For change detection (reduce serial spam)
  23. static uint8_t lastHue = 255;
  24. static float lastBrightness = -1.0;
  25.  
  26. // Smooth potentiometer reading
  27. int readPotSmooth() {
  28.   int sum = 0;
  29.   for (int i = 0; i < 5; i++) {
  30.     sum += analogRead(POT_PIN);
  31.     delay(2);
  32.   }
  33.   return sum / 5;
  34. }
  35.  
  36. void setup() {
  37.   Serial.begin(9600);
  38.   FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
  39.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  40.   analogReference(DEFAULT);
  41.   FastLED.setBrightness(brightness * 255);
  42.   leds[0] = CHSV(hue, 255, 255);
  43.   FastLED.show();
  44.   Serial.println(F("Arduino Color Picker Example"));
  45.   Serial.println(F("Press button to toggle between color and brightness control"));
  46. }
  47.  
  48. void loop() {
  49.   int reading = digitalRead(BUTTON_PIN);
  50.   // --- Debounce the button ---
  51.   if (reading != lastButtonState) {
  52.     lastDebounceTime = millis();
  53.   }
  54.   if ((millis() - lastDebounceTime) > debounceDelay) {
  55.     if (reading != buttonState) {
  56.       buttonState = reading;
  57.       // Toggle on press (HIGH → LOW)
  58.       if (buttonState == LOW) {
  59.         modeColor = !modeColor;
  60.         Serial.println();
  61.         Serial.println(F("--------------------------"));
  62.         Serial.print(F("Mode changed to: "));
  63.         Serial.println(modeColor ? F("COLOR") : F("BRIGHTNESS"));
  64.         Serial.println(F("--------------------------"));
  65.         // --- Visual indicator flash ---
  66.         if (modeColor) {
  67.           // Flash current hue at *full* brightness for visibility
  68.           FastLED.setBrightness(255);
  69.           leds[0] = CHSV(hue, 255, 255);
  70.         } else {
  71.           // Flash white at current brightness
  72.           FastLED.setBrightness(brightness * 255);
  73.           leds[0] = CRGB::White;
  74.         }
  75.         FastLED.show();
  76.         delay(500);  // flash duration
  77.         // Restore last display
  78.         FastLED.setBrightness(brightness * 255);
  79.         leds[0] = CHSV(hue, 255, 255);
  80.         FastLED.show();
  81.         delay(500);  // pause so user can notice the change
  82.       }
  83.     }
  84.   }
  85.   lastButtonState = reading;
  86.  
  87.   // --- Potentiometer reading (smoothed) ---
  88.   int potValue = readPotSmooth();
  89.  
  90.   if (modeColor) {
  91.     // Map pot to hue range 0–230 (avoid red wraparound)
  92.     hue = map(potValue, 0, 1023, 0, 230);
  93.   } else {
  94.     // Adjust brightness (8%–100% to keep LED visible)
  95.     brightness = map(potValue, 0, 1023, 20, 255) / 255.0;
  96.   }
  97.  
  98.   FastLED.setBrightness(brightness * 255);
  99.   leds[0] = CHSV(hue, 255, 255);
  100.   FastLED.show();
  101.  
  102.   // --- Serial output (only when values change) ---
  103.   bool changed = false;
  104.   if (modeColor && hue != lastHue) {
  105.     lastHue = hue;
  106.     changed = true;
  107.   } else if (!modeColor && abs(brightness - lastBrightness) > 0.01) {
  108.     lastBrightness = brightness;
  109.     changed = true;
  110.   }
  111.  
  112.   if (changed) {
  113.     // Convert HSV to RGB for display
  114.     CRGB rgbColor = CHSV(hue, 255, 255);
  115.  
  116.     Serial.print(F("HSV: ("));
  117.     Serial.print(hue);
  118.     Serial.print(F(", 255, "));
  119.     Serial.print((uint8_t)(brightness * 255));
  120.     Serial.print(F(")  |  RGB: ("));
  121.     Serial.print(rgbColor.r);
  122.     Serial.print(F(", "));
  123.     Serial.print(rgbColor.g);
  124.     Serial.print(F(", "));
  125.     Serial.print(rgbColor.b);
  126.     Serial.print(F(")  |  Brightness: "));
  127.     Serial.print((int)(brightness * 100));
  128.     Serial.println(F("%"));
  129.   }
  130.  
  131.   delay(20);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment