Last active
March 15, 2025 18:08
-
-
Save BlvckBytes/7658bc0647fd8729c9c38b923c27c9ee to your computer and use it in GitHub Desktop.
Simple brightness- and color-temperature controller for a strip of WS2812-LEDs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| #include <EEPROM.h> | |
| #include <Adafruit_NeoPixel.h> | |
| #define LED_DATA_PIN 11 | |
| #define LED_COUNT 100 | |
| #define LED_WRITE_CLEARANCE_MS 5 | |
| #define POT_TEMPERATURE A1 | |
| #define POT_BRIGHTNESS A2 | |
| #define POT_DELTA_THRESHOLD 2 // [0;255] | |
| // 0 255 | |
| // |==============================| | |
| // WARM COLD | |
| // |====================| | |
| // Pot: 0% 100% | |
| // |==============================| | |
| // |====| |====| | |
| // WARM_SHIFT COLD_SHIFT | |
| #define COLOR_WARM 0xFF4600 | |
| #define COLOR_WARM_SHIFT 0 // [0;255] | |
| #define COLOR_COLD 0xFFFFFF | |
| #define COLOR_COLD_SHIFT 80 // [0;255] | |
| Adafruit_NeoPixel leds(LED_COUNT, LED_DATA_PIN, NEO_GRB + NEO_KHZ800); | |
| uint8_t current_temperature; | |
| uint8_t last_stored_temperature; | |
| uint8_t current_brightness; | |
| uint8_t last_stored_brightness; | |
| bool is_initial_update = true; | |
| void load_eeprom() | |
| { | |
| EEPROM.begin(); | |
| current_temperature = EEPROM.read(0); | |
| last_stored_temperature = current_temperature; | |
| current_brightness = EEPROM.read(1); | |
| last_stored_brightness = current_brightness; | |
| } | |
| void save_eeprom() | |
| { | |
| EEPROM.write(0, current_temperature); | |
| last_stored_temperature = current_temperature; | |
| EEPROM.write(1, current_brightness); | |
| last_stored_brightness = current_brightness; | |
| } | |
| void update_leds() | |
| { | |
| double p_warm = map(current_temperature, 0, 255, COLOR_COLD_SHIFT, 255 - COLOR_WARM_SHIFT) / 256.0; | |
| double p_cold = 1 - p_warm; | |
| uint32_t current_r = ((uint32_t) (((COLOR_COLD >> 16) & 0xFF) * p_cold + ((COLOR_WARM >> 16) & 0xFF) * p_warm)) & 0xFF; | |
| uint32_t current_g = ((uint32_t) (((COLOR_COLD >> 8) & 0xFF) * p_cold + ((COLOR_WARM >> 8) & 0xFF) * p_warm)) & 0xFF; | |
| uint32_t current_b = ((uint32_t) ((COLOR_COLD & 0xFF) * p_cold + (COLOR_WARM & 0xFF) * p_warm)) & 0xFF; | |
| leds.setBrightness(255); | |
| leds.fill((current_r << 16) | (current_g << 8) | current_b, 0, LED_COUNT); | |
| leds.setBrightness(current_brightness); | |
| leds.show(); | |
| delay(LED_WRITE_CLEARANCE_MS); | |
| } | |
| void setup() | |
| { | |
| leds.begin(); | |
| delay(50); | |
| load_eeprom(); | |
| pinMode(POT_TEMPERATURE, INPUT_PULLUP); | |
| pinMode(POT_BRIGHTNESS, INPUT_PULLUP); | |
| } | |
| void loop() | |
| { | |
| current_temperature = analogRead(POT_TEMPERATURE) / 4; | |
| current_brightness = analogRead(POT_BRIGHTNESS) / 4; | |
| if ( | |
| is_initial_update || | |
| abs(last_stored_brightness - current_brightness) > POT_DELTA_THRESHOLD || | |
| abs(last_stored_temperature - current_temperature) > POT_DELTA_THRESHOLD | |
| ) | |
| { | |
| if (is_initial_update) | |
| is_initial_update = false; | |
| else | |
| save_eeprom(); | |
| update_leds(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment