Rushabh Jain
Published © GPL3+

Clap-O-Switch

Switching appliances using two hands (clapping).

IntermediateFull instructions provided6 hours726

Things used in this project

Hardware components

ATtiny85
Microchip ATtiny85
×1
Gravity: Analog Sound Sensor For Arduino
DFRobot Gravity: Analog Sound Sensor For Arduino
×1
Relay (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Soldering Gun Kit, Instant Heat
Soldering Gun Kit, Instant Heat
Drill / Driver, Cordless
Drill / Driver, Cordless

Story

Read more

Schematics

Clap-O-Switch

Code

Clap-O-Switch

Arduino
#include <ResponsiveAnalogRead.h>

#define sound_pin   5
#define relay_pin   3

ResponsiveAnalogRead sound(sound_pin, true);

int soundData = 0;
int threshold = 1000;
int lastSoundValue = 0 ;
int soundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;
int relayStatus = HIGH;
int data = 0;


void setup() {
  pinMode(relay_pin,OUTPUT);
  digitalWrite(relay_pin,HIGH);
  //pinMode(sound_pin,INPUT);
  Serial.begin(9600);

}

void loop() {
  sound.update();
  soundValue = sound.getValue();
  currentNoiseTime = millis();
  Serial.println(soundValue);

  if (soundValue > threshold) { // if there is currently a noise

    if (
      (currentNoiseTime > lastNoiseTime + 250) && // to debounce a sound occurring in more than a loop cycle as a single noise
      (lastSoundValue < 400) &&  // if it was silent before
      (currentNoiseTime < lastNoiseTime + 750) && // if current clap is less than 0.75 seconds after the first clap
      (currentNoiseTime > lastLightChange + 1000) // to avoid taking a third clap as part of a pattern
    ) {

      relayStatus = !relayStatus;
      digitalWrite(relay_pin, relayStatus);
      delay(300);
      lastLightChange = currentNoiseTime;
     }

     lastNoiseTime = currentNoiseTime;
  }

  lastSoundValue = sound.getValue();

}

Credits

Rushabh Jain

Rushabh Jain

10 projects • 43 followers
An engineer who loves to solve problems with technology. Expertise in Embedded firmware and software.

Comments