Repository navigation
Expand file tree
/
Copy pathsimple_nightlight.ino
More file actions
162 lines (147 loc) · 3.35 KB
/
Copy pathsimple_nightlight.ino
File metadata and controls
162 lines (147 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <EEPROM.h>
#ifdef __AVR_ATtiny85__
#define SWITCH_PIN 0
#define RED_PIN 1
#define GREEN_PIN 2
#define BLUE_PIN 3
#else
#define SWITCH_PIN 2
#define LED_PIN 3
// TODO
#endif
#define COLORS 7
#define STATE_IDLE 0
#define STATE_BUTTON_DOWN 1
#define STATE_BUTTON_RELEASED 2
#define STATE_TRANSITION 3
#define STATE_SHOW_COLOR 4
#define WHITE 0
#define YELLOW 1
#define RED 2
#define PURPLE 3
#define BLUE 4
#define CYAN 5
#define GREEN 6
#define COLOR_SETTING 0
#define LOOP_DELAY_MILLIS 40
int state = STATE_SHOW_COLOR;
int debounce = 0;
int color = 0;
int next_color = 0;
int write_settings = 0;
int clampColor(int color) {
return color >= COLORS? 0 : color;
}
void setup() {
pinMode(SWITCH_PIN, INPUT);
digitalWrite(SWITCH_PIN, HIGH);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
color = clampColor(EEPROM.read(COLOR_SETTING));
}
void showColor(int color) {
switch(color) {
case WHITE: {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
break;
case YELLOW: {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}
break;
case RED: {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
}
break;
case PURPLE: {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
break;
case BLUE: {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
break;
case CYAN: {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
break;
case GREEN: {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}
break;
}
}
void blendColors(int percentage, int color1, int color2) {
for ( int i = 0; i < 100; i++ ) {
showColor(i > percentage? color1 : color2);
delayMicroseconds(1);
}
}
void loop() {
switch(state) {
case STATE_IDLE: {
if ( digitalRead(SWITCH_PIN) == LOW ) {
state = STATE_BUTTON_DOWN;
debounce = 0;
}
else if ( write_settings > 0 ) {
write_settings--;
if ( write_settings == 0 ) {
if ( clampColor(EEPROM.read(COLOR_SETTING)) != color ) {
EEPROM.write(COLOR_SETTING, color);
}
}
}
}
break;
case STATE_BUTTON_DOWN: {
// make sure button held down for long enough
if ( digitalRead(SWITCH_PIN) == LOW ) {
debounce++;
}
else if ( debounce >= 2 ) {
state = STATE_BUTTON_RELEASED;
}
else {
state = STATE_IDLE;
}
}
break;
case STATE_BUTTON_RELEASED: {
next_color = clampColor(color + 1);
state = STATE_TRANSITION;
}
break;
case STATE_TRANSITION: {
state = STATE_SHOW_COLOR;
for ( int i = 0; i < 33; i++ ) {
blendColors(3*(i+1), color, next_color);
}
color = next_color;
// wait a bit before we write settings away
write_settings = 800/LOOP_DELAY_MILLIS;
}
break;
case STATE_SHOW_COLOR: {
showColor(color);
state = STATE_IDLE;
}
break;
}
delay(LOOP_DELAY_MILLIS);
}