The sound-activated device sketch

The C sketch is designed to switch ON the light as soon as there is a sound detected by the sound sensor. If another subsequent sound is detected, the light will be switched OFF. This sketch can be used with both parts in this chapter.

By now, you should be able to understand the following code on your own. However, we will take a quick look at the various portions of the code shortly.

The sketch is available for download from the online location mentioned in Chapter 1, Boot Camp of this book.

Here is the sketch for a sound operated relay switch:

//**********************************************************/ 
// Step-1: CONFIGURE VARIABLES
//**********************************************************/
int _pinD10 = 10; // pin 10 to receive input
// from sensor
int _pinD7 = 7; // pin 7 to send signal to
// relay
int _soundLevel = HIGH; // variable for storing
// output of sensor
boolean _bRelayState = false; // start by assuming OFF
// state
unsigned long _lastSoundEventTime; // variable to store the
// time
// of the last sound event
int _gapBetween2Sounds = 1000; // gap between 2 sound
// events

//**********************************************************/
// Step-2: INITIALIZE I/O PARAMETERS
//**********************************************************/
void setup ()
{
// configure the pins to be used for I/O
pinMode (_pinD10, INPUT) ; // input from the sound sensor
pinMode(_pinD7, OUTPUT); // output to relay
}

//**********************************************************/
// Step-3: MAIN PROGRAM
//**********************************************************/
void loop ()
{
_soundLevel = digitalRead (_pinD10) ; // read sound level
if (_soundLevel == LOW) // if a sound occurs
{
if (_bRelayState == false) // if relay is in
{ // OPEN state
//if event is after at least 1 second of the previous
// event
if( (millis()-_lastSoundEventTime)> _gapBetween2Sounds)
{
_lastSoundEventTime = millis(); // record event time
_bRelayState = true;
//Switch ON the Relay
digitalWrite(_pinD7, 0);
}
}
if (_bRelayState == true) //if relay is already ON
{
//if event is after at least 1 second of the previous
// event
if( (millis()-_lastSoundEventTime)> _gapBetween2Sounds)
{
_lastSoundEventTime = millis(); // record event time
_bRelayState = false;
//Switch OFF the Relay
digitalWrite(_pinD7, 1);
}
}
}
}

The first two steps of the sketch are self-explanatory in that they are used to setup initialization parameters and variables and specify the input/output modes of the pins used by the embedded code. The main program keeps processing any sound events that occur. Upon detecting the first sound event, the light bulb is switched ON.

For the sound detector, it is very important to know how to handle incoming signals while working with a device that sends a continuous stream of signals. Otherwise too many sound events will be captured and processed by the program and the light bulb will start getting switched ON and OFF too quickly in succession. To avoid this situation, the following statement has been used to track the last time the sound started:

if ( (millis() - _lastSoundEventTime) > _gapBetween2Sounds) 

The preceding code is written in such a way that there will be a mandatory gap of 1 second before the program accepts a sound event for processing. So, in essence, if someone claps their hands twice within a 1 second time window, then the second clap will be ignored. There should be a gap of at least one second between two subsequent sound events. Try to change the value of the variable gapBetween2Sounds to a higher value and see how the sketch behaves.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.9