arduino とLEDmatrix で 電光マスク を作ってみた

工作

ネットで見つけたこの記事This Arduino-powered LED matrix mask responds to your voiceが面白そうだったのでマネて 電光マスク を作ってみましたw

必要なものは、arduinoとWS2812B neopixel PCB 8×8とマイクアンプキットです。マスク部分は段ボールで型紙を作って、neopixel PCB 8×8とマイクアンプキットを貼り付けました。

マスク

arduino nanoが無かったのでarduino unoで代用してるため配線はむきだしです。でもなかなか反応も良く、編集長に歌を歌ってもらいました。

元の記事から本ページに飛ぶリンクが切れているので、忘備録的にスケッチを載せておきます。

//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(A5);
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);
}
Serial.println(vol);
}

いいね! (1 いいね!)

読み込み中...

コメント

タイトルとURLをコピーしました