/
keypad_test2.ino
60 lines (47 loc) · 1.5 KB
/
keypad_test2.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
#include "Adafruit_NeoTrellisM4.h"
// The NeoTrellisM4 object is a keypad and neopixel strip subclass
// that does things like auto-update the NeoPixels and stuff!
Adafruit_NeoTrellisM4 trellis = Adafruit_NeoTrellisM4();
boolean *lit_keys;
void setup(){
Serial.begin(115200);
trellis.begin();
trellis.setBrightness(80);
Serial.println("toggle keypad test!");
lit_keys = new boolean[trellis.num_keys()];
for (int i=0; i<trellis.num_keys(); i++) {
lit_keys[i] = false;
}
}
void loop() {
// put your main code here, to run repeatedly:
trellis.tick();
while (trellis.available()){
keypadEvent e = trellis.read();
if (e.bit.EVENT == KEY_JUST_PRESSED) {
int key = e.bit.KEY; // shorthand for what was pressed
Serial.print(key); Serial.println(" pressed");
lit_keys[key] = !lit_keys[key];
if (lit_keys[key]) {
trellis.setPixelColor(key, Wheel(random(255)));
} else {
trellis.setPixelColor(key, 0);
}
}
}
delay(10);
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return trellis.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return trellis.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return trellis.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}