搜索代码

Lesson 15-1: Sound Sensor via Arduino Digital Output Pin

Lesson 15-1: Sound Sensor via Arduino Digital Output Pin

This is the Arduino code for Sound module for Arduino (basic) 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 basic version where digital output of the module is used. Please see other version where actual Analog sound values is used to control light.
549-Lesson 15: Sound Sensor Module (analog and digital outputs)
语言: C++
/*
 * Robojax Arduino Step-by-Step Course
 * Lesson 15: Sound Sensor via digital pin
 * This is the Arduino code for a sound module for Arduino (basic).
This video shows you how to turn an AC light ON 
with a clap or your voice and turn it off with another clap
or shout. This code is a basic version where 
the digital output of the module is used.
Please see another version where actual analog sound values are 
used to control the light.

  Please watch video instructions 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  
 // Written on March 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 downloaded 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/>.
 */
 
int soundInPin = 2;// connect output to Sound module DO
int relayPin = 8;// Connected to relay or buzzer (or LED)
int extra5V = 12;// define a pin for extra extra 5V

// do not change values bellow
int val = 0; // sound value from pin 2
int relayON = 0;//light status
int heard = 0;//sound heard status


void setup() {

//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338   
  Serial.begin(9600);
  pinMode(soundInPin, INPUT_PULLUP); 
  pinMode(relayPin, 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 = digitalRead(soundInPin);// read the sound pin

  if(val == HIGH && relayON == LOW){

    heard = 1-heard;// toggle the value of "heard" from HIGH to LOW or from LOW to HIGH
    delay(100);
  }    
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  relayON = val;// save the value of pin 2

      if(heard == HIGH){
        Serial.println("Light ON");
        digitalWrite(relayPin, LOW); // turn relay ON
       
      }else{
        Serial.println("Light OFF");
        digitalWrite(relayPin, HIGH);// turn relay OFF
   
      }     


//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  delay(100);
}

资源与参考

尚无可用资源。

文件📁

没有可用的文件。