-
Notifications
You must be signed in to change notification settings - Fork 1
/
dragonlights.ino
207 lines (177 loc) · 4.33 KB
/
dragonlights.ino
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// sketch setup
int delay_ms = 100;
// light pins (EL + LED)
int red_body_pin_start = 2;
int green_body_pin_start = 6;
int pins_per_color = 4;
// state tracking for lighting programs
int fade_index = 0;
int fade_ticks = 0;
int program_mode_id = 0;
// buttons!
int program_button_solid_green = 12;
int program_button_solid_red = 11;
int program_button_fade_to_green = 10;
// button debouncing
int button_pressed_state = LOW; // change for normally-open
int program_transition_to = 0;
// light helper methods
void turn_on_green() {
for (int i=0; i<pins_per_color; i++) {
digitalWrite(green_body_pin_start + i, HIGH);
}
}
void turn_on_red() {
for (int i=0; i<pins_per_color; i++) {
digitalWrite(red_body_pin_start + i, HIGH);
}
}
void turn_off_green() {
for (int i=0; i<pins_per_color; i++) {
digitalWrite(green_body_pin_start + i, LOW);
}
}
void turn_off_red() {
for (int i=0; i<pins_per_color; i++) {
digitalWrite(red_body_pin_start + i, LOW);
}
}
void pulse_green(int ticks) {
turn_on_green();
fade_ticks++;
if (fade_ticks > ticks) {
fade_index++;
fade_ticks = 0;
}
}
void pulse_red(int ticks) {
turn_on_red();
fade_ticks++;
if (fade_ticks > ticks) {
fade_index++;
fade_ticks = 0;
}
}
void setup() {
for (int i=0; i<pins_per_color; i++) {
pinMode(red_body_pin_start + i, OUTPUT);
digitalWrite(red_body_pin_start + i, LOW);
}
for (int i=0; i<pins_per_color; i++) {
pinMode(green_body_pin_start + i, OUTPUT);
digitalWrite(green_body_pin_start + i, LOW);
}
pinMode(program_button_solid_green, INPUT);
digitalWrite(program_button_solid_green, HIGH);
pinMode(program_button_solid_red, INPUT);
digitalWrite(program_button_solid_red, HIGH);
pinMode(program_button_fade_to_green, INPUT);
digitalWrite(program_button_fade_to_green, HIGH);
Serial.begin(9600);
}
void run_program_mode(int id) {
switch(id) {
case 0:
program_nothing();
break;
case 1:
program_green_solid();
break;
case 2:
program_red_solid();
break;
case 3:
program_fade_to_green();
break;
}
}
void check_program_change() {
check_button_program(program_button_solid_green, 1);
check_button_program(program_button_solid_red, 2);
check_button_program(program_button_fade_to_green, 3);
}
void check_button_program(int pin, int program_index) {
if (digitalRead(pin) == button_pressed_state){
if (program_transition_to == program_index) {
program_mode_id = program_index;
program_transition_to = 0;
}
else {
program_transition_to = program_index;
Serial.println("switch program");
Serial.println(program_index);
}
}
}
void program_nothing() {}
void program_green_solid() {
turn_off_red();
turn_on_green();
}
void program_red_solid() {
turn_off_green();
turn_on_red();
}
void program_fade_to_green() {
switch(fade_index) {
// 3/4 time red, 1/4 time green
case 0:
turn_off_red();
pulse_green(3);
break;
case 1:
turn_off_green();
pulse_red(9);
break;
// 1/2 time each color
case 2:
turn_off_red();
pulse_green(6);
break;
case 3:
turn_off_green();
pulse_red(6);
break;
// 3/4 time green, 1/4 time red
case 4:
turn_off_red();
pulse_green(9);
break;
case 5:
turn_off_green();
pulse_red(3);
break;
case 6:
// solid green!
turn_off_red();
turn_on_green();
program_mode_id = 0;
fade_index = 0;
fade_ticks = 0;
break;
}
}
int current_pin = 0;
void loop() {
//for(int i=0; i<pins_per_color*2; i++) {
//digitalWrite(i + red_body_pin_start, HIGH);
//digitalWrite(i + green_body_pin_start, HIGH);
//delay(300);
//digitalWrite(i + red_body_pin_start, LOW);
//digitalWrite(i + green_body_pin_start, LOW);
//}
//run_program_mode(program_mode_id);
//check_program_change();
digitalWrite(2, LOW);
digitalWrite(red_body_pin_start + current_pin, HIGH);
Serial.print("ON: ");
Serial.println(red_body_pin_start + current_pin);
delay(2000); // quarter-second ticks
digitalWrite(red_body_pin_start + current_pin, LOW);
digitalWrite(2, HIGH);
Serial.print("OFF: ");
Serial.println(red_body_pin_start + current_pin);
delay(2000);
current_pin++;
current_pin = current_pin % pins_per_color;
}