Project showcase
Clap-Controlled Lights That You Can Actually Use

Clap-Controlled Lights That You Can Actually Use

Clap twice to turn on and off a bright LED.

  • 499 views
  • 0 comments
  • 6 respects

Components and supplies

Necessary tools and machines

About this project

During the night, no one wants to get up and walk all the way to your light switch just to be hit with a sudden extreme amount of light. Solution! A lighting system controlled by two claps. Just clap from wherever you are to turn on LEDs bright enough to light an entire room just the right amount. It looks for a clap and repeats the program in loop(). I used four super bright LEDs, an Arduino Micro (any will work), and a sound sensor. Just clap twice to turn on and off.

Bright LEDs: https://www.amazon.com/gp/product/B07MQJKY73/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

Arduino Micro:

https://store.arduino.cc/usa/arduino-micro

Sound Sensor:

https://www.sparkfun.com/products/12642

Code

ClapLightC/C++
/*
* GND → GND
 * VCC → 5V
 * Gate → Pin 4
 * Envelope → A0
 * 
 * LED → 7
 * LED → 9
 */
int LED0 = 7;
int firstClap;
int LED1 = 9;

#define PIN_GATE_IN 4
#define IRQ_GATE_IN  0
#define PIN_LED_OUT 13
#define PIN_ANALOG_IN A0

// if true, the LED is on
// if false, then LED is off
bool state = false;

// when the last clapped happened
unsigned long lastClapTime;

// soundISR()
// This function is installed as an interrupt service routine for the pin
// change interrupt.  When digital input 2 changes state, this routine
// is called.
// It queries the state of that pin, and sets the onboard LED to reflect that
// pin's state.
void soundISR()
{
  int pin_val;

  pin_val = digitalRead(PIN_GATE_IN);
  digitalWrite(PIN_LED_OUT, pin_val);
}

void setup()
{
  Serial.begin(9600);

  //  Configure LED pin as output
  pinMode(PIN_LED_OUT, OUTPUT);

  // configure input to interrupt
  pinMode(PIN_GATE_IN, INPUT);
  attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE);


  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);

  // when we start, LED is off
  state = false;
  lastClapTime = 0;

  digitalWrite(LED0, LOW);
  digitalWrite(LED1, LOW);
}

//
// This function returns true if person has clapped
// else it returns false.
//
bool isClapped(int sound) {
  if (sound > 50) {
    return true;
  }
  return false;
}

void toggleState() {
  if (state == true) {
    state = false;
    digitalWrite(LED0, LOW);
    digitalWrite(LED1, LOW);
  } else {
    state = true;
    digitalWrite(LED0, HIGH);
    digitalWrite(LED1, HIGH);
  }
}


void loop() {  
  int sound = analogRead(PIN_ANALOG_IN);

  unsigned long now = millis();
  if (isClapped(sound)) {
    delay(200);
    
    // check if the two previos claps were within 1 second of one another 
    if (lastClapTime + 1000 > now) {
      Serial.println("Now: " + String(now) + " LastClappedTime: " +  String(lastClapTime) + " Toggling");
      toggleState();
      lastClapTime = 0;
      delay(1000);
    } else {
      Serial.println("Now: " + String(now) + " LastClappedTime: " +  String(lastClapTime));
      lastClapTime = now;
    }
  }
}

Schematics

claplight_XEJHAtkVr1.png
Claplight xejhatkvr1

Comments

Similar projects you might like

Arduino Controlled Artificial Candle Lights

Project tutorial by Mattias Larsson

  • 22,759 views
  • 14 comments
  • 47 respects

Huge LED Christmas Lights

Project showcase by Savva Osipov

  • 14,924 views
  • 2 comments
  • 39 respects

Arduino Bike Blink Lights

Project showcase by Sam

  • 6,553 views
  • 3 comments
  • 15 respects

IoT Controlled Planter Lights

Project showcase by John Weers

  • 2,146 views
  • 0 comments
  • 12 respects

Automated Staircase RGB LED Lights

Project tutorial by Team NotEnoughTech.com

  • 42,564 views
  • 20 comments
  • 141 respects

How to Configure NeoPixels Using Vixen Lights and Arduino

Project tutorial by Victor Aguilar

  • 37,905 views
  • 39 comments
  • 67 respects
Add projectSign up / Login