-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledControl.ino
159 lines (140 loc) · 3.41 KB
/
ledControl.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
#define RED 4
#define GREEN 5
#define BLUE 6
#define delayTime 20
#define DEBUG
#define VERBOSE
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
#ifdef DEBUG
Serial.begin(9600);
#endif
}
int redVal;
int blueVal;
int greenVal;
void loop() {
/*
setColor(0xFFFFFFL);
delay(1000);
setColor(0x00FF0000L);
delay(500);
setColor(0x00FF00L);
delay(500);
setColor(0x0000FFL);
delay(500);
setColor(255,255,255);
delay(1000);
setColor(255,0,0);
delay(500);
setColor(0,255,0);
delay(500);
setColor(0,0,255);
delay(500);
setColor(255,0,255);
delay(500);
setColor(0,255,255);
delay(500);
setColor(255,255,0);
delay(500);
setColor(255,255,255);
delay(500);
for (int i = 255; i > 0; i--) {
setColor(i, i, i);
delay(20);
}
delay(500);
for (int i = 0; i < 255; i++) {
setColor(i, 0, i);
delay(20);
}*/
// pass();
// fail();
gradient(255,255,0, 128,0,255);
gradient(128,0,255, 255,0,0);
gradient(255,0,0, 0,255,0);
gradient(0,255,0, 255,255,0, 1000, 32);
delay(500);
off(100);
}
void setColor(unsigned long rgb) {
int red = (rgb & 0xFF0000L) >> 16;
int green = (rgb & 0x00FF00L) >> 8;
int blue = rgb & 0xFFL;
setColor(red, green, blue);
}
void setColor(int red, int green, int blue) {
analogWrite(RED, red);
analogWrite(GREEN, green);
analogWrite(BLUE, blue);
}
void gradient(int startRed, int startGreen, int startBlue, int endRed, int endGreen, int endBlue) {
gradient(startRed, startGreen, startBlue, endRed, endGreen, endBlue, 1000, 16);
}
void gradient(int startRed, int startGreen, int startBlue, int endRed, int endGreen, int endBlue, int duration) {
gradient(startRed, startGreen, startBlue, endRed, endGreen, endBlue, duration, 16);
}
void gradient(int startRed, int startGreen, int startBlue, int endRed, int endGreen, int endBlue, int duration, int steps) {
#ifdef DEBUG
Serial.print("In gradient(");
Serial.print(startRed);
Serial.print(",x,x, ");
Serial.print(endRed);
Serial.println(",x,x)");
#endif
float stepRed = (endRed - startRed) / steps; // It's ok if these are negative, because they will still get 'added' to the start color.
float stepGreen = (endGreen - startGreen) / steps;
float stepBlue = (endBlue - startBlue) / steps;
Serial.print("stepRed = ");
Serial.println(stepRed);
for (int i = 0; i <= steps; i++) {
setColor(startRed, startGreen, startBlue);
startRed += stepRed;
startGreen += stepGreen;
startBlue += stepBlue;
delay(duration / steps);
}
#ifdef DEBUG
assertEqual(endRed, startRed, stepRed);
#endif
}
void off(int duration) {
setColor(0x000000);
delay(duration);
}
void assertEqual(int expected, int actual) {
assertEqual(expected, actual, 0);
}
void assertEqual(int expected, int actual, int tolerance) {
tolerance = abs(tolerance);
if (expected <= actual + tolerance && expected >= actual - tolerance) pass();
else {
fail();
Serial.print("Expected: ");
Serial.print(expected);
Serial.print(". Actual: ");
Serial.print(actual);
Serial.print(". Tolerance: ");
Serial.print(tolerance);
Serial.println(".");
}
}
void pass() {
#ifdef VERBOSE
setColor(0x00FF00);
delay(100);
#endif
}
void fail() {
for (int i = 0; i < 5; i++) {
setColor(0xFF0000);
delay(150);
setColor(0x000000);
delay(50);
}
}