forked from roger8849/voicemask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.ino
163 lines (139 loc) · 3.82 KB
/
mask.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
//code by Tyler Glaiel
//for this mask: https://twitter.com/TylerGlaiel/status/1265035386109128704
//You will need to also include these libraries in the Arduino IDE
//see here: https://www.arduino.cc/en/guide/libraries
#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoMatrix.h>
#include <gamma.h>
#define lengthof(A) ((sizeof((A))/sizeof((A)[0])))
const PROGMEM uint8_t mouth_0[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{6,6,6,6,6,6,6,6},
{6,6,6,6,6,6,6,6},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
const PROGMEM uint8_t mouth_4[8][8] = {
{0,0,6,6,6,6,0,0},
{0,6,0,0,0,0,6,0},
{6,0,0,0,0,0,0,6},
{6,0,0,0,0,0,0,6},
{6,0,0,0,0,0,0,6},
{6,0,0,0,0,0,0,6},
{0,6,0,0,0,0,6,0},
{0,0,6,6,6,6,0,0}
};
const PROGMEM uint8_t mouth_3[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,6,6,6,6,0,0},
{0,6,0,0,0,0,6,0},
{6,0,0,0,0,0,0,6},
{6,0,0,0,0,0,0,6},
{0,6,0,0,0,0,6,0},
{0,0,6,6,6,6,0,0},
{0,0,0,0,0,0,0,0}
};
const PROGMEM uint8_t mouth_2[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,6,6,6,6,6,6,0},
{6,0,0,0,0,0,0,6},
{6,0,0,0,0,0,0,6},
{0,6,6,6,6,6,6,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
const PROGMEM uint8_t mouth_1[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,6,6,0,0,0},
{6,6,6,0,0,6,6,6},
{6,6,6,0,0,6,6,6},
{0,0,0,6,6,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
const PROGMEM uint8_t mouth_smile[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{6,0,0,0,0,0,0,6},
{6,6,0,0,0,0,6,6},
{0,6,6,6,6,6,6,0},
{0,0,6,6,6,6,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
uint16_t palette[8] = {};
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, 6,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT +
NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
void drawImage(short image_addr){
for(int x = 0; x<8; x++){
for(int y = 0; y<8; y++){
uint8_t index = pgm_read_byte(image_addr+x+y*8);
matrix.drawPixel(x, y, palette[index]);
}
}
matrix.show();
}
int pop_detection = 0;
bool smiling = false;
unsigned long smiletimer = 0;
unsigned long last_face = 0;
void setup() {
matrix.begin();
palette[0] = matrix.Color(0,0,0);
palette[1] = matrix.Color(255,0,0);
palette[2] = matrix.Color(0,255,0);
palette[3] = matrix.Color(0,0,255);
palette[4] = matrix.Color(0,255,255);
palette[5] = matrix.Color(255,0,255);
palette[6] = matrix.Color(255,255,0);
palette[7] = matrix.Color(255,255,255);
Serial.begin(9600);
}
float vol = 0;
const uint16_t samples = 128;
void loop() {
float nvol = 0;
int previous_peak = -1;
for (int i = 0; i<samples; i++){
auto analog = analogRead(A7);
auto micline = abs(analog - 512);
nvol = max(micline, nvol);
}
vol = (nvol + 1.0*vol)/2.0;
if(nvol > 200){
pop_detection += 1;
if(pop_detection > 2) {
smiling = false;
last_face = millis();
}
} else {
if(pop_detection > 0 && pop_detection <= 2) {
if(millis() > last_face + 500){
smiling = true;
smiletimer = millis() + 2000;
}
}
pop_detection = 0;
}
if(millis() > smiletimer) smiling = false;
if(smiling){
drawImage(mouth_smile);
} else if(vol < 200){
drawImage(mouth_0);
} else if(vol < 250){
drawImage(mouth_1);
} else if(vol < 350){
drawImage(mouth_2);
} else if(vol < 450){
drawImage(mouth_3);
} else {
drawImage(mouth_4);
}
}