-
Notifications
You must be signed in to change notification settings - Fork 117
/
AnalogMicAudio_Alarm.ino
144 lines (131 loc) · 3.58 KB
/
AnalogMicAudio_Alarm.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
/**
@file AnalogMicAudio_Alarm.ino
@author rakwireless.com
@brief The analog microphone detects the noise threshold .
When the ambient noise is greater than the set threshold, a warning will be generated.
And the LED of WisBase will lights 1 seconds.
@note This example need use the RAK18040 analog microphone module.
@version 0.1
@date 2022-06-10
@copyright Copyright (c) 2022
*/
#include <Arduino.h>
#ifdef NRF52_SERIES
#include <Adafruit_TinyUSB.h>
#endif
#include "audio.h" // Click here to install the library: http://librarymanager/All#RAKwireless-Audio
NAU85L40B MIC(0x1D);
#define I2S_DATA_BLOCK_WORDS 512
i2s_channels_t channels = Stereo ; //Right、 Left;//
int frequency = 16000;
int sampleBit = 16;
uint32_t readbuff[I2S_DATA_BLOCK_WORDS] = {0};
int16_t leftChannel[512] = {0};
int16_t rightChannel[512] = {0};
//Alarm threshold
int audio_threshold = 800; //You can modify this value to your desired noise trigger threshold.
int alarm_count = 0;
volatile uint8_t rx_flag = 1;
int abs_int(short data);
void i2s_config();
void rx_irq();
void setup()
{
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
delay(500);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
MIC.begin();
MIC.config(frequency, (MIC_CHANNEL1 | MIC_CHANNEL2), I2S_SAMPLE_16BIT); //
i2s_config();
}
void loop()
{
// wait for samples to be read
if (rx_flag == 1)
{
rx_flag = 0;
int sumLeft = 0;
int sumRight = 0;
I2S.read(&readbuff, sizeof(readbuff));
for (int i = 0; i < I2S_DATA_BLOCK_WORDS; i++)
{
if(channels == Stereo)
{
uint32_t const * p_word = &readbuff[i];
leftChannel[i] = ((uint16_t const *)p_word)[0];
rightChannel[i] = ((uint16_t const *)p_word)[1];
}
else
{
uint32_t const * p_word = &readbuff[i];
int16_t temp = ((uint8_t const *)p_word)[3];
temp = (int16_t)((temp<<8)|((uint8_t const *)p_word)[1]);
leftChannel[i] = temp;
temp = 0;
temp = ((uint8_t const *)p_word)[2];
temp = (int16_t)((temp<<8)|((uint8_t const *)p_word)[0]);
rightChannel[i] = temp;
}
sumLeft = sumLeft+abs(leftChannel[i]);
sumRight = sumRight+abs(rightChannel[i]);
// Serial.print("L:");
// Serial.print(leftChannel[i]);
// Serial.print(" R:");
// Serial.println(rightChannel[i]);
}
int averLeft = sumLeft / I2S_DATA_BLOCK_WORDS;
int averRight = sumRight / I2S_DATA_BLOCK_WORDS;
if ((averLeft > audio_threshold)||(averRight > audio_threshold))
{
alarm_count++;
}
}
if (alarm_count > 2)
{
alarm_count = 0;
Serial.println("Alarm");
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_GREEN, HIGH);
delay(1000);
/*You can add your alarm processing tasks here*/
}
else
{
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
}
}
int abs_int(short data)
{
if (data > 0) return data;
else return (0 - data);
}
void rx_irq()
{
rx_flag = 1;
// I2S.read(&readbuff,sizeof(readbuff));
}
void i2s_config()
{
I2S.RxIRQCallBack(rx_irq);
I2S.begin(channels, frequency, sampleBit);
I2S.start();
}