Light Clapper [MAX4466]
This is a project that I quickly made one night because I wanted to 'clap' to turn off and on the light in my room! I have a small room for myself, so I basically have no other noise around. So, I was able to use an amplifier, which finds the value of the sound level in its surroundings. I hooked this amplifier, MAX4466 from Adafruit, and a servo that I had from a while ago. This project took some time to engineer for the servo to attach on my light switch. We took a wire, that I got from someone that does jewelry, put tape around the wire, and tightened the servo on the metal plate that was covering the light switch. Then, I found the value for my clap's loudness, and set the range in the code so that the servo will turn off and on the light when that level was reached. I really enjoyed the project; I stayed on my bed for hours, both awake and asleep, being able to turn on and off the light on my bed. A tip might be that you might want to connect the arduino to an external power source, not your computer when having it on for a while, so that you can use your computer during that time for other things. I hope you enjoy!
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
#include <Servo.h>
int servoPin = 9; // servo's digital output pin
Servo servo;
int angle = 0;
int Pin = 3; // amplifier's analogue output pin
void setup(){
Serial.begin(9600);
servo.attach(servoPin);
}
void loop() {
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow){
sample = analogRead(Pin);
if (sample < 1024){ // toss out spurious readings
if (sample > signalMax){
signalMax = sample; // save just the max levels
}
else if (sample < signalMin){
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
Serial.print("peakToPeak: "); Serial.println(peakToPeak);
if (peakToPeak > 200 && peakToPeak < 1000) {
// if the sound level is more than 200 but less than 1000, turn the servo
for(angle = 0; angle < 100; angle++){ // angle < 100 will determine how much your servo will turn
servo.write(angle);
delay(15);
}
}
}
// adapted from Adafruit's tutorial on the amplifier