Components and supplies
High Brightness LED, White
SparkFun Sound Detector (with Headers)
Arduino Micro
Tools and machines
PCB, For DMB-4775
Soldering iron (generic)
Solder Wire, Lead Free
Project description
Code
ClapLight
c_cpp
1/* 2* GND → GND 3 * VCC → 5V 4 * Gate → Pin 4 5 * Envelope → A0 6 7 * 8 * LED → 7 9 * LED → 9 10 */ 11int LED0 = 7; 12int firstClap; 13int 14 LED1 = 9; 15 16 17 18// if true, the LED is on 19// if false, then LED is off 20bool 21 state = false; 22 23// when the last clapped happened 24unsigned long lastClapTime; 25 26// 27 pin's state. 28void soundISR() 29{ 30 int pin_val; 31 32 pin_val = digitalRead(PIN_GATE_IN); 33 34 digitalWrite(PIN_LED_OUT, pin_val); 35} 36 37void setup() 38{ 39 Serial.begin(9600); 40 41 42 // Configure LED pin as output 43 pinMode(PIN_LED_OUT, OUTPUT); 44 45 // 46 configure input to interrupt 47 pinMode(PIN_GATE_IN, INPUT); 48 attachInterrupt(IRQ_GATE_IN, 49 soundISR, CHANGE); 50 51 52 pinMode(3, OUTPUT); 53 digitalWrite(3, LOW); 54 55 56 pinMode(LED0, OUTPUT); 57 pinMode(LED1, OUTPUT); 58 59 // when we start, 60 LED is off 61 state = false; 62 lastClapTime = 0; 63 64 digitalWrite(LED0, 65 LOW); 66 digitalWrite(LED1, LOW); 67} 68 69// 70// This function returns true 71 if person has clapped 72// else it returns false. 73// 74bool isClapped(int sound) 75 { 76 if (sound > 50) { 77 return true; 78 } 79 return false; 80} 81 82void 83 toggleState() { 84 if (state == true) { 85 state = false; 86 digitalWrite(LED0, 87 LOW); 88 digitalWrite(LED1, LOW); 89 } else { 90 state = true; 91 digitalWrite(LED0, 92 HIGH); 93 digitalWrite(LED1, HIGH); 94 } 95} 96 97 98void loop() { 99 100 int sound = analogRead(PIN_ANALOG_IN); 101 102 unsigned long now = millis(); 103 104 if (isClapped(sound)) { 105 delay(200); 106 107 // check if the two 108 previos claps were within 1 second of one another 109 if (lastClapTime + 1000 110 > now) { 111 Serial.println("Now: " + String(now) + " LastClappedTime: " 112 + String(lastClapTime) + " Toggling"); 113 toggleState(); 114 lastClapTime 115 = 0; 116 delay(1000); 117 } else { 118 Serial.println("Now: " + String(now) 119 + " LastClappedTime: " + String(lastClapTime)); 120 lastClapTime = now; 121 122 } 123 } 124}
ClapLight
c_cpp
1/* 2* GND → GND 3 * VCC → 5V 4 * Gate → Pin 4 5 * Envelope → A0 6 * 7 * LED → 7 8 * LED → 9 9 */ 10int LED0 = 7; 11int firstClap; 12int LED1 = 9; 13 14 15 16// if true, the LED is on 17// if false, then LED is off 18bool state = false; 19 20// when the last clapped happened 21unsigned long lastClapTime; 22 23// pin's state. 24void soundISR() 25{ 26 int pin_val; 27 28 pin_val = digitalRead(PIN_GATE_IN); 29 digitalWrite(PIN_LED_OUT, pin_val); 30} 31 32void setup() 33{ 34 Serial.begin(9600); 35 36 // Configure LED pin as output 37 pinMode(PIN_LED_OUT, OUTPUT); 38 39 // configure input to interrupt 40 pinMode(PIN_GATE_IN, INPUT); 41 attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE); 42 43 44 pinMode(3, OUTPUT); 45 digitalWrite(3, LOW); 46 47 pinMode(LED0, OUTPUT); 48 pinMode(LED1, OUTPUT); 49 50 // when we start, LED is off 51 state = false; 52 lastClapTime = 0; 53 54 digitalWrite(LED0, LOW); 55 digitalWrite(LED1, LOW); 56} 57 58// 59// This function returns true if person has clapped 60// else it returns false. 61// 62bool isClapped(int sound) { 63 if (sound > 50) { 64 return true; 65 } 66 return false; 67} 68 69void toggleState() { 70 if (state == true) { 71 state = false; 72 digitalWrite(LED0, LOW); 73 digitalWrite(LED1, LOW); 74 } else { 75 state = true; 76 digitalWrite(LED0, HIGH); 77 digitalWrite(LED1, HIGH); 78 } 79} 80 81 82void loop() { 83 int sound = analogRead(PIN_ANALOG_IN); 84 85 unsigned long now = millis(); 86 if (isClapped(sound)) { 87 delay(200); 88 89 // check if the two previos claps were within 1 second of one another 90 if (lastClapTime + 1000 > now) { 91 Serial.println("Now: " + String(now) + " LastClappedTime: " + String(lastClapTime) + " Toggling"); 92 toggleState(); 93 lastClapTime = 0; 94 delay(1000); 95 } else { 96 Serial.println("Now: " + String(now) + " LastClappedTime: " + String(lastClapTime)); 97 lastClapTime = now; 98 } 99 } 100}
Downloadable files
untitled
untitled
Comments
Only logged in users can leave comments
ShreyanR
4 years ago
Does the Sound Detector measure in decibels? If not, how many decibels is 50 "sound" on the sensor?