Modified Simple RTC Reader

Le Sun 08 December 2013

This sketch has taken the Simple RTC Reader Sketch that is available pretty much with the RTC Library if I recall correctly that is provided as an example with the Time library and calls on a couple of other commands to extract the day of the week from the output from the RTC. In other words most of this code is not mine, it has just been noted all over and shuffled a little bit to suit my needs.

char  *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

Sets up a lookup list that will provide the names of the days given the weekday number extracted from the RTC output.

  Serial.print(Day[weekday()]);

This sends the day of the week from the Day[] lookup to the serial port for viewing. The weekday() command is used to extract the weekday from the RTC output.

The code also calls on the TimeAlarms Library to introduce events. In this case an LED on pin 13 is toggled on and off based on a repeating alarm generated every fifteen seconds. The Toggle boolean variable keeps track of the status of the LED. The output to the LED will ultimately become our Hot Water Cylinder control signal.

Related Project Pages

The extra libraries required for this sketch are:

You can also download the libraries from the following webpage: A Collection of Arduino Libraries Used in This Project.

The sketch below can be downloaded from here: TimeRTC_mod.ino

The code

/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * 
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <TimeAlarms.h> // this library will allow us to set alarms - ultimately these alarms will be read from the SD Card
// We will design this project to use alarms and durations of the on-condition rather than on-alarms and off-alarms.This will
// maximise our ability to have multiple alarms running.  We can use the control coding from the spreadsheet to calculate the duration if need be.
// No adjustments will be made for daylight saving.
// We may re-use an AlarmOnce type of feature to control durations. 


char  *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};  // Lookup list for day names
int LEDpin = 13;    // LED connected to digital pin 13 this is our substitute output signal that will ultimately become the HWC switcher
boolean Toggle = false;    //Introduce the controlling switch.  This will be used to track the status of the switch


void setup()  {
  pinMode(LEDpin, OUTPUT); // Initialise the pin as output.
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  Alarm.timerRepeat(15, Repeats);            // timer for every 15 seconds - calls Repeats()
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop()
{
   digitalClockDisplay();  
   Alarm.delay(1000);
   if (Toggle == true) {
    digitalWrite(LEDpin, HIGH); // set the LED on
    }
   else {
    digitalWrite(LEDpin,LOW);
  }
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(Day[weekday()]);
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void Repeats(){
  Serial.println("15 second timer"); 
  //Look at the Toggle to see what status it is and toggle it to the other state.
  Toggle = !Toggle; //The should switch the value - "!" means NOT
}

Par Hamish Trolove, Cat├ęgorie : Tech Projects

Tags : Arduino /