Hello everybody,
I am using the Nano 328 along with DS1307 RTC to design a automatic school bell which rings at preset times.
The Basic Idea
- The current time is printed on a 16x2 LCD along with the current mode of operation i.e Normal Day, Exam Days, Internal Assessment day, or Just Office Hours.
- All of these modes have their own times predefined via a switch case, and controlled via a push button switch variable.
- When the clock reaches a preset time, the Nano switches on the relay and after a pre-defined time, switches the relay off.
- The time duration of the bell ringing depends on 3 functions longbell(), shortbell, medbell(), each of which is called at different times defined in the individual mode cases.
The Problem
Now the problem is that once the clock reaches a preset time eg. 14:00:00, the relay switches on, while the time display on the LCD remains constant. After 10 seconds, the relay remains on and the time on the lcd increments in sets of 10 secs. This repeats for 6 sets, for a total of 60 secs. And then everything goes back to normal. The code and the problem is attached below.
Code:
#include <Wire.h>
#include "RTClib.h"
#include<LiquidCrystal.h>
#define relay 4 //relay switch pin
#define sw 5 //mode switch pin
RTC_DS1307 rtc;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void shbell(); //relay on function for 5 secs bell
void longbell(); //relay on function for 7 secs bell
void medbell(); //relay on function for 10 secs bell
void off(); //relay on function for NO bell
int mode = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0;
void setup () {
Serial.begin(57600);
Wire.begin();
rtc.begin();
pinMode(relay, OUTPUT);
pinMode(sw, INPUT);
digitalWrite(sw,HIGH);
lcd.begin(16, 2); // 16x2 LCD
lcd.clear(); // blank the display
}//setup ends here
void loop (){
DateTime now = rtc.now();
//Calculate date/time
int hh = now.hour();
int mm = now.minute();
int ss = now.second();
int yr = (now.year(), DEC);
int mt = (now.month(), DEC);
int dd = (now.day(), DEC);
//Print Current Date/TIme and mode
lcd.setCursor(0, 0);
lcd.print(hh);
lcd.setCursor(2, 0);
lcd.print(":");
lcd.setCursor(3, 0);
lcd.print(mm);
lcd.setCursor(5, 0);
lcd.print(":");
lcd.setCursor(6, 0);
lcd.print(ss);
//Mode Change Code for push button press
buttonState = digitalRead(sw);
if (buttonState != lastButtonState) {
if (buttonState == HIGH)
{
if(mode!=4)
{mode++;}
else
{mode=1;};
}
}
delay(50);
lastButtonState = buttonState;
//Mode Definition with preset times
switch (mode) {
case 1:
lcd.setCursor(0, 1);
lcd.print(" "); //Clears LCD Second Line since I found some residual text remaining
lcd.setCursor(0, 1);
lcd.print("Normal Day");
{
if (hh == 8 && mm == 0)
longbell();
else if (hh == 8 && mm == 50)
shbell();
else if (hh == 9 && mm == 40)
shbell();
else if (hh == 10 && mm == 30)
medbell();
else if (hh == 11 && mm == 0)
medbell();
else if (hh == 11 && mm == 50)
shbell();
else if (hh == 12 && mm == 40)
shbell();
else if (hh == 13 && mm == 30)
longbell();
else if (hh == 14 && mm == 15)
medbell();
else if (hh == 15 && mm == 5)
shbell();
else if (hh == 15 && mm == 55)
shbell();
else if (hh == 16 && mm == 45)
medbell();
else if (hh == 17 && mm == 00)
longbell();
else
off();
}
break;
case 2:
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Internals");
{if (hh == 9 && mm == 30)
longbell();
else if (hh == 10 && mm == 30)
medbell();
else if (hh == 11 && mm == 00)
medbell();
else if (hh == 12 && mm == 00)
medbell();
else if (hh == 14 && mm == 00)
medbell();
else if (hh == 15 && mm == 00)
medbell();
else if (hh == 15 && mm == 30)
medbell();
else if (hh == 16 && mm == 30)
medbell();
else
off();}
break;
case 3: lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Examination Hrs");
{
if (hh == 9 && mm == 15)
shbell();
else if (hh == 9 && mm == 20)
medbell();
else if (hh == 9 && mm == 30)
longbell();
else if (hh == 10 && mm == 00)
shbell();
else if (hh == 12 && mm == 20)
shbell();
else if (hh == 12 && mm == 30)
longbell();
else if (hh == 13 && mm == 45)
shbell();
else if (hh == 13 && mm == 50)
medbell();
else if (hh == 14 && mm == 00)
longbell();
else if (hh == 14 && mm == 30)
shbell();
else if (hh == 16 && mm == 50)
shbell();
else if (hh == 17 && mm == 00)
longbell();
else
off();
}
break;
case 4: lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Office Hours");
{
if (hh == 9 && mm == 00)
longbell();
else if (hh == 9 && mm == 30)
medbell();
else if (hh == 13 && mm == 00)
longbell();
else if (hh == 14 && mm == 00)
medbell();
else if (hh == 17 && mm == 00)
longbell();
else
off();
}
break;
}//switch statement ends here
}//loop ends here
void shbell()
{ digitalWrite(relay, 0); //Switches relay on
delay(5000); //waits for 5 seconds
digitalWrite(relay, 1); //switches relay off
}
void medbell()
{
digitalWrite(relay, 0); //Switches relay on
delay(7000); //waits for 7 seconds
digitalWrite(relay, 1); //switches relay off
}
void longbell()
{
digitalWrite(relay, 0); //Switches relay on
delay(10000); //waits for 10 seconds
digitalWrite(relay, 1); //switches relay off
}
void off()
{ digitalWrite(relay, HIGH);
}