Difference between revisions of "Clap Light Project (X02)"
(→Usage) |
(→Program) |
||
Line 24: | Line 24: | ||
Copy and paste the code below and upload. | Copy and paste the code below and upload. | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | |||
#include <stdint.h> | #include <stdint.h> | ||
Line 36: | Line 35: | ||
//The maximum loudness between the two claps | //The maximum loudness between the two claps | ||
const uint16_t SILENT_LOUDNESS_THRESHOLD = 200; | const uint16_t SILENT_LOUDNESS_THRESHOLD = 200; | ||
+ | |||
+ | //The amount of time from the detection of the first clap until testing if there is silence | ||
+ | const uint16_t PAUSE_TIME = 250; | ||
volatile uint16_t mic_value; | volatile uint16_t mic_value; | ||
Line 62: | Line 64: | ||
if ( mic_value > CLAP_LOUDNESS_THRESHOLD ) { | if ( mic_value > CLAP_LOUDNESS_THRESHOLD ) { | ||
Serial.println("First clap detected!"); | Serial.println("First clap detected!"); | ||
− | delay( | + | delay(PAUSE_TIME); //Wait PAUSE_TIME |
//Read in a new mic value for the silence between the claps | //Read in a new mic value for the silence between the claps | ||
mic_value = analogRead(mic_value); | mic_value = analogRead(mic_value); | ||
Line 70: | Line 72: | ||
Serial.println("Silence detected!"); | Serial.println("Silence detected!"); | ||
//wait a bit of time beforce looking for the second clap | //wait a bit of time beforce looking for the second clap | ||
− | delay( | + | //delay(PAUSE_TIME); |
second_clap_timer = millis(); | second_clap_timer = millis(); | ||
while(((millis() - second_clap_timer) < 2000)){ | while(((millis() - second_clap_timer) < 2000)){ | ||
Line 113: | Line 115: | ||
'''SILENT_LOUDNESS_THRESHOLD''' will change the silent registry level. 0~1023. Default is 200. | '''SILENT_LOUDNESS_THRESHOLD''' will change the silent registry level. 0~1023. Default is 200. | ||
+ | |||
+ | '''PAUSE_TIME''' will change the time in which you pause between claps in milliseconds. Default is 200 milliseconds or 1/5 of a second. | ||
=Usage= | =Usage= | ||
Clap once, wait about 1-2 seconds, then clap another time. The LED should turn on and a bump noise will occur. Repeating the sequence will turn off the LED. | Clap once, wait about 1-2 seconds, then clap another time. The LED should turn on and a bump noise will occur. Repeating the sequence will turn off the LED. |
Revision as of 01:40, 30 December 2016
About
This project uses the Microphone to detect clapping noises and turns on or off the LED if it senses two claps with a second pause inbetween. This project was designed for the second generation mCookie Maker kits (102 Basic, 202 Advanced and 302 Expert kits).
Required Materials
- 1 x mCookie Core
- 1 x mCookie USB TTL (102 Kit) or 1 x mBattery (202/302 Kit)
- 1 x mCookie Sensor Hub
- 1 x Single ColorLED
- 1 x Microphone Sensor
- 1 x Buzzer
- 3 x Sensor Cable
- 1 x MicroUSB Cable
Build
- Connect the Single ColorLED to the Sensor Hub on Pin A0/A1
- Connect the Buzzer to the Sensor Hub on Pin 6/7
- Connect the Microphone to the Sensor Hub on Pin A2/A3
- Grab the mCookie USBTTL or mBattery and Stack the mCookie Core on top.
- Stack the mCookie Sensor on top of that.
- Plug in the MicroUSB cable to the mCookie USBTTL or mBattery to a computer.
Program
Copy and paste the code below and upload.
#include <stdint.h>
#define MIC_PIN A2
#define BUZZER_PIN 6
#define LED_PIN A0
//The minimum amount of clap loudness to be registered as a clap
const uint16_t CLAP_LOUDNESS_THRESHOLD = 400;
//The maximum loudness between the two claps
const uint16_t SILENT_LOUDNESS_THRESHOLD = 200;
//The amount of time from the detection of the first clap until testing if there is silence
const uint16_t PAUSE_TIME = 250;
volatile uint16_t mic_value;
unsigned long second_clap_timer;
bool leds_are_on = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(MIC_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//Take in mic reading for the first clap
uint16_t mic_value = analogRead(MIC_PIN);
//Check if it is greater than the threadhold
if ( mic_value > CLAP_LOUDNESS_THRESHOLD ) {
Serial.println("First clap detected!");
delay(PAUSE_TIME); //Wait PAUSE_TIME
//Read in a new mic value for the silence between the claps
mic_value = analogRead(mic_value);
Serial.println(mic_value);
//If the value is under the threshold, then it is silent
if ( mic_value < SILENT_LOUDNESS_THRESHOLD ) {
Serial.println("Silence detected!");
//wait a bit of time beforce looking for the second clap
//delay(PAUSE_TIME);
second_clap_timer = millis();
while(((millis() - second_clap_timer) < 2000)){
mic_value = analogRead(MIC_PIN);
if(mic_value > CLAP_LOUDNESS_THRESHOLD){
Serial.println("Second clap detected!");
tone(BUZZER_PIN, 3038, 200);
if( leds_are_on == false ){
activate_light();
}
else{
deactivate_light();
}
delay(1000);
break;
}
}
}
}
delay(1);
}
void activate_light() {
digitalWrite(LED_PIN, HIGH);
leds_are_on = true;
}
void deactivate_light() {
digitalWrite(LED_PIN, LOW);
leds_are_on = false;
return;
}
Debugging
- Open the Serial monitor. Information will print out when the first clap is detected as "First clap detected!".
- Then a value will print out which is the silent value. If it is under the threshold, "Silence detected!" will print.
- "Second clap detected!" will print if it detected a second clap.
Tweaking
CLAP_LOUDNESS_THRESHOLD will change the clapping loudness trigger point. 0 ~ 1023. Default is 400.
SILENT_LOUDNESS_THRESHOLD will change the silent registry level. 0~1023. Default is 200.
PAUSE_TIME will change the time in which you pause between claps in milliseconds. Default is 200 milliseconds or 1/5 of a second.
Usage
Clap once, wait about 1-2 seconds, then clap another time. The LED should turn on and a bump noise will occur. Repeating the sequence will turn off the LED.