Kirsten RamseyKenneth Hitt
Published

Clap On/Off Lights

Don't you just hate getting into bed, getting comfortable, and then realizing you forgot to turn off the lights? Well this is for you!

IntermediateFull instructions provided5 hours1,957
Clap On/Off Lights

Things used in this project

Hardware components

Photon
Particle Photon
×2
SparkFun SEN 12642 Sound Detector
×1
Photo resistor
Photo resistor
×1
Resistor 100k ohm
Resistor 100k ohm
×1
Mini Smart Plug
×1

Software apps and online services

IFTTT
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Light Sensor Circuit Diagram

Sound Detector Circuit Diagram

Code

Sound/Clap Sensor

C#
The Photon reads the output of a SEN-12642 sound detector, and when the output reaches a certain value the photon will publish a "clapOn" or "clapOff" event.
int audioSensor = A0;   //labeling pin A0 for future use
int value = soundDetect();  //making value a integer
bool light = false;


void setup() {
    pinMode(audioSensor, INPUT);    //setting pinMode for input pin
    Particle.publish("clapOff", PRIVATE);   // making sure program always knows which state it is in by starting with light off
    Particle.subscribe("BrightRoom", myHandler2, MY_DEVICES); // BrightRoom function turns light off when room is brighter than 4000
}

void loop() {
    value = soundDetect();  // runs sound detect program
    if (value > 2500) {     //if loop constantly checks to see if sound value is higher than ####
        if (light == true) {
            light = false;  //sets light value to false which equals off
            Particle.publish("clapOff", PRIVATE);   //sends publish event to ifttt applet looking for "event_name_here"
            delay(1000);  //Delay to make sure loop doesn't run more than once per sound
        }
        else {
            light = true;   // sets light value to true which equals on
            Particle.publish("clapOn", PRIVATE);    //sends publish event to ifttt applet looking for "event_name_here"
            delay(1000);    //Delay to make sure loop doesn't run more than once per sound
        }    
        delay(1000);    //delay added to make program function properly     
    }
}

int soundDetect() {     //when soundDetect is call this is what is run
    return analogRead(audioSensor);     //this takes a reading from the audioSensor pin
}

void myHandler2(const char *BrightRoom, const char *data) {     //this function looks for BrightRoom Event
    Particle.publish("clapOff", PRIVATE);   //sends publish event to ifttt applet looking for "event_name_here"      
}

LightSensor

C#
This code gathers the output of a photo resistor and sends the data to ThinkSpeak to be graphed. Also, when the room is too bright it will shut off light when its not needed.
int lightsensor = A0;       // Labels A0 pin as lightsensor for further use
int value = readlight();    // sets value as an integer

void setup() {
    pinMode(D7, OUTPUT);    //sets D7 to be an output
    digitalWrite(D7, LOW);  //turns D7 off when photon just starts to clear any previous requests
    pinMode(lightsensor, INPUT);
    
    Particle.subscribe("clapOn", myHandler, MY_DEVICES);    // Subscribe to the integration response event
    Particle.subscribe("clapOff", myHandler2, MY_DEVICES);  // Subscribe to the integration response event
}    

void loop() {
    value = readlight();     //Runs readlight() program written below
    Particle.publish("LightRead", "{ \"value\": " + String(value) + ", \"key\": \"THINGSPEAK_API_KEY_HERE\" }", THINGSPEAKCHANNEL_ID_HERE, PRIVATE);          //publishes to ThingSpeak
    delay(1000);
    if (value > 4000) {
      Particle.publish("BrightRoom", PRIVATE);   // This checks to see if the room is bright enough to turn light off 
    }
}

int readlight() {   // readlight() program written here in global view
    int analogValue = analogRead(lightsensor); // sets lightsensor pin to a value
    return analogValue;     // returns analogValue number to where readlight() is 
}

int funblink() {    // code for the funblink() NOTE:Do not place below myHandler functions or Photon will only blink rapidly forever  
    digitalWrite(D7, HIGH);
    delay(50);
    digitalWrite(D7, LOW);
    delay(50);
    digitalWrite(D7, HIGH);
    delay(50);
    digitalWrite(D7, LOW);
    delay(50);
    digitalWrite(D7, HIGH);
    delay(50);
    digitalWrite(D7, LOW);
    delay(50);
    digitalWrite(D7, HIGH);
    delay(50);
    digitalWrite(D7, LOW);
    delay(50);
}

void myHandler(const char *clapOn, const char *data) {      // Looks for the clapon event
    digitalWrite(D7, HIGH);     // when clap on is published D7 Pin turned on 
}   

void myHandler2(const char *clapOff, const char *data) {    // looks for the clapoff event
    funblink();       // runs the fun blink program
}

Credits

Kirsten Ramsey

Kirsten Ramsey

1 project • 0 followers
Kenneth Hitt

Kenneth Hitt

1 project • 0 followers
Junior in Mechanical Engineering

Comments