Motion Coded Light Schematic
Motion Coded Light Arduino Sketch
//The following Arduino code for the motion coded light is totally open for anybody to use for anythingint tog = LOW; //variable for toggling the light on or off
const int val = 350; //value for tracking if IR switch has been tripped
void setup() {//only setup code is to set digital pin to output
pinMode(9, OUTPUT); //Using digital pin 9 to control relay
}
void loop() {
if (analogRead(A5) < val) { //look for object in front of sensor (switch is tripped)
for (int i=0; i<14; i++) { //check for object to move out of sensor range within 420 ms
delay(30);
if (analogRead(A5) >= val) { //look for object to move from sensor
for (int j=0; j<14; j++) { //give the object 420 ms to move back in front of sensor
delay(30);
if (analogRead(A5) < val) { //look for object in front of sensor
int activate = HIGH; //variable to track if the light should be turned off or on
for (int c=0; c<100; c++) { //ensure the object stays in front of sensor for 1 second
delay(10); //delay 10 ms 10o time for 1 sec total
if (analogRead(A5) > val) { //if object is not in front of sensor for 1 sec break out of loop and do not toogle switch
activate = LOW; //set variable to not toogle switch
break; //break out of loop
}
}
if(activate == HIGH) { //if variable is high toogle light switch
tog = toggleSwitch(tog); //toggle switch value so opposite action is taken next time
digitalWrite(9,tog); //write value to digital pin to open or close relay
delay(1500); //delay 1.5 seconds so switch is not unintentionally toogled again
}
break;
}
}
break;
}
}
}
delay(100);
}
//function toggles variable that controls switch position
int toggleSwitch(int t) {
if(t==LOW) { return HIGH; }
else { return LOW; }
}
No comments:
Post a Comment