It happens to the best of us, working so long that the room gets dark around you without getting up to turn the lights on. What if the lights turned on when it got dark enough? I am glad you asked: that's exactly what this project is supposed to do.
Using two servos affixed to the top and bottom of my light switch and a photoresistor, the Particle Argon can tell how bright it is in my room and turn the lights on if it is too dark.
Setting Up the ServosThis first step in the project was probably the most annoying. Calibrating the servos is an arduous process.
First, the one-side servo horn is attached to the servo. Using command strips, one servo is affixed to the top and bottom ends of the lightswitch. In order to turn the lights on and off, the servos need to be told to only move enough but not to move too little. This is where it can get a little frustrating. Using the code below, I tested different values for where the servo needs to move. Given the range that each servo had, I ended up switching the servos multiple times and was constantly tweaking the position values. It was a long trial-and-error process.
Servo top; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo bottom;
int pos = 0; // variable to store the servo position
boolean switchState = 1;
void setup() {
top.attach(2);
bottom.attach(4);
top.write(150);
bottom.write(0);
}
void loop() {
for (pos = 0; pos <= 50; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
bottom.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 50; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
bottom.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 150; pos >= 100; pos -= 1) { // goes from 180 degrees to 0 degrees
top.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 100; pos <= 150; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
top.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
This code is from when I finally got all the values correct.
Controlling with AlexaTo be able to toggle the lights with my Amazon Echo, I used IFTTT to publish an event.
While the servos were the most tedious part of this project, integrating the light sensor took the most work. The Argon had already been using it to determine if the light switch toggle had actually changed the state of the lights, but the specific values were not too important for that function nor could I know what the values actually were. In order to determine when to turn the lights on, I needed to establish a baseline.
If you have a serial monitor, use that. However, the serial monitor on my computer broke. So how was I to determine the value for when it was dark enough for me to want the lights? Using Particle's cloud capabilities, IFTTT, and Tumblr, I created an online (albeit laggy) serial monitor alternative. This also meant that I did not need to have the Argon plugged into my computer when I did this; the Argon could be in its spot by the light switch.
Now that the trigger value had been established, the photoresistor needed code to turn the lights on when the light goes below that level. The photoresistor should only turn the lights on if the lights are off when the light level is low. But how would it know if the lights were on or off? When the device boots up, it toggles the lights to determine where the light switch is in relative to the servos and if the lights are on or off. This action is stored in a function that can be called at any time if the other light switch in my room is used. Additionally, the light sensor is used to detect if the lights changed when triggered and if not, to trigger the lights to be toggled again.
Director OverrideSometimes I want the lights off in my room even though it will be dark. The Argon, however, does not know my aesthetic desires. When director override is enabled, the device will only perform actions that were triggered by an event or an IFTTT function call. Director Override is toggled by running a particle cloud function that is connected to Alexa in IFTTT.
Another override on this device is Sleep Override. Unless Director Override is on, the device will not react to darkness during the hours in which I am supposed to be sleeping.
Demonstration
Comments
Please log in or sign up to comment.