Coding a clap switch with two clapping sounds
In this section, we will modify our program to identify two clapping sounds from the microphone. This will allow us to be more precise before activating the remote control:
- Once we define the constants, we define two variables: the integer type, to count the number of claps, and the Boolean type, to know the status of the LED (on or off). For a better reading, we have highlighted the changes to the original variables' declaration of the sketch:
const int MicAnalogPin = 0; const int LedDigitalPin = PC13; const int ClapThreshold = 300; int ClapNumber = 0; bool LedState = false;
As you can see, the clap count is set to
0
, and the LED status tofalse
(off), which means thattrue
will be on. - We will leave the
setup()
section unchanged and continue to theloop()
section, where we have the most important logic changes. We will modify the instructions that are within the conditional sentence that verifies whether the sound registered...