Bell ringing code for arduino



/* bell ringing code by saurav yadav, Developped for MDVM PARLE SCHOOL NEEMRANA:
 *  Instruction for use:
 *  1. Start Serial monitor and enter current time
 *  2. Enter total number of bells you need
 *  3. Enter bell timings in hh:mm format for all the bells.
 *  4. Close the serial monitor and you are set..
 *  The bell will ring as per the time set for a duration of 3 seconds.
 *  Code version 2.0
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);

int h=0;
int m=0;
int s=0;
long nh,nm, nbh,nbm;
int flag,flag1, state;
int TIME;
int totalperiod;
int period=1;
int duration=1;
int laps=0;
int bell=10;
const int hs=8;
const int ms=9;
int ph[8], pm[8];


void setup()
{
  lcd.begin(16,2);
  pinMode(hs,INPUT);
  pinMode(ms,INPUT);
  pinMode(bell,OUTPUT);
  digitalWrite(bell, HIGH);
  Serial.begin(9600);
  Serial.println("Enter current time");
  Serial.println("E.G. 12:23");
  Serial.println();
  lcd.setCursor(0,0);
  lcd.print("SET BELL TIMINGS");
  lcd.setCursor(0,1);
  lcd.print("MDVM PRAGYAN");
}

void loop()
{

if(flag1==0)
{
    while(Serial.read()==0);
    while(Serial.available()== 0)
    nh = Serial.parseInt();
    nm = Serial.parseInt();
    h=int(nh);
    m=int(nm);
    Serial.print("Time Entered is: ");
    Serial.print(h);
    Serial.print(":");
    Serial.println(m);
   
    Serial.println("Enter Number of bells: ");
    while(Serial.read()==0);
    while(Serial.available()== 0)
    totalperiod = Serial.parseInt();
   
  for(int i=0; i<totalperiod; i++)
  {
  Serial.print("Enter ");Serial.print(i+1); Serial.println(" Bell Timing");
    while(Serial.read()==0);
    while(Serial.available()== 0)
    ph[i] = Serial.parseInt();
    pm[i] = Serial.parseInt();
    Serial.print(i+1); Serial.print(" Bell Timing is: ");
    Serial.print(ph[i]);Serial.print(":");Serial.println(pm[i]);
  }
  flag1=1;
  nbh=ph[0];
  nbm=pm[0];
  }  

 lcd.setCursor(0,0);
 s=s+1;
  lcd.print("P=");
  if (period>totalperiod)
  {
   lcd.print("   ");
  }
  else
  {
    lcd.print(period);
 
  lcd.print("/");
  lcd.print(totalperiod);
  }
  lcd.print(" T:");
  lcd.print(h);
 lcd.print(":");
 lcd.print(m);
 lcd.print(":");
 lcd.print(s);

 if(flag<12)lcd.print("AM");
 if(flag==12)lcd.print("PM");
 if(flag>12)lcd.print("PM");
 if(flag==24)flag=0;

 delay(980);
 //s=s+1;
 lcd.clear();
 if(s==60){
  s=0;
  m=m+1;
  state=0;
  laps=laps+1;
 }
 if(m==60)
 {
  m=0;
  h=h+1;
  flag=flag+1;
 }
 if(h==13)
 {
  h=1;
 }

//Bell ring code
 if (period<=totalperiod && state==0)
 {
 for(int j=0; j<totalperiod;j++)
 {
  if(h==ph[j] && m==pm[j] && state==0)
  {
    digitalWrite(bell, LOW);
    lcd.setCursor(0,0);
    lcd.print("BELL IS RINGING");
    lcd.setCursor(0,1);
    lcd.print("~~MDVM ATL~~");
    delay(4000);
    digitalWrite(bell, HIGH);
    period=period+1;
    laps=0;
    nbh=ph[j+1];
    nbm=pm[j+1];
    state=1;
  }
 }
 }
 
lcd.setCursor(0,1);
lcd.print("NXT_BL:");
//lcd.print(duration-laps);
lcd.print(nbh);
lcd.print(":");
lcd.print(nbm);
lcd.print(" ATL");

}