Search code examples
arduinoschedulerreal-time-clock

RTC + Scheduler using Arduino mini


I am using Arduino mini 5V for my project and RTC - Real Time Clock Module DS1307 I would like to wake the board at a certain time and run a function. (Buzzer is connected to D3)

When I use the TimeAlarm alone and manually set the time everything works fine:

#include "Time.h"
#include "TimeAlarms.h"

void setup(){
  setTime(22,29,55,12,31,14); // set time to Saturday 8:29:00am Jan 1 2011

  Alarm.alarmRepeat(10,30,0,buzz);  // 10:30am every day
  Alarm.alarmRepeat(16,30,0,buzz);  // 4:30pm every day 
  Alarm.alarmRepeat(22,30,0,buzz);  // 10:30pm every day 

  Serial.begin(9600);
}

void loop(){
  digitalClockDisplay();
  Alarm.delay(1000);
}

void buzz(){
   tone(3, 220, 1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits){
  Serial.print(":");
  if(digits < 10)
  Serial.print('0');
  Serial.print(digits);
}

However when I use RTC the buzzer function wouldn't get called, it still prints the time though

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

RTC_Millis rtc;

void setup(){
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));

  Alarm.alarmRepeat(10,30,0,buzz);  // 10:30am every day
  Alarm.alarmRepeat(16,30,0,buzz);  // 4:30pm every day 
  Alarm.alarmRepeat(22,30,0,buzz);  // 10:30pm every day -- modify this to your current time when running the example

  Serial.begin(9600);
}

void loop(){
  //printing the current time
  DateTime now = rtc.now();

  Serial.print(now.year());
  Serial.print('/');
  Serial.print(now.month());
  Serial.print('/');
  Serial.print(now.day());
  Serial.print(' ');
  Serial.print(now.hour());
  Serial.print(':');
  Serial.print(now.minute());
  Serial.print(':');
  Serial.print(now.second());
  Serial.println();
  Alarm.delay(1000); // wait one second between clock display
}

void buzz(){
  tone(3, 220, 1000);
}

Solution

  • OK so I found the answer:

    First problem was: using RTC_DS1307 instead of RTC_Millis

    RTC ds1307 is referring to pin 12C which in Arduino mini they are above A3 and VCC. They require soldering. Once that's done they should be connected to SDA and SCL, using M/F wires

    SDA -> to the pin above A3
    SCL -> to the pin above VCC
    

    Then I changed the code to the following:

    #include <Wire.h>
    #include "RTClib.h"
    #include "Time.h"
    #include "TimeAlarms.h"
    
    RTC_DS1307 rtc;
    const int output = 3;
    
    uint32_t syncProvider()//function which sets up the RTC as the source of external time{
      return rtc.now().unixtime();
    }
    
    
    void setup(){
      Wire.begin();
      rtc.begin();
      rtc.adjust(DateTime(__DATE__, __TIME__));//comment this out when the RTC has been set
      setSyncProvider(syncProvider);   // the function to get the time from the RTC
    
    
      Alarm.alarmRepeat(10,30,0,buzzer);  // 10:30am every day
      Alarm.alarmRepeat(16,30,0,buzzer);  // 4:30pm every day 
      Alarm.alarmRepeat(22,30,00,buzzer);  // 10:30pm every day 
    
      pinMode(output , OUTPUT);//new line
      Serial.begin(9600);
    }
    
    void loop(){
    
      //printing the current time
      DateTime now = rtc.now();
    
      Serial.print(now.year());
      Serial.print('/');
      Serial.print(now.month());
      Serial.print('/');
      Serial.print(now.day());
      Serial.print(' ');
      Serial.print(now.hour());
      Serial.print(':');
      Serial.print(now.minute());
      Serial.print(':');
      Serial.print(now.second());
      Serial.println();
      Alarm.delay(1000); // wait one second between clock display
    }
    
    void buzzer(){
     //Do Stuff
    }