Christopher BrannonJoshua Smith
Published

Automatic Light Switch

Turn your light switch on and off from your phone.

IntermediateWork in progress997
Automatic Light Switch

Things used in this project

Hardware components

Argon
Particle Argon
×2
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×2
High Brightness LED, White
High Brightness LED, White
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×2
SG90 Micro-servo motor
SG90 Micro-servo motor
×2
Jumper wires (generic)
Jumper wires (generic)
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Fishing Line
×1

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver
Tape, Electrical
Tape, Electrical

Story

Read more

Schematics

Motion Sensor

Servos

Code

Servo

C/C++
int led = D7;

Servo rightServo;
Servo leftServo;


void setup() {
    
   

    rightServo.attach(D2);
    leftServo.attach(D3);
    
   
    
    pinMode(led, OUTPUT);
    digitalWrite(led, LOW);
    
  
    
  Particle.subscribe("lightSwitchOn", myHandler,"e00fce686eb6f38cd0da6697");
   
  Particle.function("lightSwitchoff", lightSwitchOff); 
  
  
  
  Particle.function("lightSwitchon", lightSwitchOn2);
  
  
 
  
}

int lightSwitchOff(String command)   
{                          
    if(command == "off")   
    {   
        digitalWrite(D7, HIGH); 
        delay(1000);
        rightServo.write(180);       
                                     
                                   
        leftServo.write(0);      
        
        digitalWrite(D7, LOW);  
         Particle.publish("lightSwitchOff","off");
           
    }
}
int lightSwitchOn2(String command)   
{                          
    if(command == "on")   
    {   
        digitalWrite(D7, HIGH); 
        delay(1000);
        rightServo.write(0);       
                                     
                                   
        leftServo.write(180);      
        
        digitalWrite(D7, LOW);  
        Particle.publish("lightSwitchOn2","on");
           
    }
}


void myHandler(const char *event, const char *data) {
    

        digitalWrite(led, HIGH);
        
        delay(1000);
       
        rightServo.write(0);
        
        leftServo.write(180);
        
       
   
  
        
        
        digitalWrite(D7, LOW);
        
   }
   
   void publish(){
        
       
   }

Motion Sensor

C/C++
int inputPin = D2;              
int ledPin = D7;                
int pirState = LOW;             
int val = 0;                    

int calibrateTime = 5000;      

void setup() {
    
    pinMode(ledPin, OUTPUT);
    pinMode(inputPin, INPUT);    
    
  Particle.subscribe("lightSwitchOn2", reportTheData1, "e00fce684e209ce7114d713a");  
  Particle.subscribe("lightSwitchOff", reportTheData2, "e00fce684e209ce7114d713a");  
 
}




void loop() {

  
  if (calibrated()) {
 
    readTheSensor();

   
    reportTheData();
    
   
    }
    
 
}
void reportTheData2(const char *event, const char *data){   

    
        
            
            Particle.publish("lightSwitchISOff", "off");

}
void reportTheData1(const char *event, const char *data){        
        
       
            
         Particle.publish("lightSwitchAlreadyOn", "on");
         
        
        
        
}




void readTheSensor() {
    val = digitalRead(inputPin);
}

bool calibrated() {
    return millis() - calibrateTime > 0;
}



void reportTheData() {
    if (val == HIGH) {

        if (pirState == LOW) {
          
        Particle.publish("lightSwitchOn", "on");
        
        pirState = HIGH;
        
        setLED(pirState);
        }
    } 
 else {
    if (pirState == HIGH) {
     
      pirState = LOW;
      setLED(pirState);
    }
  }
}
        

void setLED(int state) {
    digitalWrite(ledPin, state);
}

Credits

Christopher Brannon

Christopher Brannon

1 project • 0 followers
Joshua Smith

Joshua Smith

1 project • 0 followers

Comments