Sound Sensitive LED Project (X02)

From Microduino Wiki
Revision as of 20:51, 3 January 2017 by Sonny (talk) (Undo revision 15593 by Sonny (talk))
Jump to: navigation, search

About

This project uses the Microphone to detect sounds. Based on the sound levels, it maps the loudness to the LED Matrix's brightness. Visually see the loudness of the surrounding sound on the LED Matrix!

Color of the LED Matrix can be adjust between 7 colors with the touch button.

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 mCooke LED Matrix
  • 1 x Microphone Sensor
  • 1 x Touch Button
  • 2 x Sensor Cable
  • 1 x MicroUSB Cable

Build

  1. Connect the Microphone Sensor to the Sensor Hub on Pin A2/A3
  2. Connect the Touch Button to the Sensor Hub on Pin 6/7
  3. Grab the mCookie USBTTL or mBattery and Stack the mCookie Core on top.
  4. Stack the mCookie LED Matrix on top of that.
  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

Copy and paste the code below and upload.

#include <Adafruit_NeoPixel.h>//Import the library for the ColorLED.
#include <stdint.h>

#define LEDMATRIX_PIN A0
#define MIC_PIN A2
#define TOUCH_PIN 6

#define NUMPIXELS 6

#define AMBIENT_THRESHOLD 20
#define MAX_THRESHOLD 200

unsigned long debounce_time_milliseconds = 200;
unsigned long debounce_time_touch_pin = 0;

Adafruit_NeoPixel matrix = Adafruit_NeoPixel(NUMPIXELS, LEDMATRIX_PIN, NEO_GRB + NEO_KHZ800);

uint8_t current_selection = 0;
uint8_t current_selection_count = 7;

uint8_t color_matrix[9][3]  ={
  {1, 0, 0},
  {0, 1, 0},
  {0, 0, 1},
  {0, 1, 1},
  {1, 0, 1},
  {1, 1, 0},
  {1, 1, 1}
};


void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  pinMode(MIC_PIN, INPUT);
  pinMode(TOUCH_PIN, INPUT);

  matrix.begin();
  matrix.show();
}

void loop() {
  // put your main code here, to run repeatedly:

  //Check if right button is pressed.
  if( !digitalRead(TOUCH_PIN ) 
      && 
      ((millis() - debounce_time_touch_pin ) > debounce_time_milliseconds)
  ){
    debounce_time_touch_pin = millis();
    if(++current_selection >= current_selection_count){
      current_selection = 0;
    }   
  }

  uint16_t mic_value = analogRead(MIC_PIN);
  //Serial.println(mic_value);

  if(mic_value < AMBIENT_THRESHOLD){
    mic_value = 0;
  }
  if(mic_value > MAX_THRESHOLD){
    mic_value = MAX_THRESHOLD;
  }
  
  uint8_t brightness = map(analogRead(MIC_PIN), 0, MAX_THRESHOLD, 0, 255);

  for (uint16_t j = 0; j < NUMPIXELS; ++j) {
    matrix.setPixelColor(j, matrix.Color(color_matrix[current_selection][0] * brightness, color_matrix[current_selection][1] * brightness, color_matrix[current_selection][2] * brightness));
  }

  matrix.show();
  delay(1);


}

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.

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.