My alarm clock project is working but at the wrong times

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:

#include <TimeAlarms.h>
#include <Time.h>
#include <Servo.h>

Servo myServo;
int angle = 0;

void setup()
   {
   Serial.begin(9600);   
   
   setTime(10,30,0,17,3,14);
   myServo.attach( 9 );
   Serial.println( "Setting servo to 0" );
   myServo.write( angle );   
//   Alarm.alarmRepeat( dowMonday, 5, 50, 0, wakeMeUp );
//   Alarm.alarmRepeat( dowTuesday, 5, 50, 0, wakeMeUp );
//   Alarm.alarmRepeat( dowWednesday, 5, 50, 0, wakeMeUp );
//   Alarm.alarmRepeat( dowThursday, 5, 50, 0, wakeMeUp );
//   Alarm.alarmRepeat( dowFriday, 5, 50, 0, wakeMeUp );

   } // end setup
   
void wakeMeUp()
   {
   Serial.println( "Starting Alarm" );
   
   for( angle = 0; angle <= 50; angle++ )
      {
      myServo.write( angle );
      delay( 10 );  
      } // end for
      
   for( angle = 50; angle <= 180; angle++ )
      {
      myServo.write( angle );
      delay( 13846 );  // 13846
       } // end for
       
   delay( 900000 );  
   
   for( angle = 180; angle >= 0; angle-- )
      {
      myServo.write( angle );
      delay( 10 );  
      } // end for
   } // end Alarm

void loop()
   {
   if( weekday( now() ) > 1 && weekday( now() ) < 7 )
      {
      if( hour( now() ) == 5 && minute( now() ) == 55 )
         {
         wakeMeUp();
         } // end if  
      } // end if
      
   digitalClockDisplay();
   Alarm.delay( 1000 ); // wait one second before display 
   } // end loop
   
void digitalClockDisplay() 
   { 
   Serial.print(hour()); 
   printDigits(minute()); 
   printDigits(second()); 
   Serial.println(); 
   }
   
void printDigits(int digits) 
   { 
   Serial.print(":"); 
   if(digits < 10) 
       Serial.print('0'); 
   Serial.print(digits); 
   }

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. :slight_smile:

How do I get the loop to constantly check the time and only set off the alarm at 5:55 AM?

Set your RTC to report time in 24 hour format, not 12 hour format.
Or, pay attention to the AM/PM setting.

And secondly, how can I structure the program so that the alarm only goes off on weekdays without my regular adjustment of the code?

Get the day of the week from the RTC.

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.

mistergreen:
You have to use the RTC. Time kept by the arduino will drift.

Would this mean, if with RTC, it can stay updated even if it is not connected to a computer?

mistergreen:
You can use your computer time too if it's connected and some app to send the time to the ardiuno.

As I do not have an RTC, please point me out to the right direction of which app I can use.

Thank you.

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?

Please let me know. Thanks for the help.

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.

Thank you so much for the great inputs.

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.

Again, THANK YOU LAR3RY and MEM.

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.