Difference between revisions of "Clap Light Project (X02)"

From Microduino Wiki
Jump to: navigation, search
(Required Materials)
(Program)
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[File:Clap Light Project.jpg|right|thumb|Clap Light Project Build]]
 
=About=
 
=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).
+
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=
 
=Required Materials=
For 102 Basic Kit
 
 
*1 x mCookie Core
 
*1 x mCookie Core
*1 x mCookie USB TTL
+
*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
 
 
 
For 202 Advanced or 302 Expert Kit
 
*1 x mCookie Core
 
*1 x mBattery
 
 
*1 x mCookie Sensor Hub
 
*1 x mCookie Sensor Hub
 
*1 x Single ColorLED
 
*1 x Single ColorLED
Line 25: Line 16:
  
 
=Build=
 
=Build=
 +
'''NOTE''': When connecting sensor wires, push on the plastic connector and not on the wires. Pushing on the wire can damage them.
 
#Connect the Single ColorLED to the Sensor Hub on Pin A0/A1
 
#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 Buzzer to the Sensor Hub on Pin 6/7
 
#Connect the Microphone to the Sensor Hub on Pin A2/A3
 
#Connect the Microphone to the Sensor Hub on Pin A2/A3
#Grab the mCookie USBTTL and Stack the mCookie Core on top.
+
#Grab the mCookie USBTTL or mBattery and Stack the mCookie Core on top.
 
#Stack the mCookie Sensor on top of that.
 
#Stack the mCookie Sensor on top of that.
#Plug in the MicroUSB cable to the mCookie USBTTL a computer.
+
#Plug in the MicroUSB cable to the mCookie USBTTL or mBattery to a computer.
 +
 
 
=Program=
 
=Program=
Copy and paste the code below and upload.
+
1. Connect [[mCookie-Core|mCookie Core]] to the PC with the USB Cable. Open the Microduino IDE.
<source lang="cpp">
 
#include <Adafruit_NeoPixel.h>  //Import the library for the ColorLED.
 
#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
+
2. Download the project file and unzip: '''<big>[[File:Clap_Light_X02.zip]]</big>'''.
const uint16_t SLIENT_LOUDNESS_THRESHOLD = 200;
+
*Using the Microduino IDE and go to '''File > Open...''' and navigate to the unzipped folder. Open the '''Clap_Light_X02.ino''' file.
  
volatile uint16_t mic_value;
+
3. Select the board, processor and port:
unsigned long second_clap_timer;
+
*Go to '''Tools > Board'''  and select '''Microduino/mCookie-Core (328p)'''
 +
*Go to '''Tools > Processor''' and select '''Atmega328P16M,5V'''
 +
*Go to '''Tools > Port''' and select the available port
 +
4. Upload the program by clicking on the '''right arrow icon''' on the top left of the window. Or under '''Sketch > Upload'''.
  
bool leds_are_on = false;
+
'''NOTE''': If not using the default Core module included in the kits, please follow the '''[[Selecting_board,_processor,_and_port|selecting the board and processor guide]]'''.
  
 +
=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.
  
void setup() {
+
=Tweaking=
  pinMode(LED_PIN, OUTPUT);
+
Variables can be edited to change the threshold / detection value for various functions to better fine tune to your environment.
 
 
  Serial.begin(9600);
 
  // put your setup code here, to run once:
 
  pinMode(MIC_PIN, INPUT);
 
  pinMode(BUZZER_PIN, OUTPUT);
 
  
}
+
'''DEBUG''' un-comment to debug.
  
void loop() {
+
'''MIC_PIN''' defines the pin that connects to the Microphone.
  // put your main code here, to run repeatedly:
 
  
 +
'''BUZZER_PIN''' defines the pin that connects to the Buzzer.
  
  //Take in mic reading for the first clap
+
'''LED_PIN''' defines the pin that connects to the LED.
  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(1000); //Wait 500 milliseconds
 
    //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 slient
 
    if ( mic_value < SLIENT_LOUDNESS_THRESHOLD ) {
 
      Serial.println("Slience detected!");
 
      //wait a bit of time beforce looking for the second clap
 
      delay(1000);
 
      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);
+
'''CLAP_LOUDNESS_THRESHOLD''' will change the clapping loudness trigger point. Lower values means a weaker noise level is detected as a clap.
}
+
*Adjustable between 0 ~ 1023
 +
*Default is 400
  
void activate_light() {
+
'''SILENT_LOUDNESS_THRESHOLD''' will change the silent registry level. Higher values will mean that higher noise levels will be registered as a silence.
  digitalWrite(LED_PIN, HIGH);
+
*Adjustable between 0 ~ 1023
  leds_are_on = true;
+
*Default is 200
}
 
  
void deactivate_light() {
+
'''PAUSE_TIME''' will change the time in which you are required to pause between claps in milliseconds. Lower values will detect faster claps, but there is settling time on the Microphone, which means too short of a pause time won't be detectable as silence.
  digitalWrite(LED_PIN, LOW);
+
* Default is 200 milliseconds (1/5 of a second)
  leds_are_on = false;
 
  return;
 
}
 
</source>
 
  
 
=Usage=
 
=Usage=
Clap once, wait about 1-2 seconds, then clap another time. The LED should turn on. Repeating the sequence will turn off the LED.
+
Clap once, wait about 1 second, then clap another time. The LED should turn on and a beep noise will occur. Repeating the sequence will turn off the LED.

Latest revision as of 21:35, 1 May 2017

Clap Light Project Build

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

NOTE: When connecting sensor wires, push on the plastic connector and not on the wires. Pushing on the wire can damage them.

  1. Connect the Single ColorLED to the Sensor Hub on Pin A0/A1
  2. Connect the Buzzer to the Sensor Hub on Pin 6/7
  3. Connect the Microphone to the Sensor Hub on Pin A2/A3
  4. Grab the mCookie USBTTL or mBattery and Stack the mCookie Core on top.
  5. Stack the mCookie Sensor on top of that.
  6. Plug in the MicroUSB cable to the mCookie USBTTL or mBattery to a computer.

Program

1. Connect mCookie Core to the PC with the USB Cable. Open the Microduino IDE.

2. Download the project file and unzip: File:Clap Light X02.zip.

  • Using the Microduino IDE and go to File > Open... and navigate to the unzipped folder. Open the Clap_Light_X02.ino file.

3. Select the board, processor and port:

  • Go to Tools > Board and select Microduino/mCookie-Core (328p)
  • Go to Tools > Processor and select Atmega328P16M,5V
  • Go to Tools > Port and select the available port

4. Upload the program by clicking on the right arrow icon on the top left of the window. Or under Sketch > Upload.

NOTE: If not using the default Core module included in the kits, please follow the selecting the board and processor guide.

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

Variables can be edited to change the threshold / detection value for various functions to better fine tune to your environment.

DEBUG un-comment to debug.

MIC_PIN defines the pin that connects to the Microphone.

BUZZER_PIN defines the pin that connects to the Buzzer.

LED_PIN defines the pin that connects to the LED.

CLAP_LOUDNESS_THRESHOLD will change the clapping loudness trigger point. Lower values means a weaker noise level is detected as a clap.

  • Adjustable between 0 ~ 1023
  • Default is 400

SILENT_LOUDNESS_THRESHOLD will change the silent registry level. Higher values will mean that higher noise levels will be registered as a silence.

  • Adjustable between 0 ~ 1023
  • Default is 200

PAUSE_TIME will change the time in which you are required to pause between claps in milliseconds. Lower values will detect faster claps, but there is settling time on the Microphone, which means too short of a pause time won't be detectable as silence.

  • Default is 200 milliseconds (1/5 of a second)

Usage

Clap once, wait about 1 second, then clap another time. The LED should turn on and a beep noise will occur. Repeating the sequence will turn off the LED.