Sydney Curran
Published

Lane Tech HS -- PCL -- Automated Light Switch

Toggle the lights without getting up!

IntermediateFull instructions provided6 hours415
Lane Tech HS -- PCL -- Automated Light Switch

Things used in this project

Hardware components

Argon
Particle Argon
×1
Photo resistor
Photo resistor
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Amazon Alexa service
IFTTT Amazon Alexa service

Story

Read more

Schematics

Schematic

Code

Home Automation

C/C++
Main body of the code.
// This #include statement was automatically added by the Particle IDE.
#include "functions.h"

Servo top; 
Servo bottom;

const int lightPin = A1;

boolean switchState;
boolean lightState;

const int cutoff = 170;

int light1;

boolean override = false;

void setup() {
    top.attach(2);  
    bottom.attach(4);
    top.write(150);
    bottom.write(0);
  
    pinMode(A0, INPUT);
    
    Particle.subscribe("Lights", lightsHandler);
    Particle.subscribe("Override", overrideToggleHandler);
    
    Particle.function("DirectorOverrideOn", DirectorOverrideToggleOn);
    Particle.function("DirectorOverrideOff", DirectorOverrideToggleOff);
    Particle.function("ResetTheLights", ResetLightsCloud);
    
    Particle.variable("LightState", lightState);
    Particle.variable("DirectorOverrideVar", override);
    Particle.variable("SwitchState", switchState);
    
    lightState = resetLights(lightState, lightPin, top, bottom);
    switchState = 1;
    
    delay(500);
    
}
void loop() {
 light1 = analogRead(lightPin);
 int nowTime = Time.hour();
 boolean sleepOverride = isSleepyTime(nowTime);
 if(override == false){
    if (lightState == 0 && light1 < cutoff && sleepOverride == 0){
        toggleLights(switchState, top, bottom);
        switchState = !switchState;
        int light2 = analogRead(lightPin);
        if (light2 != light1){
            lightState = 1;
        }
        else{
            toggleLights(switchState, top, bottom);
            lightState = 1;
        }
    }
    
    if(sleepOverride == 1 && lightState == 1){
        toggleLights(switchState, top, bottom);
        switchState = !switchState;
        int light2 = analogRead(lightPin);
         if (light2 != light1){
            lightState = 0;
        }
        else{
            toggleLights(switchState, top, bottom);
            lightState = 0;
        }
    }
 }
}

void lightsHandler (const char* event, const char* data){
    int toggle1 = analogRead(lightPin);
    toggleLights(switchState, top, bottom);
    int toggle2 = analogRead(lightPin);
    switchState = !switchState;
    if (toggle2 != toggle1){
        lightState = !lightState;
    }
    else{
        toggleLights(switchState, top, bottom);
        lightState = !lightState;
    }
}
int overrideToggleHandler (const char* event, const char* data){
    int Data = (int) data;
    if (Data == 1){
        override = 1;
    }
    if (Data == 0){
        override = 0;
    }
    return override;
}

int DirectorOverrideToggleOn (String d){
    override = true;
    return 1;
}
int DirectorOverrideToggleOff (String D){
    override = false;
    return 0;
}

int ResetLightsCloud (String y){
   lightState = resetLights(lightState, lightPin, top, bottom);
   switchState = 1;
   return 1;
}

Functions.h

C Header File
Header file for many of the functions in the project.
int pos;

void toggleLights(boolean switchState, Servo top, Servo bottom){
    if(switchState == 1){
        for (pos = 150; pos >= 100; pos -= 1) { 
            top.write(pos);              
            delay(15);                       
        }

        for (pos = 100; pos <= 150; pos += 1) { 
            top.write(pos);              
            delay(15);                      
        }
        
    }
    if(switchState == 0){
       for (pos = 0; pos <= 50; pos += 1) { 
            bottom.write(pos);             
            delay(15);                      
        }
        for (pos = 50; pos >= 0; pos -= 1) { 
            bottom.write(pos);             
            delay(15);                      
        } 
    }
}

boolean resetLights(boolean lightState, int lightPin, Servo top, Servo bottom){
    for (pos = 150; pos >= 100; pos -= 1) { 
        top.write(pos);              
        delay(5);                       
    }
    for (pos = 100; pos <= 150; pos += 1) { 
        top.write(pos);              
        delay(5);                       
    }
    int read1 = analogRead(lightPin);
    delay(50);
    
    for (pos = 0; pos <= 50; pos += 1) { 
        bottom.write(pos);              
        delay(5);                      
    }
    for (pos = 50; pos >= 0; pos -= 1) { 
        bottom.write(pos);             
        delay(5);                       
    }
    delay(50);
    int read2 = analogRead(lightPin);
    
    if (read2 <= read1){
        lightState = 0;
    }
    if (read2 > read1){
        lightState = 1;
    }
    return lightState;
}

boolean isSleepyTime (int theTime){
    if(theTime > 2 && theTime < 11){
        return true;
    }
    else{
        return false;
    }
}

Credits

Sydney Curran

Sydney Curran

3 projects • 2 followers

Comments