Tyler ThompsonThomas Lawe
Published

Synchronized Light Switch

This project uses two Particle Photons and photoresistors to synchronize a pair of light switches in a combined living room/kitchen.

IntermediateFull instructions provided819
Synchronized Light Switch

Things used in this project

Hardware components

Photon
Particle Photon
×2
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Photo resistor
Photo resistor
×2
Futaba S3003 Standard Servo
×1
Male/Male Jumper Wires
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Light Sensor Schematic

Servo Schematic

Code

Light Sensor Code

C/C++
This is the code used on the photon with the photoresistors to send out events that trigger the servo control Photon.
int led = D7;
int ph1 = A0;
int ph2=A1;
int power = A5;
int LightKitchen;     
int LightLiving;
bool Darker= false;
int On=1;
int Off=1;

//This section of code assigns the pins on the Particle Photon, and names 
//variables that will be used later in the code.

void setup() {
    pinMode(led,OUTPUT);
    pinMode(power,OUTPUT);
    pinMode(ph1, INPUT);
    pinMode(ph2, INPUT);
    
    digitalWrite(power,HIGH);
    
     Particle.variable("LightKitchen", &LightKitchen, INT);
     Particle.variable("LightLiving", &LightLiving, INT);
    
    Particle.subscribe("LightsLog", Lightflip, "420038000c47363433353735");
    
}

//This section of the code defines how each pin used will function as well as
//well as the variables used to establish light level in the kitchen section and living section of 
//the room.  It also turns on the power supply to the photoresistors.

void Lightflip (const char *event, const char *data){
    if (strcmp(data,"On")==0) {
    
    Darker=false;
    Particle.publish("DarkStatus", "Darker=false");
  }
  else if (strcmp(data,"Off")==0) {
    
    Darker=true;
    Particle.publish("DarkStatus", "Darker=true");
  }
  else {
    
  }
}

void loop() {
    
   int loopdelay=2000;
   delay(loopdelay);
   int gap=300;
    int In1 = analogRead(ph1);
    int Out1= analogRead(ph2);
    delay(gap);
    int In2= analogRead(ph1);
    int Out2= analogRead(ph2);
    delay(gap);
    int In3= analogRead(ph1);
    int Out3= analogRead(ph2);
    delay(gap);
    int In4= analogRead(ph1);
    int Out4= analogRead(ph2);
    delay(gap);
    int In5= analogRead(ph1);
    int Out5= analogRead(ph2);
    delay(gap);
    LightKitchen=(In1+In2+In3+In4+In5)/5;
    LightLiving=(Out1+Out2+Out3+Out4+Out5)/5;
    //This section takes the average of 5 readings over 1.5 seconds for each of the photosensors to 
    //get a more accurate measurement of the light level in each part of the room.
   
    
    
    
   if (LightLiving < LightKitchen){
       if (Darker==true){
        Particle.publish("On125", String(On));              //Publishes the event that will cause the 
                                                            //other photon turn on the Living Room section Light Switch
        digitalWrite(led,HIGH);                             //Turns on Indicator LED
        Particle.publish("LightKitchen", String(LightKitchen));   //Publishes the event that allows a webhook 
                                                            //to get data from the inside photosensor
                                                            //to ThingSpeak graph
        delay(16000);
        Particle.publish("LightLiving", String(LightLiving)); //Publishes the event that allows a webhook 
                                                            //to get data from the photosensor outside
                                                            //to ThingSpeak graph
        }
        
        
    }
    else {
        if (Darker==false){
        Particle.publish("Off125",String(Off));             //Publishes the event that will cause the 
                                                            //other photon to begin opening the blinds
         digitalWrite(led,LOW);                             //Turns off Indicator LED
         delay(16000);
         Particle.publish("LightKitchen", String(LightKitchen));  //Publishes the event that allows a webhook 
                                                            //to get data from the Kitchen photosensor
                                                            //to ThingSpeak graph
        Particle.publish("LightLiving", String(LightLiving)); //Publishes the event that allows a webhook 
                                                            //to get data from the photosensor in the 
                                                            //living room section to ThingSpeak graph
        }
        
    }
        
}
    
//This loop checks the analogue value of the photoresistor in the kitchen section and compares it to the
//analogue value of photoresistor on the side of the counter measuring the living room light leel.  Then, 
//depending on whether the light level is higher inside or outside, the Particle publishes an event to the 
//console, with the event reading either "On" or "Off."

Servo Control

C/C++
Code to drive the servo controlling the 2nd light switch
//ServoLightSwitch
// Connect RED wire to VIN (~5V)
// Connect ORANGE, YELLOW, or WHITE wire to A0 (servo signal)
// Connect BLACK or BROWN wire to GND (0V)
// Adjust these connections for your particular servo
// if you have a wiring diagram for it
//----------------------------------------------------------------------------

Servo myservo;
                // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
int led= D7;
int angle=0;
void setup()
{ 
    Particle.subscribe("Off125", turnoff, "43003a000547363332363639");
    Particle.subscribe("On125", turnon, "43003a000547363332363639");
    
    pinMode(led,OUTPUT);
    
    myservo.attach(D0);
    myservo.write(10);
    angle=myservo.read();
    Particle.publish("Servo_Start",String(angle));
  
  // register the Spark function
  Spark.function("servo", updateServo);
  }
int updateServo(String command)
{
  // convert string to integer
  uint8_t pos = command.toInt();
  
  
}

void turnoff(const char *event, const char *data)
{
    digitalWrite (led, HIGH);
    int angle_start=myservo.read();
    pos=15;
    myservo.write(pos);
    Particle.publish("LightsLog","Off");
    angle=myservo.read();
    delay(16000);
    Particle.publish("Servo_Position",String(angle_start));
    delay(16000);
    Particle.publish("Servo_Position",String(angle));
    
}
void turnon(const char *On125, const char *On)  
{
    digitalWrite (led, LOW);
    int angle_start=myservo.read();
    pos=120;
    myservo.write(pos);
    Particle.publish("LightsLog","On");
    angle=myservo.read();
    delay(16000);
    Particle.publish("Servo_Position",String(angle_start));
    delay(16000);
    Particle.publish("Servo_Position",String(angle));
  }

Credits

Tyler Thompson

Tyler Thompson

1 project • 0 followers
Thomas Lawe

Thomas Lawe

0 projects • 0 followers
Mechanical Engineering Student at UNC Charlotte, currently enrolled in MEGR 3171

Comments