So my teacher gave me his realization of a school bell (actually its more like a digital clock with an alarm). So basically if you want to make an alarm time you need to put it manually with pushbuttons. So my job is to remove that part of the user putting the alarm manually and replace it with a integrated ringing time. I am not really good at this so I would like some help with changing that if it is not a problem. Thank you in advance.
(most part of the code because all of the code cant fit here)
{
MAIN,
MENU_SET_ALARM,
SET_AL_HOUR,
SET_AL_MINUTE,
SET_AL_ON_OFF,
ALARM_TIME,
};
STATES state;
int8_t button;
int8_t trigger;
int Second;
int Minute;
int Hour;
int Day;
int Month;
int Year;
int8_t DoW;
String day_of_week;
unsigned char address, data;
int AL_Hour;
int AL_Minute;
bool alarm_active = false;
int alarm_out = 10;
uint32_t blink_interval = 500;
uint32_t blink_previousMillis = 0;
boolean blink_state = false;
boolean RTC_error = true;
boolean long_press_button = false;
void setup()
{
lcd.begin();
lcd.backlight();
Wire.begin(); // start I2C
pinMode(11,INPUT_PULLUP);
pinMode(alarm_out, OUTPUT);
lcd.createChar(0, thermometer_symbol);
lcd.createChar(1, bell_symbol);
lcd.createChar(2, char_S);
lcd.createChar(3, char_D);
lcd.createChar(4, char_J);
lcd.createChar(5, menu_RIGHT);
lcd.createChar(6, menu_LEFT);
Wire.begin();
setSyncProvider(RTC.get);
setSyncInterval(5);
RTC.squareWave(SQWAVE_NONE);
digitalWrite(SQW_PIN, HIGH);
pinMode(SQW_PIN, INPUT_PULLUP);
attachInterrupt(INT0, alarmIsr, FALLING);
lcd.clear();
lcd.setCursor(5, 0);
lcd.write(2);
lcd.setCursor(6, 0);
lcd.write(2);
lcd.setCursor(7, 0);
lcd.write(3);
lcd.setCursor(8, 0);
lcd.write(4);
lcd.setCursor(0, 1);
lcd.print("Arduino & DS3231");
delay(2000);
lcd.clear();
state = MAIN;
}
volatile boolean alarmIsrWasCalled = false;
void alarmIsr()
{
alarmIsrWasCalled = true;
}
void loop()
{
change_states();
check_inputs();
check_RTC();
if (alarmIsrWasCalled){
if (RTC.alarm(ALARM_1)) {
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Wake up!!!");
lcd.setCursor(1,1);
lcd.print("Wake up!!!");
for(int i=0;i<25; i++){
buttonbtnBACK.read();
if (buttonbtnBACK.wasPressed())
{
alarmIsrWasCalled = false;
break;
}
beep();
}
}
lcd.clear();
}
alarmIsrWasCalled = false;
}// End of LOOP
void change_states()
{
switch (state)
{
case MAIN:
get_alarm();
break;
case MENU_SET_ALARM:
menu_set_alarm();
break;
case SET_AL_HOUR:
display_set_AL_time();
break;
case SET_AL_MINUTE:
display_set_AL_time();
break;
case SET_AL_ON_OFF:
display_set_AL_time();
break;
case ALARM_TIME:
displayALARM();
delay(2000);
lcd.clear();
state=MAIN;
break;
break;
}
}
void transition(int trigger)
{
switch (state)
{
case MAIN:
if (trigger == btnMENU)
{
lcd.clear();
state = MENU_SET_ALARM;
}
else if (trigger == btnBACK)
{
lcd.clear();
state = ALARM_TIME;
}
break;
case MENU_SET_ALARM:
if (trigger == btnUP)
{
lcd.clear();
state = MENU_SET_TIME;
}
else if (trigger == btnUP)
{
lcd.clear();
state = MENU_SET_DATE;
}
if (trigger == btnMENU)
{
lcd.clear();
state = SET_AL_HOUR;
}
if (trigger == btnBACK)
{
lcd.clear();
state = MAIN;
}
break;
case MENU_SET_TIME:
if (trigger == btnUP)
{
lcd.clear();
state = MENU_SET_DATE;
}
if (trigger == btnDOWN)
{
lcd.clear();
state = MENU_SET_ALARM;
}
if (trigger == btnMENU)
{
lcd.clear();
state = SET_HOUR;
}
if (trigger == btnBACK)
{
lcd.clear();
state = MAIN;
}
break;
case MENU_SET_DATE:
if (trigger == btnDOWN)
{
lcd.clear();
state = MENU_SET_TIME;
}
else if (trigger == btnDOWN)
{
lcd.clear();
state = MENU_SET_ALARM;
}
if (trigger == btnMENU)
{
lcd.clear();
state = SET_DAY;
}
if (trigger == btnBACK)
{
lcd.clear();
state = MAIN;
}
break;
case SET_AL_HOUR:
if (trigger == btnUP)
{
AL_Hour++;
if (AL_Hour>23) AL_Hour = 0;
}
else if (trigger == btnDOWN)
{
AL_Hour--;
if (AL_Hour<0) AL_Hour = 23;
}
if (trigger == btnMENU)
{
state = SET_AL_MINUTE;
}
break;
case SET_AL_MINUTE:
if (trigger == btnUP)
{
AL_Minute++;
if (AL_Minute>59) AL_Minute = 0;
}
else if (trigger == btnDOWN)
{
AL_Minute--;
if (AL_Minute<0) AL_Minute = 59;
}
if (trigger == btnMENU)
{
state=SET_AL_ON_OFF;
}
if (trigger == btnBACK)
{
state = SET_AL_HOUR;
}
break;
case SET_AL_ON_OFF:
if (trigger == btnUP)
{
alarm_active = true;
}
else if (trigger == btnDOWN)
{
alarm_active =false;
}
if (trigger == btnBACK)
{
state = SET_AL_MINUTE;
}
if (trigger == btnMENU)
{
set_alarm();
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Alarm Set!");
delay(1000);
lcd.clear();
state = MAIN;
}
break;}}
void menu_set_alarm(){
lcd.setCursor(6,0);
lcd.print("MENU");
lcd.setCursor(4,1);
lcd.print("Set Alarm");
lcd.setCursor(15,1);
lcd.write(5);
}
DoW= weekday();
switch(DoW){
case 1: day_of_week = "Sun"; break;
case 2: day_of_week = "Mon"; break;
case 3: day_of_week = "Tue"; break;
case 4: day_of_week = "Wed"; break;
case 5: day_of_week = "Thu"; break;
case 6: day_of_week = "Fri"; break;
case 7: day_of_week = "Sat"; break;}
lcd.setCursor(0,1);
lcd.print (day_of_week);}
void display_set_AL_time(){
lcd.setCursor(1,0);
lcd.print("Set Alarm Time:");
if (alarm_active==true){
lcd.setCursor(12,1);
lcd.print("ON");
}else{
lcd.setCursor(12,1);
lcd.print("OFF");}
switch (state)
{
case SET_AL_HOUR:
lcd.setCursor(10,1);
lcd.print("h");
if (long_press_button == false){
if (blink_state == 0){
lcd.setCursor(4,1);
display_position(AL_Hour);}
else{
lcd.setCursor(4,1);
lcd.print(" ");}}
else{
lcd.setCursor(4,1);
display_position(AL_Hour);
}
lcd.print(":");
display_position(AL_Minute);
break;
case SET_AL_MINUTE:
lcd.setCursor(10,1);
lcd.print("m");
lcd.setCursor(4,1);
display_position(AL_Hour);
lcd.print(":");
if (long_press_button == false){
if (blink_state == 0){
lcd.setCursor(7,1);
display_position(AL_Minute);
}
else{
lcd.setCursor(7,1);
lcd.print(" ");
}}
else{
lcd.setCursor(7,1);
display_position(AL_Minute);
}
break;
case SET_AL_ON_OFF:
lcd.setCursor(7,1);
display_position(AL_Minute);
if (alarm_active == true){
if (blink_state == 0){
lcd.setCursor(14,1);
lcd.print(" ");
lcd.setCursor(12,1);
lcd.print("ON");}
else{
lcd.setCursor(12,1);
lcd.print(" ");}}
else{
if (blink_state == 0){
lcd.setCursor(12,1);
lcd.print("OFF");
}
else{
lcd.setCursor(12,1);
lcd.print(" ");
}
}
break;
}
unsigned long blink_currentMillis = millis();
if(blink_currentMillis - blink_previousMillis > blink_interval){
blink_previousMillis = blink_currentMillis;
if (blink_state == 0){
blink_state = 1;
}else{
blink_state = 0;}}}
void displayALARM(){
lcd.setCursor(2,0);
lcd.print("<< ALARM >>");
lcd.setCursor(3,1);
display_position(AL_Hour);
lcd.print(":");
lcd.setCursor(6,1);
display_position(AL_Minute);
lcd.setCursor(9,1);
lcd.print("h");
if (alarm_active == true){
lcd.setCursor(13,1);
lcd.print(" ");
lcd.setCursor(11,1);
lcd.print("ON");}
else{
lcd.setCursor(12,1);
lcd.print("OFF");
}}
void set_alarm(){ //save alarm time to EEPROM
EEPROM.write(0, AL_Hour);
EEPROM.write(1, AL_Minute);
EEPROM.write(2, alarm_active);
RTC.setAlarm(ALM1_MATCH_HOURS, 0, AL_Minute, AL_Hour, 0);
RTC.alarm(ALARM_1);
if (alarm_active==true){
RTC.alarmInterrupt(ALARM_1, true);
}
else{
RTC.alarmInterrupt(ALARM_1, false);
}}
void get_alarm(){ //read alarm time from EEPROM
AL_Hour = EEPROM.read(0);
if (AL_Hour > 23) AL_Hour=0;
AL_Minute = EEPROM.read(1);
if (AL_Minute > 59) AL_Minute=0;
alarm_active = EEPROM.read(2);
if(alarm_active==true){
lcd.setCursor(5,1);
lcd.write(1);
}
else{
lcd.setCursor(5,1);
lcd.print(" ");
}}
void check_RTC()
{
if (timeStatus() != timeSet)
RTC_error = true;
else
RTC_error = false;
}
byte decToBcd(byte val){
return ( (val/1016) + (val%10) );}
byte bcdToDec(byte val){
return ( (val/1610) + (val%16) );}
void display_position(int digits){
if(digits < 10)
lcd.print("0");
lcd.print(digits);}