So basically my project is supposed to slowly turn on my bedside lamp with a servo, and everything works properly when I set the time to a few seconds before the alarm is supposed to go off, which is 5:55 AM. The alarm takes 30 minutes to run its course before turning off, at which point I want it to run again at 5:55 AM. But when I set it as the actual time and let it work overnight, it doesn’t work at the time it’s supposed to; instead it goes off at another time, and I haven’t verified this but I’m pretty sure it goes off 12 hours after it’s supposed to. But before I go further, here’s my code:
Let me cut to the chase. How do I get the loop to constantly check the time and only set off the alarm at 5:55 AM? And secondly, how can I structure the program so that the alarm only goes off on weekdays without my regular adjustment of the code?Thanks in advance for all of your advice.
Sorry I forgot to clarify, I'm not using an RTC chip. I'm simply using the Time Alarm and Time libraries for the Uno. But I might consider adjusting the project to include an RTC chip if that's better?
jcapili:
Sorry I forgot to clarify, I’m not using an RTC chip. I’m simply using the Time Alarm and Time libraries for the Uno. But I might consider adjusting the project to include an RTC chip if that’s better?
You have to use the RTC. Time kept by the arduino will drift.
The arduino uses the RTC to correct itself every ‘x’ minutes. You can use your computer time too if it’s connected and some app to send the time to the ardiuno.
arcQuisumbing:
Would this mean, if with RTC, it can stay updated even if it is not connected to a computer?
Yes. The RTC is designed to be accurate over a long period of time, and has a built-in battery. The Arduino is not designed for accurate timekeeping, so in order to keep accurate time, it needs to have access to an accurate time source that can re-sync the time periodically. RTC, GPS, or PC, for example, will all serve this purpose.
As I do not have an RTC, please point me out to the right direction of which app I can use.
I don't know of any program that will send the current time to a COM port, but it is easy enough to write one in C++, VB.Net, Java, Python, Rexx, or virtually any other language you are comfortable with. If you don't have a compiler for a langauge you like, there are free versions available for many languages. If you are not familiar with any suitable language, I would recommend something like VB.Net Express Edition, and a site much like this one, where you can get advice and help with coding. I like http://www.dreamincode.net
lar3ry:
Yes. The RTC is designed to be accurate over a long period of time, and has a built-in battery. The Arduino is not designed for accurate timekeeping.......
How much inaccuracy in how much time?
I am asking since my project is a modification of Michael/Mem's HomeControlDailyTimer which also can be programmed to have a random variance in the actual start time. So if the inaccuracy is just few minutes to about an hour or two within one month, then it is not much of a problem for my use.
But what about power failure? What time will Arduino be on after restarting if it will be a standalone and no syncing? Will it reset to factory time, continue where it left off or restart to the last sync?
I couldn't really say how far it might drift, except to say that it would vary between boards. As well, it might also vary depending on the code it's running.
As for power off/on, everything resets, so it would depend entirely on the code you are running as to what time will come up after it powers on. It will do the same thing after any reset (like when you plug it into a PC with the USB connector). But really, the only reasonable way to ensure at least reasonable accuracy is a Real Time CLock. They will do what you want, and they are very low cost.
Unfortunately, I can only get hold of an RTC from eBay and it will take at least a 1.5 to 2 months to get here and I am bound to go for a vacation in 2 weeks time.
My project is to control some lights and some sound making appliances at approximate times of the day to emulate people inside the house.
I am a bit paranoid as we had a burglary last year.
Oh well, accuracy is not much of my concern. I'll just pray that there will be no power failure so hopefully the code will last for a month functioning properly.
Well, there's more than one way to sync, if you are only concerned with approximations.
If you have an alarm clock with a battery backup, you could set it to go off at a certain time, and detect the sound with a small microphone or a direct hookup to a speaker. It might require a small diode or two, or perhaps a transistor, but an analogRead could detect the sound.
You could use a phototransistor to detect sunrise and sunset, syncing on those times. You can find lots of online sunrise/sunset time calculators for your latitude/longitude.
Or, you could try your Arduino to see how much it drifts, and if the rate is within your tolerance, run it with a battery, making it sleep for most of the time.