a886a
Published © GPL3+

Double clap LED Lamp

Turn on an LED strip with 2 claps using a sound sensor

IntermediateFull instructions provided2,725
Double clap LED Lamp

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
KY-037 Sound Sensor
Comes in most Arduino starter kits
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Capacitor 1000 µF
Capacitor 1000 µF
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
LED Strip, NeoPixel Digital RGB
LED Strip, NeoPixel Digital RGB
I used the 30 LED one
×1
AC/DC Power Supply, External Plug In
AC/DC Power Supply, External Plug In
5v
×1
Female DC Socket to Screw Terminal Block Adaptor
Used to connect to power supply
×1
Craft wire
(optional) To create lamp shape
×1
Small cardboard box
(optional) To hold components and serve as a lamp stand
×1

Story

Read more

Schematics

Schematic

Code

Source Code

Arduino
Code to turn on the NeoPixels with 2 claps
#include <FastLED.h>

#define NUM_LEDS 19 // How many leds are in the strip?
#define signalToLEDPin 5 // LED pin
#define soundSensorPin 4 // sound sensor pin

// sound sensor variables
int lastSoundValue;
int soundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;


// LED variables
boolean lightOn = false; 
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
CRGB leds[NUM_LEDS]; // This is an array of leds.  One item for each led in your strip


// This function sets up the leds and tells the controller about them
void setup() {
  
    delay(2000); // sanity check delay - allows reprogramming if accidently blowing power w/leds

    FastLED.addLeds<WS2811, signalToLEDPin, RGB>(leds, NUM_LEDS);
    pinMode(soundSensorPin,INPUT);
    pinMode(signalToLEDPin, OUTPUT);
    LEDS.setBrightness(84);

}

// This function runs over and over, and is where you do the magic to light your leds.
void loop() {
  readSoundSensor();
}

void fadeall() { 
  for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } 
}

// LED pattern
void cyclon(){
  static uint8_t hue = 0;
  // First slide the led in one direction
  for(int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led to red 
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show(); 
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(80);
  }
  
  // Now go in the other direction.  
  for(int i = (NUM_LEDS)-1; i >= 0; i--) {
    // Set the i'th led to red 
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(30);
  }
  // End with a rainbow
  fill_rainbow( leds, NUM_LEDS, 0, 255/NUM_LEDS );
  FastLED.show();
}

// Switches LED off or display pattern
void updateLEDState(){
  if (!lightOn) {
      fill_solid(leds, NUM_LEDS, CRGB::Black);
      FastLED.show();
  } else {
      cyclon();
  }
}

// Sound sensor code
void readSoundSensor(){
  soundValue = digitalRead(soundSensorPin);
  currentNoiseTime = millis();

  if (soundValue == 1) { // if there is currently a noise
     if ((currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise
        (lastSoundValue == 0) &&  // if it was silent before
        (currentNoiseTime < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the first clap
        (currentNoiseTime > lastLightChange + 1000)) { // to avoid taking a third clap as part of a pattern 
        lightOn = !lightOn; 
        updateLEDState();
        lastLightChange = currentNoiseTime;
     }
     lastNoiseTime = currentNoiseTime;
  }
  lastSoundValue = soundValue;
}


  

Credits

a886a

a886a

0 projects • 0 followers

Comments