工具:
硬件:arduino开发板,麦克风模块和若干杜邦线
声音拾取模块:
接线:
麦克风模块的A0引脚接arduino开发板的A0引脚
步骤:
一、打开arduino软件
二、使用以下程序:
<实验1> 判断模块的好坏
volatile int item;
void setup(){
item = 0;
Serial.begin(9600);
pinMode(11, OUTPUT);
}
void loop(){
item = analogRead(A0);
Serial.println(item);
if (item >= 70) {
digitalWrite(11,HIGH);
} else {
digitalWrite(11,LOW);
}
}
此时串口监视器会显示当前声音的模拟量,可根据模拟量的阈值范围添加响应的内容
程序:
现象:加入了热释电红外检测,红外和声音检测到人亮不同颜色的灯,同时OLED屏显示
/************************************
* 项目:MIC检测+OLED
* 现象:检测到人分别亮不同的颜色,并在LED显示状态
***********************************/
#include "U8glib.h"
#include "FastLED.h" //程序需要使用FastLED库
#define button1 13 //触摸按键接口定义
#define NUM_LEDS 8 // LED灯珠数量
#define LED_DT 2 // Arduino输出控制信号引脚
#define DATA_PIN 9 // Arduino输出控制信号引脚
#define LED_TYPE WS2812 // LED灯带型号
#define COLOR_ORDER GRB // RGB灯珠中红色、绿色、蓝色LED的排列顺序
const int ledPin = 13;
const int inputPin = A0;
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
CRGB leds[NUM_LEDS]; // 建立光带leds
uint8_t max_bright = 128; // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
uint8_t openState = 0;
uint8_t c = 0;
uint8_t cishu = 0;
uint8_t val, flag=0;
int output;
unsigned long previousBlinkTime;
int blinkInterval = 1500; //LED闪烁时间间隔
bool toggle;
static unsigned char deng[] U8G_PROGMEM = {0x00,0x00,0x7E,0x3E,0x40,0x22,0x40,0x12,0x42,0x12,0x24,0x0A,0x28,0x12,0x10,0x12,
0x10,0x22,0x28,0x22,0x28,0x22,0x44,0x16,0x42,0x0A,0x01,0x02,0x00,0x02,0x00,0x02,/*"邓",0*/};
static unsigned char zhao[] U8G_PROGMEM = {0x00,0x00,0xBE,0x3F,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x29,0xBE,0x10,0x22,0x3F,
0x22,0x21,0x22,0x21,0x22,0x21,0x3E,0x3F,0x00,0x00,0x12,0x11,0x22,0x22,0x21,0x22,/*"照",1*/};
static unsigned char quan[] U8G_PROGMEM = {0x08,0x00,0xC8,0x3F,0x88,0x20,0x88,0x20,0xBF,0x20,0x08,0x11,0x0C,0x11,0x1C,0x11,
0x2A,0x0A,0x2A,0x0A,0x09,0x04,0x08,0x04,0x08,0x0A,0x08,0x11,0x88,0x20,0x68,0x40,/*"权",2*/};
void button_State()
{
//A
if(digitalRead(button1)){ //检测按键是否按下
delay(20); //延时去抖动
if(digitalRead(button1)){//判断长短键
openState++;
}
while(digitalRead(button1));
}
}
void millisBlink(unsigned long currentTime) {
//检查是否到达时间间隔
if (currentTime - previousBlinkTime >= blinkInterval) { //如果时间间隔达到了
toggle = (toggle == 1) ? 0 : 1;
digitalWrite(LED_BUILTIN, toggle); //执行闪烁LED操作
flag = 2;
previousBlinkTime = currentTime; // 将检查时间复位
//Serial.print(F("toggle = "));Serial.println(toggle);
} else if (currentTime - previousBlinkTime <= 0) { // 如果millis时间溢出
previousBlinkTime = currentTime;
}
}
void draw0(void) {
u8g.drawXBMP( 0, 16, 16, 16, deng);
u8g.drawXBMP( 16, 16, 16, 16, zhao);
u8g.drawXBMP( 32, 16, 16, 16, quan);
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 0, 45, "Value:");
u8g.setPrintPos(48,45);
u8g.print(val);
if(flag == 2){
//u8g.drawStr( 0, 45, "bey");
u8g.drawStr( 50, 29, "Bluck");
}
else{
if(cishu == 1)
u8g.drawStr( 50, 29, "Blue");
else if(cishu == 2)
u8g.drawStr( 50, 29, "Yellow");
else if(cishu == 0)
u8g.drawStr( 50, 29, "Red");
else if(cishu == 3)
u8g.drawStr( 50, 29, "White");
else if(cishu == 4)
u8g.drawStr( 50, 29, "Tan");
}
// else
// u8g.drawStr( 50, 29, "Bluck");
// u8g.drawStr( 0, 12, "190306238");
// u8g.setPrintPos(42,45);
// u8g.print(c++);
}
void draw1(void) {
// u8g.drawXBMP( 0, 16, 16, 16, deng);
// u8g.drawXBMP( 16, 16, 16, 16, zhao);
// u8g.drawXBMP( 32, 16, 16, 16, quan);
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 0, 45, "Numb:");
u8g.setPrintPos(42,45);
u8g.print(c++);
}
void mode_0()
{
u8g.firstPage();
do {
draw0();
} while( u8g.nextPage() );
}
void mode_1()
{
u8g.firstPage();
do {
draw1();
} while( u8g.nextPage() );
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//pinMode(button1, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // 初始化光带
FastLED.setBrightness(max_bright); // 设置光带亮度
//将引脚13设置为输出模式
// pinMode(ledPin, OUTPUT);
//将引脚2设置为输入上拉模式
//引脚2与外部按钮连接
pinMode(inputPin, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis(); // 获取当前时间
millisBlink(currentMillis);
output = analogRead(inputPin);
val = map(output, 0, 1023, 0, 255);
Serial.print("sensor = ");
Serial.print(output);
Serial.print("\t output = ");
Serial.println(val);
if ((val!=131) && (flag==1))
{
flag==0;
cishu++;
if(cishu == 5)
cishu = 0;
if(cishu == 0){
leds[0] = CRGB::Red; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show();
delay(100);
leds[1] = CRGB::Red;
FastLED.show();
}
if(cishu == 1){
leds[0] = CRGB::Blue; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show();
delay(100);
leds[1] = CRGB::Blue;
FastLED.show();
}
if(cishu == 2){
leds[0] = CRGB::Yellow; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show();
delay(100);
leds[1] = CRGB::Yellow;
FastLED.show();
}
if(cishu == 3){
leds[0] = CRGB::White; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show();
delay(100);
leds[1] = CRGB::White;
FastLED.show();
}
if(cishu == 4){
leds[0] = CRGB::Tan; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show();
delay(100);
leds[1] = CRGB::Tan;
FastLED.show();
}
// FastLED.show(); // 更新LED色彩
}
else if(flag==2)
{
flag = 1;
leds[0] = CRGB::Black; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show(); // 更新LED色彩
delay(150);
leds[1] = CRGB::Black;
FastLED.show();
}
//Serial.println(val);
//delay(100);
// int sensorValue = analogRead(A0);
// // print out the value you read:
// Serial.println(sensorValue);
//button_State();
switch (openState)
{
case 0:
// openState = 0;
mode_0(); //
break;
case 1:
mode_1(); //
break;
case 2:
// mode_2(); //
break;
case 3:
// mode_3(); //
break;
default:
break;
}
if(openState == 3) openState = 0;
// Serial.println(openState);
}