Coding your clap switch sketch
In this section, we will develop the program to identify a clap sound from a microphone. This sound will turn an LED on and off. Let's get started:
- As a first step, we need to define which pins of the Blue Pill card pins will be used for input and output. Then, we need to assign the sound threshold level for the microphone to detect the sound; this value is in the range
0
-1023
. We are using a value of300
, so the sound captured by the microphone is loud enough to identify a clap and not any background noise (we will show how to select an appropriate threshold in the Improving the project performance section).As can be seen in the following code snippet, the analog reading pin will be
0
(labeled A0 on the Blue Pill), and the digital output pin will bePC13
(labeled C13 on the Blue Pill):const int MicAnalogPin = 0; const int LedDigitalPin = PC13; const int ClapThreshold = 300;
Constant value variables are being used with the
const
keyword...