Arduino - 聲音控制矩陣型LED (MIC + Counter + Matrix LED)


目的:


以聲音作為輸入,將次數顯示於矩陣型LED,0~9間做循環,並有開關作為RESET用。

成果:


Arduino Code:

#include < SPI.h >
int micPin = A0; //麥克風輸入
int mictemp = 0; //麥克風獨到的音量
int temp = 0;
byte symbol[10][8] = {
    {
        0x3E,
        0x7F,
        0x71,
        0x59,
        0x4D,
        0x7F,
        0x3E,
        0x00
    }, // '0'
    {
        0x40,
        0x42,
        0x7F,
        0x7F,
        0x40,
        0x40,
        0x00,
        0x00
    }, // '1'
    {
        0x62,
        0x73,
        0x59,
        0x49,
        0x6F,
        0x66,
        0x00,
        0x00
    }, // '2'
    {
        0x22,
        0x63,
        0x49,
        0x49,
        0x7F,
        0x36,
        0x00,
        0x00
    }, // '3'
    {
        0x18,
        0x1C,
        0x16,
        0x53,
        0x7F,
        0x7F,
        0x50,
        0x00
    }, // '4'
    {
        0x27,
        0x67,
        0x45,
        0x45,
        0x7D,
        0x39,
        0x00,
        0x00
    }, // '5'
    {
        0x3C,
        0x7E,
        0x4B,
        0x49,
        0x79,
        0x30,
        0x00,
        0x00
    }, // '6'
    {
        0x03,
        0x03,
        0x71,
        0x79,
        0x0F,
        0x07,
        0x00,
        0x00
    }, // '7'
    {
        0x36,
        0x7F,
        0x49,
        0x49,
        0x7F,
        0x36,
        0x00,
        0x00
    }, // '8'
    {
        0x06,
        0x4F,
        0x49,
        0x69,
        0x3F,
        0x1E,
        0x00,
        0x00
    } // '9'
}; //設定8x8LED矩陣顯示器
const byte NOOP = 0x0;
const byte DECODEMODE = 0x9;
const byte INTENSITY = 0xA;
const byte SCANLIMIT = 0xB;
const byte SHUTDOWN = 0xc;
const byte DISPLAYTEST = 0xF;
void max7219(byte reg, byte data) {
    digitalWrite(SS, LOW);
    SPI.transfer(reg);
    SPI.transfer(data);
    digitalWrite(SS, HIGH);
}

void setup() {
    max7219(SCANLIMIT, 7);
    max7219(DECODEMODE, 0);
    max7219(INTENSITY, 8);
    max7219(DISPLAYTEST, 0);
    max7219(SHUTDOWN, 1);
    pinMode(SS, OUTPUT);
    digitalWrite(SS, HIGH);
    SPI.begin();

    for (byte i = 0; i < 8; i++) {
        max7219(i + 1, 0);
    }
    temp = 0;
    for (byte i = 0; i < 8; i++) {
        max7219(i + 1, symbol[temp][i]);
    }
}

void loop() {
    mictemp = analogRead(micPin);
    if (mictemp >= 450) 當收到大於450的輸入 {
        for (byte i = 0; i < 8; i++) {
            max7219(i + 1, symbol[temp][i]);
        }
        temp++; //收到訊號+1
        if (temp == 10) {
            temp = 0; //跑到10歸零
        }
        delay(500);
    }
}

留言