I have made an alarm using DS3231 RTC and TM1637 Display
I'm using the RTClib library
Everything appears to work fine.
The clock keeps time, I can set an alarm, the led lights when the alarm is set, I can cancel the alarm or it will time out and cancel itself and when it is canceled the led goes out.
Exactly what I want
However, evrey day at midnight an alarm goes off without me setting anything and without the led on. This happens even if another alarm is set and the led is on
I have tried everything I can think of.
I have changed out hardware. I have even tried removing the batery from the RTC incase there is something in memory that hasn't cleared
I have examined the code to see if I have accidentally set a second alarm which doesn't appear to be the case but, no matter what I try it still behaves same.
Could some kind person please take a look at it for me, help put an end to my sleepless nights, and tell me what I have missed or done wrong.
(if you look at the code you will see when I initiated this clock ------ I have been trying to sort this problem out since it woke me up on the first night lol)
#include <Wire.h>
#include "RTClib.h"
#include <TM1637Display.h>
RTC_DS3231 rtc;
#define CLK 8
#define DIO 9
#define ldr A1 //input for light sensor not setup yet but will use this to dim the display
#define led 6 //Alarm set warning light
TM1637Display display(CLK, DIO);
const uint8_t blank[] = {0x00, 0x00, 0x00,0x00};
int setButton = 2; // pushbutton for setting alarm
int hourButton = 3; // pushbutton for hour
int minButton = 4; // pushbutton for minutes
int exitButton = 5; // pushbutton for exit of set alarm
int buzzer = 13;
int t, a, Hour, Min, h, m;
int set_time, alarm_time, auto_alarmStop, set_alarm_min, set_alarm_hour;
int setButtonState = 0; // pushbutton state for setting alarm
int hourButtonState = 0; // pushbutton state for hour
int minButtonState = 0;// pushbutton state for minutes
int exitButtonState = 0; // pushbutton state for exit of set alarm
int activate;
void setup() {
Serial.begin(9600); // Begin serial communication at a baud rate of 9600:
delay(3000); // Wait for console opening:
if (! rtc.begin()) { // Check if RTC is connected correctly:
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) { // Check if the RTC lost power and if so, set the time:
Serial.println("RTC lost power, lets set the time!");
// 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
// July 02, 2020 at 3:47am you would call:
//rtc.adjust(DateTime(2020, 7, 02, 03, 47, 0));
}
Wire.begin();
display.setBrightness(7);
pinMode(setButton, INPUT_PULLUP);
pinMode(hourButton, INPUT_PULLUP);
pinMode(minButton, INPUT_PULLUP);
pinMode(exitButton, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
pinMode (led, OUTPUT);
// pinMode(ldr, INPUT); //used to read light sensor
activate = 0;
rtc.begin();
//rtc.adjust(DateTime(2021,5,14,10,27.00));
}
void loop() {
DateTime now = rtc.now();
Hour = now.hour();
Min = now.minute();
t =(now.hour()* 100 )+ now.minute();
a=now.second()%2;
display.showNumberDecEx(t,(0x80>>1), true);
delay(500);
display.showNumberDecEx(t,(0x80>>2), true);
delay(500);
switch(activate){
case 0:
display.showNumberDec(t, true);
setButtonState = digitalRead(setButton);
if(setButtonState == LOW){ delay(50); activate = 1;}
if(t == alarm_time && now.second()==0){activate = 2;}
break;
case 1:
display.setSegments(blank);//clear display
display.showNumberDec(0, true);
while(activate == 1){
hourButtonState = digitalRead(hourButton);
if(hourButtonState == LOW){
h++;
edit();
set_alarm_hour = h;
delay(200);
}
minButtonState = digitalRead(minButton);
if(minButtonState == LOW){
m++;
edit();
set_alarm_min = m;
delay(200);
}
exitButtonState = digitalRead(exitButton);
if(exitButtonState == LOW){delay(50); activate = 0;}
}
alarm_time = (set_alarm_hour*100)+set_alarm_min;
digitalWrite(led,HIGH);
break;
case 2:
alarm();
display.showNumberDec(t, true);
auto_alarmStop = alarm_time+1; //Auto stp after (# min)
exitButtonState = digitalRead(exitButton);
if(exitButtonState == LOW){delay(50); activate = 0; digitalWrite(led,LOW);}
if(t == auto_alarmStop){activate = 0;
digitalWrite(led,LOW);}
display.showNumberDec(t, true);
break;
}
delay(5);
}
void edit(){
if(m==60){m=0; h++;}
if(m<0){m=59; h--;}
if(h==24){h=0;}
if(h<0){h=23; m=59;}
set_time = (h*100)+ m;
display.showNumberDec(set_time, true);
}
void alarm(){
digitalWrite(buzzer,HIGH);
delay(75);
digitalWrite(buzzer,LOW);
delay(75);
digitalWrite(buzzer,HIGH);
delay(75);
digitalWrite(buzzer,LOW);
delay(75);
digitalWrite(buzzer,HIGH);
delay(75);
digitalWrite(buzzer,LOW);
delay(300);
digitalWrite(buzzer,HIGH);
delay(200);
digitalWrite(buzzer,LOW);
delay(100);
digitalWrite(buzzer,HIGH);
delay(200);
digitalWrite(buzzer,LOW);
delay(100);
digitalWrite(buzzer,HIGH);
delay(200);
digitalWrite(buzzer,LOW);
delay(300);
digitalWrite(buzzer,HIGH);
delay(75);
digitalWrite(buzzer,LOW);
delay(75);
digitalWrite(buzzer,HIGH);
delay(75);
digitalWrite(buzzer,LOW);
delay(75);
digitalWrite(buzzer,HIGH);
delay(75);
digitalWrite(buzzer,LOW);
delay(300);
}
TIA