Using RTC module to control LED

Hello!

I have recently been attempting to use an RTC module which has the DS1302 chip to control a signal to an LED. I'd like to send a signal at a specific time of day, every day for 2 hours. I have so far used this code:

// Program: Date and time with RTC DS1302 module 
// Amendment and adaptation: Arduino and Cia 
// 
// Based on the original program Krodal and virtuabotixRTC library

// Load the virtuabotixRTC library 
#include <virtuabotixRTC.h>           

// Determine the pins connected to the module 
// myRTC (clock, data, RST) 
virtuabotixRTC myRTC (6, 7, 8);

void setup ()  
{      
  Serial.begin 
  (9600); // date and time of initial Information 
  // After to set the entire information, comment the following line 
  // (seconds, minutes, hours, day of week, day of month, month, year) 
  myRTC.setDS1302Time (50, 01, 18, 4, 9, 10, 2015);
}

void loop ()  
{
  // Reads the information from the CI
  myRTC.updateTime (); 

  // Print the details in serial monitor 
  Serial.print 
  ("Data"); // Call the routine that prints the day of the week
  imprime_dia_da_semana (myRTC.dayofweek);
  Serial.print (",");
  Serial.print (myRTC.dayofmonth);
  Serial.print ("/");
  Serial.print (myRTC.month);
  Serial.print ("/");
  Serial.print (myRTC.year);
  Serial.print ("");
  Serial.print 
  ("Time"); // Adds a 0 if the time value is <10 
  if (myRTC.hours <10)
  {
    Serial.print ("0");
  }
  Serial.print (myRTC.hours);
  Serial.print 
  (":"); // Adds a 0 if the value of the minutes is <10 
  if (myRTC.minutes <10)
  {
    Serial.print ("0");
  }
  Serial.print (myRTC.minutes);
  Serial.print 
  (":"); // Adds a 0 if the value of the latter is <10 
  if (myRTC.seconds <10)
  {
    Serial.print ("0");
  }
  Serial.println (myRTC.seconds);

  delay (1000);
}

void imprime_dia_da_semana (int day)
{
  switch (day)
  {
    case  1:
    Serial.print 
    ("Sunday"); 
    break; case  2:
    Serial.print 
    ("Second"); 
    break; case  3:
    Serial.print 
    ("Terca"); 
    break; case  4:
    Serial.print 
    ("Wednesday"); 
    break; case  5:
    Serial.print 
    ("Quinta"); 
    break; case  6:
    Serial.print 
    ("Friday"); 
    break; case  7:
    Serial.print 
    ("Saturday"); break;
  }



  
}

Which allows me to read the time in the serial monitor, but I am little stuck for how to read the serial monitor in order to send a signal. I know it will be something I need to write in the void loop...

I was thinking something similar to this might help:
http://forum.arduino.cc/index.php?topic=143730.0

but neither of the inputs that were suggested for the void loop seemed to work

so far the visual set-up looks like this (obviously without the LED in place yet):

any comments/advice much appreciated!

cheers

You're printing the variables; surely you can make a decision whether to write an output High or Low based on the same variables?

i.e. save them as an initial step, print them, make a decision based on them
vs just calling them and printing the returned data directly.

Ah I see what you're saying. So if for example I introduce the LED as pin 13 saying pinMode(13, OUTPUT); at the beginning of the sketch, I could write in the void loop:

 if (myRTC.hours 18);
 digitalWrite (13, HIGH);

or am I being too naïve?

if (myRTC.hours 18);

This definitely won't work. You need to have == or > or <, etc, in the condition, and no ';' at the end or the statement does nothing.

You probably mean

if (myRTC.hours == 18)

I see, I see.

So no I've tried:

if (myRTC.hours == 18);
      (myRTC.minutes == 02);
   digitalWrite (13, HIGH);
   delay (5000);

in an attempt to send a signal to the LED for 5 seconds at 18.02, however the LED doesn't seem to be responding, though it does flash momentarily on the 18.00 mark I think

am now also having the problem that the serial monitor is telling me it is the original time I set the RTC even though I have tried changing the time and re-uploading the code a bunch of times...

if ((myRTC.hours == 18) && (myRTC.minutes == 02))
{
   digitalWrite(13, HIGH);
   delay (5000);
   digitalWrite(13, LOW);
}

I would also recommend not using delay() if you want something else to be happening while the delay is on.

great, that's so helpful thank you.
i've now got:

   if ((myRTC.hours == 18)) 
{
   digitalWrite(5, HIGH);

}
   else 
{
   digitalWrite(5, LOW); 
}

how do i adjust the ((myRTC.hours == 18)) to make sure the pin is on between 18.00 and 20.00? do i need to use > and < ?

cheers!

if ((myRTC.hours >= 18) && (myRTC.hours < 20))
{
   digitalWrite(5, HIGH);
}
   else 
{
   digitalWrite(5, LOW); 
}

I think it may be time for you to read up on conditional statements in C/C++.

Thanks so much for the help. I now need to the signal to be sent between both 8-10pm and 06-08am. To do this I guess I need an || (OR statement?)

The sketch didn't seem to approve of this

if ((myRTC.hours >= 18) && (myRTC.hours < 20)) || ((myRTC.hours >= 18) && (myRTC.hours < 20)); 
{
   mp3_play(1); // plays the track "mp3/0001.mp3" once
      
      mp3_play(2); // plays the track "mp3/0002.mp3" once
      
}
   else 
{
 mp3_stop();
}

Look at your semi colons again.

I hear you.

if ((myRTC.hours >= 18) && (myRTC.hours < 20)) || ((myRTC.hours >= 06) && (myRTC.hours < 08))

but now it is giving me

error: expected `;' before '{' token

Cannot tell from your snippet but you have probably left something out earlier in the code.

aha! nope it's working fine now I just didn't need to be using the full 24h clock, didn't need the '0' before the single digit hours...

now have:

if ((myRTC.hours >= 18) && (myRTC.hours < 20) || (myRTC.hours >= 6) && (myRTC.hours < 8)