/
bargraph_code.ino
251 lines (233 loc) · 7.04 KB
/
bargraph_code.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <Wire.h> // Include the I2C library (required)
#include <SparkFunSX1509.h> // Include SX1509 library
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
SX1509 io; // Create an SX1509 object to be used throughout
// bargraph helper variables
int seq_1_current = 0; // current led in sequence 1
const int num_led = 15; // total number of leds in bar graph
// SX1509 pin definitions for the leds on the graph:
const byte SX1509_BAR_01 = 0;
const byte SX1509_BAR_02 = 1;
const byte SX1509_BAR_03 = 2;
const byte SX1509_BAR_04 = 3;
const byte SX1509_BAR_05 = 4;
const byte SX1509_BAR_06 = 5;
const byte SX1509_BAR_07 = 6;
const byte SX1509_BAR_08 = 7;
const byte SX1509_BAR_09 = 8;
const byte SX1509_BAR_10 = 9;
const byte SX1509_BAR_11 = 10;
const byte SX1509_BAR_12 = 11;
const byte SX1509_BAR_13 = 12;
const byte SX1509_BAR_14 = 13;
const byte SX1509_BAR_15 = 14;
void setup() {
// Call io.begin(<address>) to initialize the SX1509. If it
// successfully communicates, it'll return 1.
if (!io.begin(SX1509_ADDRESS)) {
while (1) ; // If we fail to communicate, loop forever for now but it would be nice to warn the user somehow
}
// configuration for the bargraph LED's
io.pinMode(SX1509_BAR_01, OUTPUT);
io.pinMode(SX1509_BAR_02, OUTPUT);
io.pinMode(SX1509_BAR_03, OUTPUT);
io.pinMode(SX1509_BAR_04, OUTPUT);
io.pinMode(SX1509_BAR_05, OUTPUT);
io.pinMode(SX1509_BAR_06, OUTPUT);
io.pinMode(SX1509_BAR_07, OUTPUT);
io.pinMode(SX1509_BAR_08, OUTPUT);
io.pinMode(SX1509_BAR_09, OUTPUT);
io.pinMode(SX1509_BAR_10, OUTPUT);
io.pinMode(SX1509_BAR_11, OUTPUT);
io.pinMode(SX1509_BAR_12, OUTPUT);
io.pinMode(SX1509_BAR_13, OUTPUT);
io.pinMode(SX1509_BAR_14, OUTPUT);
io.pinMode(SX1509_BAR_15, OUTPUT);
// set everything off initially
shutdown_leds();
}
unsigned long firing_interval = 40; // interval at which to cycle firing lights on the bargraph. We update this in the loop to speed up the animation so must be declared here (milliseconds).
void loop() {
// get the current time
int currentMillis = millis();
}
/*************** Bar Graph Animations *********************/
// This is the idle sequence
unsigned long prevBarMillis_on = 0; // bargraph on tracker
const unsigned long pwrcl_interval = 60; // interval at which to cycle lights (milliseconds).
bool reverseSequenceOne = false;
void barGraphSequenceOne(unsigned long currentMillis) {
// normal sync animation on the bar graph
if ((unsigned long)(currentMillis - prevBarMillis_on) > pwrcl_interval) {
// save the last time you blinked the LED
prevBarMillis_on = currentMillis;
if ( reverseSequenceOne == false ) {
switch_graph_led(seq_1_current, HIGH);
seq_1_current++;
if ( seq_1_current > num_led ) {
reverseSequenceOne = true;
}
} else {
switch_graph_led(seq_1_current, LOW);
seq_1_current--;
if ( seq_1_current < 0 ) {
reverseSequenceOne = false;
}
}
}
}
// This is the firing sequence
unsigned long prevBarMillis_fire = 0; // bargraph firing tracker
int fireSequenceNum = 1;
void barGraphSequenceTwo(unsigned long currentMillis) {
if ((unsigned long)(currentMillis - prevBarMillis_fire) > firing_interval) {
// save the last time you blinked the LED
prevBarMillis_fire = currentMillis;
switch (fireSequenceNum) {
case 1:
switch_graph_led(2, LOW);
switch_graph_led(14, LOW);
switch_graph_led(1, HIGH);
switch_graph_led(15, HIGH);
fireSequenceNum++;
break;
case 2:
switch_graph_led(1, LOW);
switch_graph_led(15, LOW);
switch_graph_led(2, HIGH);
switch_graph_led(14, HIGH);
fireSequenceNum++;
break;
case 3:
switch_graph_led(2, LOW);
switch_graph_led(14, LOW);
switch_graph_led(3, HIGH);
switch_graph_led(13, HIGH);
fireSequenceNum++;
break;
case 4:
switch_graph_led(3, LOW);
switch_graph_led(13, LOW);
switch_graph_led(4, HIGH);
switch_graph_led(12, HIGH);
fireSequenceNum++;
break;
case 5:
switch_graph_led(4, LOW);
switch_graph_led(12, LOW);
switch_graph_led(5, HIGH);
switch_graph_led(11, HIGH);
fireSequenceNum++;
break;
case 6:
switch_graph_led(5, LOW);
switch_graph_led(11, LOW);
switch_graph_led(6, HIGH);
switch_graph_led(10, HIGH);
fireSequenceNum++;
break;
case 7:
switch_graph_led(6, LOW);
switch_graph_led(10, LOW);
switch_graph_led(7, HIGH);
switch_graph_led(9, HIGH);
fireSequenceNum++;
break;
case 8:
switch_graph_led(7, LOW);
switch_graph_led(9, LOW);
switch_graph_led(6, HIGH);
switch_graph_led(10, HIGH);
fireSequenceNum++;
break;
case 9:
switch_graph_led(6, LOW);
switch_graph_led(10, LOW);
switch_graph_led(5, HIGH);
switch_graph_led(11, HIGH);
fireSequenceNum++;
break;
case 10:
switch_graph_led(5, LOW);
switch_graph_led(11, LOW);
switch_graph_led(4, HIGH);
switch_graph_led(12, HIGH);
fireSequenceNum++;
break;
case 11:
switch_graph_led(4, LOW);
switch_graph_led(12, LOW);
switch_graph_led(3, HIGH);
switch_graph_led(13, HIGH);
fireSequenceNum++;
break;
case 12:
switch_graph_led(3, LOW);
switch_graph_led(13, LOW);
switch_graph_led(2, HIGH);
switch_graph_led(14, HIGH);
fireSequenceNum = 1;
break;
}
}
}
/************************* Shutdown and helper functions ****************************/
void shutdown_leds() {
// reset the sequence
seq_1_current = 1;
fireSequenceNum = 1;
// shut all led's off
for (int i = 1; i <= 15; i++) {
switch_graph_led(i, LOW);
}
}
void switch_graph_led(int num, int state) {
switch (num) {
case 1:
io.digitalWrite(SX1509_BAR_01, state);
break;
case 2:
io.digitalWrite(SX1509_BAR_02, state);
break;
case 3:
io.digitalWrite(SX1509_BAR_03, state);
break;
case 4:
io.digitalWrite(SX1509_BAR_04, state);
break;
case 5:
io.digitalWrite(SX1509_BAR_05, state);
break;
case 6:
io.digitalWrite(SX1509_BAR_06, state);
break;
case 7:
io.digitalWrite(SX1509_BAR_07, state);
break;
case 8:
io.digitalWrite(SX1509_BAR_08, state);
break;
case 9:
io.digitalWrite(SX1509_BAR_09, state);
break;
case 10:
io.digitalWrite(SX1509_BAR_10, state);
break;
case 11:
io.digitalWrite(SX1509_BAR_11, state);
break;
case 12:
io.digitalWrite(SX1509_BAR_12, state);
break;
case 13:
io.digitalWrite(SX1509_BAR_13, state);
break;
case 14:
io.digitalWrite(SX1509_BAR_14, state);
break;
case 15:
io.digitalWrite(SX1509_BAR_15, state);
break;
}
}