Alarm clock with DS1307

Hi,

I am trying to write a code that will open a box lid using a servo motor while setting off a buzzer at the same time each morning. I am working off of an alarm example by Paul Stoffragen: (TimeAlarms/TimeAlarmExample.ino at master · PaulStoffregen/TimeAlarms · GitHub)

I'm using an Arduino Uno, a DC buzzer, DS1307 RTC, SG90 MicroServo, powering the board from USB. I have the IDE 1.8.13 and am on Mac OS 10.14.06. I also tried the same code on a windows machine, and it made no difference.

I have successfully opened the lid and set off the buzzer using an ultrasonic sensor, but I am now having trouble figuring out the RTC so that it can happen at the same time each morning. I have used the "ReadTest" example sketch to confirm that the RTC has the correct time, but the MorningAlarm function does not seem to get triggered in the code below (I cannot get the servo and buzzer to activate). I have been testing this by setting the time in this line directly below to be a minute after I upload:
Alarm.alarmRepeat(2, 27, 0, MorningAlarm); // want to trigger MorningAlarm function 7:00am every day

The goal: At 7am everyday, the buzzer goes off followed by the servo arm moving from vertical (0) to 120 degrees to open the lid, wait a few minutes, then reset back to vertical (0).

I has used Arduino a little bit in school, but I've always had fairly detailed instructions to work off of, so I am definitely new.

Any and all suggestions are appreciated, thank you!
Alex

*/
#include <DS1307RTC.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <Servo.h>

Servo servo;

int const buzzPin = 7;
int const servoPin = 13;
int const DSPin = A4;
int const SCLPin = A5;

void setup() {

  Serial.begin(9600);
  // wait for Arduino Serial Monitor
  while (!Serial) ;

  // prepare pin as output
  servo.attach(servoPin);
  pinMode(buzzPin, OUTPUT);

  // get and set the time from the RTC
  setSyncProvider(RTC.get);
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  // to test your project, you can set the time manually
  //setTime(6,59,0,28,3,21); // set time to Saturday 8:29:00am Jan 1 2011

  // create the alarms, to trigger functions at specific times
  Alarm.alarmRepeat(7, 0, 0, MorningAlarm); // triggers MorningAlarm function 7:00am every day
}
//
void loop() {
  //  digitalClockDisplay();
  //  // wait one second between each clock display in serial monitor
  //  Alarm.delay(1000);
}

// functions to be called when an alarm triggers
void MorningAlarm() {
  // write here the task to perform every morning
  digitalWrite(buzzPin, HIGH);
  delay(1500);
  digitalWrite(buzzPin, LOW);
  servo.write(120);
  delay(240000);
  servo.write(0);
}
  //  Alarm.delay(1000);

TimeAlarms will not work with this line commented out because it is this function that checks whether an alarm should be triggered

Thank you so much! It works now :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.