Skip to content

Instantly share code, notes, and snippets.

@elktros
Created July 16, 2018 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/e1d175ee942f722f897eb22cf63a145c to your computer and use it in GitHub Desktop.
Save elktros/e1d175ee942f722f897eb22cf63a145c to your computer and use it in GitHub Desktop.
Code for controlling a Relay using Sound Sensor and Arduino.
const int relayPin = 11;
const int soundPin = 7;
int soundVal = 0;
int relayToggle = LOW;
int previousState = HIGH;
void setup ()
{
pinMode (relayPin, OUTPUT);
pinMode (soundPin, INPUT);
}
void loop ()
{
soundVal = digitalRead(soundPin);
if (soundVal == LOW && previousState==HIGH)
{
relayToggle=!relayToggle;
digitalWrite (relayPin, relayToggle);
}
previousState=soundVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment