Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created February 3, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilukasz/4d649fc7c1ec8d9005d2 to your computer and use it in GitHub Desktop.
Save hilukasz/4d649fc7c1ec8d9005d2 to your computer and use it in GitHub Desktop.
int soundSensor = A2;
int relay = A0;
int relay2 = A3;
int relay3 = A4;
int relay4 = A5;
int claps = 0;
long detectionSpanInitial = 0;
long detectionSpan = 0;
boolean clapState = false;
int doubleClap = 0;
//Light sensor
int photocellPin = A1; // the cell and 10K pulldown are connected to a0
int LEDbrightness;
//int delayValue = 100;
int photoSensor = 0;
// sound sample
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
void setup() {
pinMode(soundSensor, INPUT);
pinMode(relay, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
Serial.begin(9600);
}
boolean state = false;
boolean lastState = true;
void loop() {
/////////////
// photo sensor
////////////////////
photoSensor = analogRead(photocellPin) > 523;
/////////////
// clap sensor
/////////////////////
//int sensorState = digitalRead(soundSensor);
double sensorState = getSoundLevel();
//Serial.println(sensorState);
double threshold = 1;
if (sensorState > threshold)
{
if (claps == 0)
{
detectionSpanInitial = detectionSpan = millis();
claps++;
}
else if (claps > 0 && millis()-detectionSpan >= 10)
{
detectionSpan = millis();
claps++;
Serial.println(" CLAP ADDED!!!!");
}
}
if (millis()-detectionSpanInitial >= 600)
{
if (claps == 2)
{
doubleClap = 1 - doubleClap;
state = !state;
//lastState = !lastState;
Serial.println("TWO claps!");
if(lastState == state){
relayControl();
lastState = !lastState;
}
}
claps = 0;
//lastState = state;
}
}
void relayControl(){
/////////////
// relay control
/////////////////////
int delayAmount = 100;
if(doubleClap == 1 && photoSensor == 0){
digitalWrite(relay, LOW);
delay(delayAmount);
digitalWrite(relay2, LOW);
delay(delayAmount);
digitalWrite(relay3, LOW);
delay(delayAmount);
digitalWrite(relay4, LOW);
Serial.println("on");
}
else{
digitalWrite(relay, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
Serial.println("off");
};
}
double getSoundLevel(){
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(soundSensor);
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
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
return volts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment