Lesson 15: Sound Sensor Module (analog and digital output)
Lesson 15: Sound Sensor Module (analog and digital output)
Please select other codes for this lecture from the links below.
This is the Arduino code Control AC Bulb with hand Clap or Voice (Advanced: Analog) This video shows you how to turn an AC light ON with clap or your voice and Turn it off with another clap or shout. This code is Advanced version where Analog output A0 of the module is used. Please see other version where DO pin is used to control light.
/*
* Robojax Arduino Step By Step Course
* Lesson 15 Sound Sensor via Analog pin
* This is the Arduino code Control AC Bulb with hand Clap or Voice (Advanced: Analog)
This video shows you how to turn an AC light ON with clap or your voice and Turn it off with another clap
or shout. This code is Advanced version where Analog output A0 of the module is used.
Please see other version where DO pin is used to control light.
Please watch video instruction here https://youtu.be/INq8A3dLNzM
This code is available at http://robojax.com/course1/?vid=lecture11
with over 100 lectures Free On YouTube Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339
// written by Ahmad Shamshiri for Robojax Robojax.com on
Mar 23, 2019 in Ajax, Ontario, Canada
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const int soundInPin = A0;// define input pin for sound
const int relayPin = 8;// Connected to relay (LED)
int extra5V = 12;// define a pin for extra extra 5V
int soundValue = 525;// The sound level to trigger ON/OFF
///////////// do NOT change the lines bellow
int val = 0; // light value from pin 2
int lightON = 0;//light status
int heard = 0;//sound heard status
int dVal =0;// digital value for "val"
void setup() {
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
Serial.begin(9600);
pinMode(relayPin, OUTPUT);// set Pin 10 as output
pinMode(extra5V,OUTPUT);// set extra5V as output
digitalWrite(extra5V,HIGH);// turn the extra5V pin HIGH to get 5V
}
void loop() {
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
val = analogRead(soundInPin);// read A0 pin value
if(val > soundValue && lightON == LOW){
heard = 1-heard;// toggle the value of "heard" from HIGH to LOW or from LOW to HIGH
dVal =HIGH;
delay(100);
}else{
dVal =LOW;
}
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
lightON = dVal;
if(heard == HIGH){
Serial.print(val);// print the A0 value
Serial.println(" Light ON");
digitalWrite(relayPin, LOW); // Turn the light ON
}else{
Serial.print(val);// print the A0 value
Serial.println(" Light OFF");
digitalWrite(relayPin, HIGH);// Turn the light OFF
}
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
delay(100);
}