I'm a college student and upon moving into my dorm a few months ago it occurred to me that it might be cool to set-up an automated blinds system. I bought an extra UNO and powerful continuous rotation servo for the build.
To put it simply I need the servo to move for a few seconds in the morning, then a few hours later reverse for a few seconds (an continue daily).
I initially tried to use the Time.h and TimeAlarms.h libraries for the timing but my lack of experience with them foiled that plan. I then went on to attempt to build by own timing function (you can see that below) using a variable and delay command.
As it stands, the timing doesn't seem to work properly as my variable doesn't appear to update with the new val. EG) servo operates every min, instead of once upon startup.
#include <Servo.h>
Servo Blinds;
int x = 0;
int time = 0;
void setup() {
Blinds.attach(9);
}
void loop() {
if(time == 1) { //Test to be run upon start-up
here:
if (x < 20) {
Blinds.write(0);
delay(1000);
++x;
goto here;
}
else {
Blinds.write(93);
x = 0;
delay(60000)
}
}
if(time == 390) { //6:30 AM Blinds OPEN
here1:
if (x < 20) {
Blinds.write(0);
delay(1000);
++x;
goto here1;
}
else {
Blinds.write(93);
}
}
if(time == 660) { //11:00 AM Blinds CLOSE
here2:
if (x > 0) {
Blinds.write(180);
delay(1000);
--x;
goto here2;
}
else {
Blinds.write(93);
}
}
if(time == 1440) { //Daily Reset
time = 0;
}
delay(60000); //Timing Func.
++time;
}
Please use descriptive variable names. x and t don't describe anything. Using extra letters in your program doesn't make the Arduino run slower but it does make it slower to program.
This doesn't do what you think it does:
if(t < 2, t > 0) {
The comma operator returns the result of the second part of the expression. The first part is basically ignored. I guess you are looking for the and operator && or perhaps the or operator|| ?
Thanks. I have some experience in other languages and goto is also not favorable, but it seemed like a simple solution to by looping problem. This code essentially can be as rough as possible, as long as it works.
MorganS, I hope I addressed your issues enough.
Does anyone have an idea how to get the servo to halt completely in between the activated times. Currently it rotates for about six seconds every ten-fifteen min. As can be noted in the code, the servo is already set to write(93), from 90.
rfdpro:
I'm a college student and upon moving into my dorm a few months ago it occurred to me that it might be cool to set-up an automated blinds system. I bought an extra UNO and powerful continuous rotation servo for the build.
To put it simply I need the servo to move for a few seconds in the morning, then a few hours later reverse for a few seconds (an continue daily).
Dude, I did exactly this project.
Instead of using a timer, I used a LDR (light dependent resistor) to open the blinds when it got light. (I want them closed at night because annoying streetlight). The arduino on-board oscillator is, like, ±5%, and that's potentially an hour over the course of a day. So using the timer to time events really won't work that well. That's why people use external real-time clocks (RTC) for that kind of thing.
The basic thing you need to get your head around is not using delay. Ever. Instead, each time through the loop the arduino needs to ask "Is there anything I need to do now?", and do it.
Look at the "blink without delay" example and grok it. Alter it so that the LED blinks the way you want it to.
You can look at my code too. It's in the final post of my "machine" posts. But my code is complicated because I'm a programmer and tend to overengineer things.