-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrumLight.ino~
198 lines (181 loc) · 5.62 KB
/
DrumLight.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
/*
* Drum light
* by Brian J. Johnson January, 2018
*
* Circuit:
* ATTiny85, powered by 3V to 5V (I used 2xAAA batteries)
* Common-anode RGB LED on redPin, greenPin, bluePin w/appropriate resistors
* Piezo disc between knockPin and ground, with a 10k pulldown
* Pushbutton between buttonPin and ground
*
* Tap the piezo to flash the LED! Push the button to select the next
* LED flash pattern from the list. The current selection is stored
* in NVRAM.
*
* The pulldown on knockPin forms a voltage divider with the internal
* pullup (about 40k.) That produces a bias voltage of about 0.2 x
* Vcc, which is enough to get the voltage produced by the piezo up
* into the range which digitalRead() can sense, while still reading
* as 0 in the normal case. Adjust the resistance to vary the
* sensitivity. Having a path to ground also helps limit the high
* voltage peaks which piezos can produce.
*
* #defining CALIBRATE puts the sketch in a mode where it colors the
* LED based on the voltage ranges for digital pins defined on the
* datasheet. A "low" input should be less than 0.3 x Vcc, and an
* "high" input should be greater than 0.6 x Vcc.
*/
#include "FastLED.h" // For CRGB/CHSV functions
const int redPin = 0;
const int greenPin = 1;
const int bluePin = 4;
const int knockPin = 3;
const int buttonPin = 2;
#ifdef CALIBRATE
const int knockPinAnalog = A3;
#endif
#if 1 // use 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 };
void WriteLed(const CRGB& Led) {
analogWrite(redPin, 255 - pgm_read_byte(&gamma8[Led.r]));
analogWrite(greenPin, 255 - pgm_read_byte(&gamma8[Led.g]));
analogWrite(bluePin, 255 - pgm_read_byte(&gamma8[Led.b]));
}
#else
void WriteLed(const CRGB& Led) {
analogWrite(redPin, 255 - Led.r);
analogWrite(greenPin, 255 - Led.g);
analogWrite(bluePin, 255 - Led.b);
}
#endif
#ifdef CALIBRATE
// Use the LED to indicate the level on the knock sensor input pin,
// so I can calibrate the voltage divider or trimpot.
#define ARANGE 1024 // analogRead() range
void calibrate() {
int val;
int peak = 0;
int d = 500; // ms
unsigned long peakMillis;
unsigned long curMillis;
while (1) {
val = analogRead(knockPinAnalog);
// Hold the highest value seen in the last d milliseconds
curMillis = millis();
if (val > peak + 10) {
peak = val;
peakMillis = curMillis;
}
else if (curMillis - peakMillis > d) {
peak = val;
}
if (peak < 0.25 * ARANGE) { // < 0.3 x Vcc reads as LOW
WriteLed(CRGB::Red);
}
else if (peak < 0.65 * ARANGE) { // 0.3 to 0.6 x Vcc is indeterminate
WriteLed(CRGB::Blue);
}
else if (peak < 0.95 * ARANGE) { // > 0.6 x Vcc reads as HIGH
WriteLed(CRGB::Green);
}
else { // Nearing the top of the range
WriteLed(CRGB::White);
}
}
}
#endif
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(knockPin, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
WriteLed(CRGB::Black);
}
uint8_t Hues[] = {HUE_RED, HUE_GREEN, HUE_BLUE};
void loop() {
CRGB Led;
uint8_t Idx;
uint8_t Idx2;
#ifdef CALIBRATE
calibrate();
#endif
if (digitalRead(knockPin)) {
int d = 500;
WriteLed(CRGB::Red);
delay(d);
WriteLed(CRGB::Green);
delay(d);
WriteLed(CRGB::Blue);
delay(d);
WriteLed(CRGB::Black);
}
#if 0
Idx = 0;
while (1) {
#if 1
int d = 500;
WriteLed(CRGB::Red);
delay(d);
WriteLed(CRGB::Black);
delay(d);
WriteLed(CRGB::Green);
delay(d);
WriteLed(CRGB::Black);
delay(d);
WriteLed(CRGB::Blue);
delay(d);
WriteLed(CRGB::Black);
delay(d);
#elif 0
analogWrite(redPin, 255 - (Idx & 3)); // Test 4 lowest values
delay(500);
Idx++; // 8-bit wraparound
#elif 0
Led = CHSV(Idx, 255, 255); // max out saturation, brightness
WriteLed(Led);
Idx++; // 8-bit wraparound
delay(12);
#elif 0
Led = CHSV(HUE_GREEN, Idx, 255); // vary saturation
WriteLed(Led);
Idx++; // 8-bit wraparound
delay(8);
#elif 0
for (Idx2 = 0; Idx2 < 255; Idx2++) {
Led = CHSV(Hues[Idx], 255, Idx2); // vary value (brightness)
WriteLed(Led);
delay(8);
}
Idx = (Idx + 1) % (sizeof(Hues) / sizeof(Hues[0]));
#elif 0
Led = ColorFromPalette(HeatColors_p, 240 - Idx, 255, LINEARBLEND);
WriteLed(Led);
Idx = (Idx + 1) % 241; // See note in colorpallets.cpp. Use only 0-240
delay(8);
#else
Led = HeatColor(Idx);
WriteLed(Led);
Idx--; // 8-bit wraparound
delay(5);
#endif
}
#endif
}
// See the blend() function in colorutils.h to interpolate between 2 colors