Double clap LED Lamp
Turn on an LED strip with 2 claps using a sound sensor
Components and supplies
1
Breadboard (generic)
1
Male/Female Jumper Wires
1
AC/DC Power Supply, External Plug In
1
Small cardboard box
1
Resistor 1k ohm
1
Female DC Socket to Screw Terminal Block Adaptor
1
Arduino UNO
1
Jumper wires (generic)
1
KY-037 Sound Sensor
1
LED Strip, NeoPixel Digital RGB
1
Capacitor 1000 µF
1
Craft wire
Project description
Code
Source Code
arduino
Code to turn on the NeoPixels with 2 claps
1#include <FastLED.h> 2 3#define NUM_LEDS 19 // How many leds are in the strip? 4#define signalToLEDPin 5 // LED pin 5#define soundSensorPin 4 // sound sensor pin 6 7// sound sensor variables 8int lastSoundValue; 9int soundValue; 10long lastNoiseTime = 0; 11long currentNoiseTime = 0; 12long lastLightChange = 0; 13 14 15// LED variables 16boolean lightOn = false; 17uint8_t gHue = 0; // rotating "base color" used by many of the patterns 18CRGB leds[NUM_LEDS]; // This is an array of leds. One item for each led in your strip 19 20 21// This function sets up the leds and tells the controller about them 22void setup() { 23 24 delay(2000); // sanity check delay - allows reprogramming if accidently blowing power w/leds 25 26 FastLED.addLeds<WS2811, signalToLEDPin, RGB>(leds, NUM_LEDS); 27 pinMode(soundSensorPin,INPUT); 28 pinMode(signalToLEDPin, OUTPUT); 29 LEDS.setBrightness(84); 30 31} 32 33// This function runs over and over, and is where you do the magic to light your leds. 34void loop() { 35 readSoundSensor(); 36} 37 38void fadeall() { 39 for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } 40} 41 42// LED pattern 43void cyclon(){ 44 static uint8_t hue = 0; 45 // First slide the led in one direction 46 for(int i = 0; i < NUM_LEDS; i++) { 47 // Set the i'th led to red 48 leds[i] = CHSV(hue++, 255, 255); 49 // Show the leds 50 FastLED.show(); 51 // now that we've shown the leds, reset the i'th led to black 52 // leds[i] = CRGB::Black; 53 fadeall(); 54 // Wait a little bit before we loop around and do it again 55 delay(80); 56 } 57 58 // Now go in the other direction. 59 for(int i = (NUM_LEDS)-1; i >= 0; i--) { 60 // Set the i'th led to red 61 leds[i] = CHSV(hue++, 255, 255); 62 // Show the leds 63 FastLED.show(); 64 // now that we've shown the leds, reset the i'th led to black 65 // leds[i] = CRGB::Black; 66 fadeall(); 67 // Wait a little bit before we loop around and do it again 68 delay(30); 69 } 70 // End with a rainbow 71 fill_rainbow( leds, NUM_LEDS, 0, 255/NUM_LEDS ); 72 FastLED.show(); 73} 74 75// Switches LED off or display pattern 76void updateLEDState(){ 77 if (!lightOn) { 78 fill_solid(leds, NUM_LEDS, CRGB::Black); 79 FastLED.show(); 80 } else { 81 cyclon(); 82 } 83} 84 85// Sound sensor code 86void readSoundSensor(){ 87 soundValue = digitalRead(soundSensorPin); 88 currentNoiseTime = millis(); 89 90 if (soundValue == 1) { // if there is currently a noise 91 if ((currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise 92 (lastSoundValue == 0) && // if it was silent before 93 (currentNoiseTime < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the first clap 94 (currentNoiseTime > lastLightChange + 1000)) { // to avoid taking a third clap as part of a pattern 95 lightOn = !lightOn; 96 updateLEDState(); 97 lastLightChange = currentNoiseTime; 98 } 99 lastNoiseTime = currentNoiseTime; 100 } 101 lastSoundValue = soundValue; 102} 103 104 105
Source Code
arduino
Code to turn on the NeoPixels with 2 claps
1#include <FastLED.h> 2 3#define NUM_LEDS 19 // How many leds are in 4 the strip? 5#define signalToLEDPin 5 // LED pin 6#define soundSensorPin 4 // 7 sound sensor pin 8 9// sound sensor variables 10int lastSoundValue; 11int 12 soundValue; 13long lastNoiseTime = 0; 14long currentNoiseTime = 0; 15long lastLightChange 16 = 0; 17 18 19// LED variables 20boolean lightOn = false; 21uint8_t gHue = 22 0; // rotating "base color" used by many of the patterns 23CRGB leds[NUM_LEDS]; 24 // This is an array of leds. One item for each led in your strip 25 26 27// 28 This function sets up the leds and tells the controller about them 29void setup() 30 { 31 32 delay(2000); // sanity check delay - allows reprogramming if accidently 33 blowing power w/leds 34 35 FastLED.addLeds<WS2811, signalToLEDPin, RGB>(leds, 36 NUM_LEDS); 37 pinMode(soundSensorPin,INPUT); 38 pinMode(signalToLEDPin, 39 OUTPUT); 40 LEDS.setBrightness(84); 41 42} 43 44// This function runs over 45 and over, and is where you do the magic to light your leds. 46void loop() { 47 48 readSoundSensor(); 49} 50 51void fadeall() { 52 for(int i = 0; i < NUM_LEDS; 53 i++) { leds[i].nscale8(250); } 54} 55 56// LED pattern 57void cyclon(){ 58 59 static uint8_t hue = 0; 60 // First slide the led in one direction 61 for(int 62 i = 0; i < NUM_LEDS; i++) { 63 // Set the i'th led to red 64 leds[i] = 65 CHSV(hue++, 255, 255); 66 // Show the leds 67 FastLED.show(); 68 // 69 now that we've shown the leds, reset the i'th led to black 70 // leds[i] = CRGB::Black; 71 72 fadeall(); 73 // Wait a little bit before we loop around and do it again 74 75 delay(80); 76 } 77 78 // Now go in the other direction. 79 for(int 80 i = (NUM_LEDS)-1; i >= 0; i--) { 81 // Set the i'th led to red 82 leds[i] 83 = CHSV(hue++, 255, 255); 84 // Show the leds 85 FastLED.show(); 86 // 87 now that we've shown the leds, reset the i'th led to black 88 // leds[i] = CRGB::Black; 89 90 fadeall(); 91 // Wait a little bit before we loop around and do it again 92 93 delay(30); 94 } 95 // End with a rainbow 96 fill_rainbow( leds, NUM_LEDS, 97 0, 255/NUM_LEDS ); 98 FastLED.show(); 99} 100 101// Switches LED off or display 102 pattern 103void updateLEDState(){ 104 if (!lightOn) { 105 fill_solid(leds, 106 NUM_LEDS, CRGB::Black); 107 FastLED.show(); 108 } else { 109 cyclon(); 110 111 } 112} 113 114// Sound sensor code 115void readSoundSensor(){ 116 soundValue 117 = digitalRead(soundSensorPin); 118 currentNoiseTime = millis(); 119 120 if (soundValue 121 == 1) { // if there is currently a noise 122 if ((currentNoiseTime > lastNoiseTime 123 + 200) && // to debounce a sound occurring in more than a loop cycle as a single 124 noise 125 (lastSoundValue == 0) && // if it was silent before 126 (currentNoiseTime 127 < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the 128 first clap 129 (currentNoiseTime > lastLightChange + 1000)) { // to avoid 130 taking a third clap as part of a pattern 131 lightOn = !lightOn; 132 updateLEDState(); 133 134 lastLightChange = currentNoiseTime; 135 } 136 lastNoiseTime = currentNoiseTime; 137 138 } 139 lastSoundValue = soundValue; 140} 141 142 143
Downloadable files
Schematic
Schematic

Comments
Only logged in users can leave comments