Created
April 5, 2020 22:20
-
-
Save dapetcu21/b1692d96dd18d1bcd9a39cf5a69b08c3 to your computer and use it in GitHub Desktop.
RGB magical staff, with hue selector
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
// ATTiny 85 | |
#define LED_B 0 // DIP 5 | |
#define LED_G 1 // DIP 6 | |
#define LED_R 4 // DIP 3 | |
#define BUTTON 3 // DIP 2 | |
// ATMega 1284p | |
//#define LED_R 6 | |
//#define LED_G 13 | |
//#define LED_B 15 | |
//#define BUTTON 1 | |
// 2.8 gamma correction | |
const uint8_t PROGMEM gamma8[] = { | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, | |
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, | |
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, | |
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, | |
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, | |
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, | |
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, | |
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, | |
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, | |
90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 109, 110, 112, 114, | |
115, 117, 119, 120, 122, 124, 126, 127, 129, 131, 133, 135, 137, 138, 140, 142, | |
144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 167, 169, 171, 173, 175, | |
177, 180, 182, 184, 186, 189, 191, 193, 196, 198, 200, 203, 205, 208, 210, 213, | |
215, 218, 220, 223, 225, 228, 231, 233, 236, 239, 241, 244, 247, 249, 252, 255 | |
}; | |
#define gamma(x) (pgm_read_byte(&gamma8[(x)])) | |
void setLEDColor(uint8_t red, uint8_t green, uint8_t blue) { | |
analogWrite(LED_R, gamma(red)); | |
analogWrite(LED_G, gamma(green)); | |
analogWrite(LED_B, gamma(blue)); | |
} | |
uint8_t hueToColorComponent(double hue) { | |
while (hue < 0) { hue += 360.0; } | |
while (hue >= 360.0) { hue -= 360.0; } | |
if (hue > 240.0) { return 0; } | |
if (hue > 180.0) { return (255.0 / 60.0) * (240.0 - hue); } | |
if (hue < 60.0) { return (255.0 / 60.0) * hue; } | |
return 255; | |
} | |
void hueToColor(double hue, uint8_t& r, uint8_t& g, uint8_t& b) { | |
r = hueToColorComponent(hue); | |
g = hueToColorComponent(hue + 120.0); | |
b = hueToColorComponent(hue + 240.0); | |
} | |
uint8_t r, g, b; | |
double hue, brightness, target_brightness; | |
bool set_mode, enabled, continous_set_mode; | |
int button, last_button; | |
int button_timeout; | |
uint8_t set_step; | |
void onButtonPress() { | |
if (set_mode) { | |
set_mode = false; | |
} else { | |
enabled = !enabled; | |
} | |
} | |
void onLongButtonPress() { | |
if (!enabled) { | |
enabled = true; | |
return; | |
} | |
if (!set_mode) { | |
set_mode = true; | |
continous_set_mode = false; | |
set_step = 0; | |
} else { | |
continous_set_mode = true; | |
hue = 0.0; | |
} | |
} | |
void set_target_brightness() { | |
target_brightness = random(6144, 8193) * (1.0 / 8192.0); | |
} | |
void setup() { | |
hue = 0.0; | |
brightness = 0.0; | |
set_mode = false; | |
enabled = true; | |
r = 255; g = 255; b = 255; | |
button = HIGH; | |
last_button = HIGH; | |
pinMode(BUTTON, INPUT_PULLUP); | |
set_target_brightness(); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
int button_val = digitalRead(BUTTON); | |
if (button == button_val) { | |
if (button == LOW) { | |
if (last_button == HIGH) { button_timeout = 30; } | |
button_timeout -= 1; | |
if (button_timeout == 0) { | |
onLongButtonPress(); | |
} | |
} else { | |
if (last_button == LOW && button_timeout > 0) { | |
onButtonPress(); | |
} | |
} | |
last_button = button; | |
} else { | |
button = button_val; | |
} | |
if (set_mode) { | |
if (continous_set_mode) { | |
hue += 1.0; | |
while (hue >= 360.0f) { hue -= 360.0f; } | |
while (hue < 0.0f) { hue += 360.0f; } | |
hueToColor(hue, r, g, b); | |
} else { | |
set_step += 1; | |
if (set_step >= 32 * 7) { set_step = 0; } | |
uint8_t mask = (set_step >> 5) + 1; | |
r = (mask & 4) ? 255 : 0; | |
g = (mask & 2) ? 255 : 0; | |
b = (mask & 1) ? 255 : 0; | |
} | |
} | |
double new_brightness = enabled | |
? (set_mode ? 1.0 : target_brightness) | |
: 0.0; | |
if (brightness < new_brightness) { | |
brightness = brightness += 0.05; | |
if (brightness > new_brightness) { | |
brightness = new_brightness; | |
if (enabled && !set_mode) { set_target_brightness(); } | |
} | |
} else { | |
brightness = brightness -= 0.05; | |
if (brightness < new_brightness) { | |
brightness = new_brightness; | |
if (enabled && !set_mode) { set_target_brightness(); } | |
} | |
} | |
setLEDColor(r * brightness, g * brightness, b * brightness); | |
delay(30); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment