Help needed with RTC1307 Code :)

Hello! I have a task that i suspect is very easy to solve for someone who knows how to code :slight_smile: I work in Sweden as an educator, and i am developing a sort of TEACCH time-aid for people with autism. The idea is that different things need to be triggered at different times during the day, and so i have found myself dragged into the world of coding. Which i dont know much about. Ive got an Arduino Nano, a RTC1307, and i have learned to set the time after the computer time, using Paul Stoffregen "Settime.ino".
Now ive tried to learn enough about coding to be able to, lets say, have an LED turn ON at 06:00 (day begins, get up!), another LED turns OFF (night-ligt off during day), another LED turns ON when its time to go to the taxi and go to work etc. Problem is, this needs to get done and im really struggling with the basics of coding...
My question is, is there a simple code i can use, copy and paste into Paul Stoffregens "Settime.ino" that allows me to specify and trigger events at certain times during the day? If someone could show me, it would make a big difference to an Autistic man here in Gothenburg, Sweden :slight_smile: / Daniel

1 Like
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(115200);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    //Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
   rtc.adjust(DateTime(2020, 9, 29, 12, 44, 15));
}

void loop () {
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    delay(100);
}

this is for my DS3231 module. look at example from library RTC1307. you will see similar commands. learn how it works.

The 1307 is far from the best RTC for you to use.

Suggest you get the DS3231.



That said, here is a 1307 sketch you can build on:

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

byte lastSecond;

//***************************************************************************
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }

  //run this once to set the RTC then comment the next line
  //rtc.adjust(DateTime(__DATE__, __TIME__));

  // This line sets the RTC with an explicit date & time, for example to set
  // May 21, 2015 at 6pm you would call: (use 24 hour time)
  //                  YYYY MM  DD  HH MM SS
  //rtc.adjust(DateTime(2021, 6, 10, 10,28, 0));


  //Any pin. I have used Pin 4
  pinMode(4, OUTPUT);

} //END of setup()


//***************************************************************************
void loop()
{
  //retreve the time from the RTC
  DateTime now = rtc.now();

  if (lastSecond != now.second())
  {
    //update to the new second
    lastSecond = now.second();

    Serial.print(now.year(), DEC);
    Serial.print("/");
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(now.hour(), DEC);
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.print(now.second(), DEC);
    Serial.print(")");
    Serial.println();

    //The time is set as a 24 hour clock
    //Pin 4 get high at 6pm and low at 6am & 2 seconds
    if (now.hour() == 6 && now.minute() == 0 && now.second() == 0)
    {
      digitalWrite(4, HIGH);
    }

    else if (now.hour() == 6 && now.minute() == 0 && now.second() == 2)
    {
      digitalWrite(4, LOW);
    }
  }

} //END of loop()




Thanx! Ill dive into that!!

A slightly more polished version:

// Version  YY/MM/DD  Description
// 1.02     21/06/10  Working sketch

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

byte lastSecond;


//                              s e t u p ( )
//***************************************************************************
void setup()
{
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }

  //run this once to set the RTC then comment the next line
  //rtc.adjust(DateTime(__DATE__, __TIME__));

  // This line sets the RTC with an explicit date & time
  //                  YYYY MM  DD  HH MM SS
  //rtc.adjust(DateTime(2021, 6, 10, 10,28, 0));

  //any pin, pin 4 is used here
  pinMode(4, OUTPUT);

} //END of setup()

//                              l o o p ( )
//***************************************************************************
void loop()
{
  //retrieve the time from the RTC
  DateTime now = rtc.now();

  //has the second digit changed ?
  if (lastSecond != now.second())
  {
    //update to the new second
    lastSecond = now.second();

    Serial.print(now.year(), DEC);
    Serial.print("/");
    printDigits(now.month());
    Serial.print("/");
    printDigits(now.day());

    Serial.print(" ");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);

    Serial.print(" (");
    printDigits(now.hour());
    Serial.print(":");
    printDigits(now.minute());
    Serial.print(":");
    printDigits(now.second());
    Serial.print(")");
    Serial.println();


    //check to see if it is time for something to happen

    //**********************************
    //the time is set as a 24 hour clock
    //pin 4 goes high at 6 am and low at 6 am & 2 seconds
    if (now.hour() == 6 && now.minute() == 0 && now.second() == 0)
    {
      digitalWrite(4, HIGH);
    }

    //**********************************
    if (now.hour() == 6 && now.minute() == 0 && now.second() == 2)
    {
      digitalWrite(4, LOW);
    }

  }

} //END of loop()

//                           p r i n t D i g i t s ( )
//***************************************************************************
void printDigits(int digits)
{
  //utility function for digital clock display: prints leading 0
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);

} //END of printDigits(int digits)

//***************************************************************************



1 Like

Hello! Quick question! Im getting an error message, saying
'printdigits' was not declared in this scope

this is the code i'm running:

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
byte lastSecond;

int dayLED=2;
int nightLED=3;
int waitLED=4;
int taxiLED=5;
int BT=100;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
Wire.begin();
rtc.begin();

if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
}

rtc.adjust(DateTime(DATE, TIME));

pinMode (dayLED, OUTPUT);
pinMode (nightLED, OUTPUT);
pinMode (waitLED, OUTPUT);
pinMode (taxiLED, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:

//retrieve the time from the RTC
DateTime now = rtc.now();

//has the second digit changed ?
if (lastSecond != now.second())
{
//update to the new second
lastSecond = now.second();

Serial.print(now.year(), DEC);
Serial.print("/");
printDigits(now.month());
Serial.print("/");
printDigits(now.day());

Serial.print(" ");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);

Serial.print(" (");
printDigits(now.hour());
Serial.print(":");
printDigits(now.minute());
Serial.print(":");
printDigits(now.second());
Serial.print(")");
Serial.println();


//check to see if it is time for something to happen

//Night time
if (now.hour() == 22 && now.minute() == 0 && now.second() == 0)
{
digitalWrite(dayLED, LOW);
digitalWrite(nightLED, HIGH);
digitalWrite(waitLED, LOW);
digitalWrite(taxiLED, LOW);

//Daytime
if (now.hour() == 6 && now.minute() == 0 && now.second() == 0)
{
digitalWrite(dayLED, HIGH);
digitalWrite(nightLED, LOW);
digitalWrite(waitLED, LOW);
digitalWrite(taxiLED, LOW);
}

//Wait for Taxi
if (now.hour() == 7 && now.minute() == 0 && now.second() == 0)
{
digitalWrite(dayLED, LOW);
digitalWrite(nightLED, LOW);
digitalWrite(waitLED, HIGH);
digitalWrite(taxiLED, LOW);
}

//Go outside, Taxi arriving
if (now.hour() == 8 && now.minute() == 0 && now.second() == 0)
{
digitalWrite(dayLED, LOW);
digitalWrite(nightLED, LOW);
digitalWrite(waitLED, LOW);
digitalWrite(taxiLED, HIGH);
}

} //END of loop()

// p r i n t D i g i t s ( )
//***************************************************************************
void printDigits(int digits)
{
//utility function for digital clock display: prints leading 0
if (digits < 10)
Serial.print('0');
Serial.print(digits);

} //END of printDigits(int digits)

and its pointing me to this line:

printDigits(now.month());

Hi,
To add code please click this link;

Tom.... :smiley: :+1: :coffee: :australia:

You missed two braces:

}

every { needs a }

Your version is Not tested for functionality.

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
byte lastSecond;

int dayLED = 2;
int nightLED = 3;
int waitLED = 4;
int taxiLED = 5;
int BT = 100;

void setup()
{
  // put your setup code here, to run once:

  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }

  //run this once to set the RTC then comment the next line
  //rtc.adjust(DateTime(__DATE__, __TIME__));

  // This line sets the RTC with an explicit date & time
  //                  YYYY MM  DD  HH MM SS
  //rtc.adjust(DateTime(2021, 6, 10, 10,28, 0));  

  pinMode (dayLED, OUTPUT);
  pinMode (nightLED, OUTPUT);
  pinMode (waitLED, OUTPUT);
  pinMode (taxiLED, OUTPUT);

} //END of setup()

void loop()
{
  // put your main code here, to run repeatedly:

  //retrieve the time from the RTC
  DateTime now = rtc.now();

  //has the second digit changed ?
  if (lastSecond != now.second())
  {
    //update to the new second
    lastSecond = now.second();

    Serial.print(now.year(), DEC);
    Serial.print("/");
    printDigits(now.month());
    Serial.print("/");
    printDigits(now.day());

    Serial.print(" ");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);

    Serial.print(" (");
    printDigits(now.hour());
    Serial.print(":");
    printDigits(now.minute());
    Serial.print(":");
    printDigits(now.second());
    Serial.print(")");
    Serial.println();


    //check to see if it is time for something to happen
    //Night time
    if (now.hour() == 22 && now.minute() == 0 && now.second() == 0)
    {
      digitalWrite(dayLED, LOW);
      digitalWrite(nightLED, HIGH);
      digitalWrite(waitLED, LOW);
      digitalWrite(taxiLED, LOW);
    }

    //Daytime
    if (now.hour() == 6 && now.minute() == 0 && now.second() == 0)
    {
      digitalWrite(dayLED, HIGH);
      digitalWrite(nightLED, LOW);
      digitalWrite(waitLED, LOW);
      digitalWrite(taxiLED, LOW);
    }

    //Wait for Taxi
    if (now.hour() == 7 && now.minute() == 0 && now.second() == 0)
    {
      digitalWrite(dayLED, LOW);
      digitalWrite(nightLED, LOW);
      digitalWrite(waitLED, HIGH);
      digitalWrite(taxiLED, LOW);
    }

    //Go outside, Taxi arriving
    if (now.hour() == 8 && now.minute() == 0 && now.second() == 0)
    {
      digitalWrite(dayLED, LOW);
      digitalWrite(nightLED, LOW);
      digitalWrite(waitLED, LOW);
      digitalWrite(taxiLED, HIGH);
    }

  } //END of    if (lastSecond != now.second())

} //END of loop()

// p r i n t D i g i t s ( )
//***************************************************************************
void printDigits(int digits)
{
  //utility function for digital clock display: prints leading 0
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);

} //END of printDigits(int digits)