Components and supplies
1
330Ω resistor
1
24 Gauge wire
1
Microphone MAX4466
1
KCD1 Switch
1
Arduino Nano R3
1
Led matrix 8x8 WS2812B
Tools and machines
1
5V power bank
1
Heat shrink
Project description
Code
mask.ino
arduino
1#include <Adafruit_NeoPixel.h> 2#include <Adafruit_NeoMatrix.h> 3#include <gamma.h> 4 5#define lengthof(A) ((sizeof((A))/sizeof((A)[0]))) 6#define button 12 7int oldstate=HIGH; 8 9 10const PROGMEM uint8_t mouth_0[8][8] = { 11 {0,0,0,0,0,0,0,0}, 12 {0,0,0,0,0,0,0,0}, 13 {0,0,6,6,6,6,0,0}, 14 {0,6,6,6,6,6,6,0}, 15 {6,6,0,0,0,0,6,6}, 16 {6,0,0,0,0,0,0,6}, 17 {0,0,0,0,0,0,0,0}, 18 {0,0,0,0,0,0,0,0} 19}; 20 21const PROGMEM uint8_t mouth_4[8][8] = { 22 {0,0,7,7,7,7,0,0}, 23 {0,7,0,0,0,0,7,0}, 24 {7,0,0,0,0,0,0,7}, 25 {7,0,0,0,0,0,0,7}, 26 {7,0,0,0,0,0,0,7}, 27 {7,0,0,0,0,0,0,7}, 28 {0,7,0,0,0,0,7,0}, 29 {0,0,7,7,7,7,0,0} 30}; 31 32const PROGMEM uint8_t mouth_3[8][8] = { 33 {0,0,0,0,0,0,0,0}, 34 {0,0,7,7,7,7,0,0}, 35 {0,7,0,0,0,0,7,0}, 36 {7,0,0,0,0,0,0,7}, 37 {7,0,0,0,0,0,0,7}, 38 {0,7,0,0,0,0,7,0}, 39 {0,0,7,7,7,7,0,0}, 40 {0,0,0,0,0,0,0,0} 41}; 42 43const PROGMEM uint8_t mouth_2[8][8] = { 44 {0,0,0,0,0,0,0,0}, 45 {0,0,0,0,0,0,0,0}, 46 {0,7,7,7,7,7,7,0}, 47 {7,0,0,0,0,0,0,7}, 48 {7,0,0,0,0,0,0,7}, 49 {0,7,7,7,7,7,7,0}, 50 {0,0,0,0,0,0,0,0}, 51 {0,0,0,0,0,0,0,0} 52}; 53 54const PROGMEM uint8_t mouth_1[8][8] = { 55 {0,0,0,0,0,0,0,0}, 56 {0,0,0,0,0,0,0,0}, 57 {0,0,0,7,7,0,0,0}, 58 {7,7,7,0,0,7,7,7}, 59 {7,7,7,0,0,7,7,7}, 60 {0,0,0,7,7,0,0,0}, 61 {0,0,0,0,0,0,0,0}, 62 {0,0,0,0,0,0,0,0} 63}; 64 65const PROGMEM uint8_t mouth_smile[8][8] = { 66 {0,0,0,0,0,0,0,0}, 67 {0,0,0,0,0,0,0,0}, 68 {2,0,0,0,0,0,0,2}, 69 {2,2,0,0,0,0,2,2}, 70 {0,2,2,2,2,2,2,0}, 71 {0,0,2,2,2,2,0,0}, 72 {0,0,0,0,0,0,0,0}, 73 {0,0,0,0,0,0,0,0} 74}; 75 76const PROGMEM uint8_t off_set[8][8] = { 77 {0,0,0,0,0,0,0,0}, 78 {0,0,0,0,0,0,0,0}, 79 {0,0,0,0,0,0,0,0}, 80 {0,0,0,0,0,0,0,0}, 81 {0,0,0,0,0,0,0,0}, 82 {0,0,0,0,0,0,0,0}, 83 {0,0,0,0,0,0,0,0}, 84 {0,0,0,0,0,0,0,0} 85}; 86 87 88 89uint16_t palette[8] = {}; 90Adafruit_NeoMatrix matrix1 = Adafruit_NeoMatrix(8, 8, 6, 91 NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + 92 NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG, 93 NEO_GRB + NEO_KHZ800); 94Adafruit_NeoMatrix matrix2 = Adafruit_NeoMatrix(8, 8, 7, 95 NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + 96 NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG, 97 NEO_GRB + NEO_KHZ800); 98 99Adafruit_NeoMatrix matrix3 = Adafruit_NeoMatrix(8, 8, 2, 100 NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + 101 NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG, 102 NEO_GRB + NEO_KHZ800); 103Adafruit_NeoMatrix matrix4 = Adafruit_NeoMatrix(8, 8, 0, 104 NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + 105 NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG, 106 NEO_GRB + NEO_KHZ800); 107 108 109void drawImage(short image_addr){ 110 for(int x = 0; x<8; x++){ 111 for(int y = 0; y<8; y++){ 112 uint8_t index = pgm_read_byte(image_addr+x+y*8); 113 matrix1.drawPixel(x, y, palette[index]); 114 matrix2.drawPixel(x, y, palette[index]); 115 matrix3.drawPixel(x, y, palette[index]); 116 matrix4.drawPixel(x, y, palette[index]); 117 } 118 } 119 120 matrix1.show(); 121 matrix2.show(); 122 matrix3.show(); 123 matrix4.show(); 124} 125 126 127int pop_detection = 0; 128bool smiling = false; 129unsigned long smiletimer = 0; 130unsigned long last_face = 0; 131float vol = 0; 132const uint16_t samples = 128; 133 134void setup() { 135 136 Serial.begin(9600); 137 matrix1.begin(); 138 matrix2.begin(); 139 matrix3.begin(); 140 matrix4.begin(); 141 142 palette[0] = matrix4.Color(0,0,0); //No color 143 // palette[1] = matrix.Color(0,0,255); //blue 144 palette[2] = matrix3.Color(255,0,255); //purple 145 // palette[3] = matrix.Color(0,255,0); //green 146 // palette[4] = matrix.Color(255,255,255); //white 147 // palette[5] = matrix.Color(255,0,0); //red 148 palette[6] = matrix1.Color(0,255,255); //yellow 149 palette[7] = matrix2.Color(255,255,0); //cyan 150 151 152 pinMode(button,INPUT_PULLUP); 153} 154 155 156void loop() { 157 158 int newstate=digitalRead(button); 159 if(newstate == LOW){ 160 161 float nvol = 0; 162 int previous_peak = -1; 163 164 for (int i = 0; i<samples; i++){ 165 auto analog = analogRead(A7); 166 auto micline = abs(analog - 512); 167 168 nvol = max(micline, nvol); 169 } 170 171 vol = (nvol + 1.0*vol)/2.0; 172 173 if(nvol > 200){ 174 pop_detection += 1; 175 if(pop_detection > 5) { 176 smiling = false; 177 last_face = millis(); 178 } 179 } else { 180 if(pop_detection > 0 && pop_detection <= 5) { 181 if(millis() > last_face + 500){ 182 smiling = true; 183 smiletimer = millis() + 2000; 184 } 185 } 186 pop_detection = 0; 187 } 188 if(millis() > smiletimer) 189 smiling = false; 190 191 192 if(smiling){ 193 drawImage(mouth_smile); 194 } else if(vol < 200){ 195 drawImage(mouth_0); 196 } else if(vol < 250 && vol > 200){ 197 drawImage(mouth_1); 198 } else if(vol < 350 && vol > 250){ 199 drawImage(mouth_2); 200 } else if(vol < 450 && vol > 350){ 201 drawImage(mouth_3); 202 } else { 203 drawImage(mouth_4); 204 } 205 206 } 207 else 208 { 209 drawImage(off_set); 210 } 211 212} 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
Downloadable files
LED Face Mask
LED Face Mask

LED Face Mask
LED Face Mask

Comments
Only logged in users can leave comments