Alarm and Timer Help....

Hello,

To start things off, here is some history. I started using the arduino platform as a means of keeping myself engaged and make some fun projects for my daughter as she is growing up (she is just 4 now). The fun and joy of making small projects, like making the LED blink when button is pressed etc, soon gave way to complex thoughts like controlling stuff using the small hand sized controller board. The urge to learn now is a full time hobby with hardly any time for my other passions. Ask my wife and she would give you a earful :).

Now to the present. I'm working on automating my aqarium operations as much as possible. The good 'ole google gave me a good start by pointing me to John Boxall's excellent tutorials and getting me started on the complex project. I did go through a lot of other sites which detailed various aquarium control related projects but they lacked in teaching me the inner working of the code or the arduino. Hence as a challenge, I started with amalgamating various codes found on the net and making something from scratch. Bad idea some might say, but it gives me the joy of creating something that fits my needs and i'm able to service it if things go wrong.

Progress So Far : Using DS18B20 the temperatures are been read and displayed. I got the external timer using the DS1307 chip to start keeping the real time and communicating that with the LCD via the Arduino. In order to save pins I'm using a 4094 to connect the LCD to the Arduino. I've the time-day-date function working and I'm able to set the time date day using a push button. Using John Boxall's tutorials, ive been able to get add alarms which show up on the LCD once the time set is reached. Now the questions

  1. I plan to have an option of at least 4-5 timers which would drive stuff like lights, pump and servo motors. I've been reading through the "potted plant watering system" but I didnt quite comprehend the discussion in that thread. So the first question is, how do i predefine the number of alarms/timers I want. Say when the setup loop kicks in it should ask me "number of alarms" with a default being 5. The alarms would be once a 24 hr operation for a "specified" period of time. SO its kind of mixing alarms with timers. So the lights turn on at 8am and turn off at 8pm. the servo turns on at 10am, 12pm, 2pm, 4pm for 10 secs each.
  2. I plan to use 2 PCF8475 to have output to run relays which in turn will run the lights and pumps and servos. How do I let arduino know that at a pre-set time (once the alarm/timer kick in) make a particular pin high. lets say alarm 1 goes off so pin x1 would be high, alarm 2 goes off pin x2 would be high and so forth.

Once I have a good clue and handle on getting the above to work, I plan to incorporate keypad for inputs and menu based selection.

Like I said earlier, its a patch work of various codes found on the net without much commenting and cleanup. Any help in answer the questions I have would be appreciated.

  17-4-2008 - http://www.glacialwanderer.com/hobbyrobotics
 */

int t1224 = 0; // set later on in function mset1224()
int alarmon = 0; // 0 for off, 1 for on
int alarmh = 12; // alarm hour
int alarmm = 00; // alarm minute


#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#include <LiquidCrystal.h> // we need this library for the LCD commands
//LiquidCrystal lcd(12,11,5,4,3,2);
#include <LiquidCrystal_SR_LCD3.h>
#include <OneWire.h>
#include <DallasTemperature.h>


#define ONE_WIRE_BUS 10

#if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
  #define I2C_WRITE Wire.write 
  #define I2C_READ Wire.read
#else                                   // Arduino Prior to v1.0 
  #define I2C_WRITE Wire.send 
  #define I2C_READ Wire.receive
#endif

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
  const int PIN_LCD_STROBE         =  2;  // Out: LCD IC4094 shift-register strobe pin1
  const int PIN_LCD_DATA           =  3;  // Out: LCD IC4094 shift-register data pin2
  const int PIN_LCD_CLOCK          =  4;  // Out: LCD IC4094 shift-register clock pin3
 // const int PIN_LCD_BACKLIGHT      =  5;  // Out: LCD backlight (PWM)                                                 
                                                   
LiquidCrystal_SR_LCD3 lcd(PIN_LCD_DATA, PIN_LCD_CLOCK, PIN_LCD_STROBE); 
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}
// DECLARATIONS***********************
int numberOfSensors;
int Maxtemp=0;
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
int i;
long previousMillis = 0;        // will store last time Temp was updated
int previousTemp = 0;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte zero;
char  *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char  *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
byte fanOn[8] = {0b11100, 0b10000, 0b11000, 0b10000, 0b10100, 0b01110, 0b00100, 0b00000};
//float allTemp [10];
byte fanOff[8] = {
  0b11100, 0b10000, 0b11000, 0b10000, 0b10000, 0b00000, 0b01111, 0b00000
};

byte alarmOn[8] = {
  0b00100, 0b01110, 0b01110, 0b01110, 0b11111, 0b00000, 0b00100, 0b00000
};

byte alarmOff[8] = {
  0b11011, 0b10001, 0b10001, 0b10001, 0b00000, 0b11111, 0b11011, 0b11111
};

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  lcd.begin(20, 4);
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  second = 0;
  minute = 32;
  hour = 18;
  dayOfWeek =4 ;
  dayOfMonth = 20;
  month = 6;
  year = 12;
 //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

 // lcd.begin(20, 4); // tells Arduino the LCD dimensions
  lcd.setCursor(0,0);
  lcd.print("tronixstuff.com"); // print text and move cursor to start of next line
  lcd.setCursor(0,1);
  lcd.print("* example 9.3 * ");
  delay(1000);
   lcd.clear();
  pinMode(8, INPUT);
  pinMode(13, OUTPUT);
  
  sensors.begin();
  delay(5000);  //important on linux as serial port can lock up otherwise
  numberOfSensors = discoverOneWireDevices();
//  Serial.println();
  zero=0x00;
  lcd.createChar(0, fanOn);
    lcd.createChar(1, fanOff);
    lcd.createChar(2, alarmOn);  
    lcd.createChar(3, alarmOff);  
    lcd.setCursor (0, 0);
    lcd.print(("Aquarium Controller")); 
    lcd.setCursor (0, 1);
    lcd.print(("v1.1"));
    lcd.setCursor (0, 2);
    lcd.print(("Ritesh Fating"));
    lcd.setCursor (0, 3);
    lcd.print(("May 2012 "));
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,2);
    delay(5000);
}

/*

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  if (digitalRead(8)==HIGH)
    // has the user pressed the button? If so, display the menu
  {
    delay(500); // for debounce
   // Serial.print(" pressed ");
   lcd.clear();
   lcd.setCursor(0,2);
    lcd.print("button pressed");
    delay(1000);
    displaymenu();

  }
  if (alarmon==1)
  {
    checkalarm(); // should the alarm activate?
    
  }
  getDateDs1307comp();
  printTemperatures();
  checkTemp();
  delay(500);
}

void setDateDs1307(byte second,        // 0-59
byte minute,        // 0-59
byte hour,          // 1-23
byte dayOfWeek,     // 1-7
byte dayOfMonth,    // 1-28/29/30/31
byte month,         // 1-12
byte year)          // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));     
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.send(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
  Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
  
  
}


int readdial(int rangemax, int dialpin)
{
  int kv=0;
  int kr=0;
  int kb=0;
  float a=0;
  float rd=0;
  rd=1023/rangemax;
  kb=analogRead(dialpin); // read potentiometer connected to analog pin 1
  a=kb/rd;
  kr=int(a);
  if (kr>rangemax)
  {
    kr=rangemax;
  }
  return kr;
}

void displaymenu()
// display the menu options, selectable by using the knob
{
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print("Turn knob slowly"); 
  lcd.setCursor(0,1);
  lcd.print("to select option");
  while (digitalRead(8)==LOW)
  {
    if      (readdial(12,1)==0) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("   Set hours    "); 
    }
    else if (readdial(12,1)==1) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("  Set minutes   "); 
    }
    else if (readdial(12,1)==2) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print(" 12 or 24h time?"); 
    }
    else if (readdial(12,1)==3) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("Set day of month");
    }
    else if (readdial(12,1)==4) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("   Set month    "); 
    }
    else if (readdial(12,1)==5) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("   Set year     "); 
    }
    else if (readdial(12,1)==6) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print(" Set Mon~Sun    "); 
    }
    
    else if (readdial(12,1)==7) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("  Alarm on/off  "); 
    }
    else if (readdial(12,1)==8) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print(" Set alarm hour"); 
    }
    else if (readdial(12,1)==9) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print(" Set alarm min. "); 
    }
    else if (readdial(12,1)==10) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("   Set Max Temp    "); 
    }
    else if (readdial(12,1)==11) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("   exit menu    "); 
    }
    
    delay(100); // stop screen flicker
  }

  switch(readdial(12,1))
  {
  case 0: 
    msethours(); 
    break;
  case 1: 
    msetminutes(); 
    break;
  case 2: 
    mset1224(); 
    break;
  case 3: 
    msetday(); 
    break;
  case 4: 
    msetmonth(); 
    break;
  case 5: 
    msetyear(); 
    break;
  case 6: 
    msetdow(); 
    break;
    case 7:
    alarmonoff();
    break;
  case 8:
    msetalarmh();
    break;
  case 9:
    msetalarmm();
    break;
  case 10:
    settemp();
    break;
  }
  // if the knob is 7, that is for return to clock display. function will end and return to clock by default
}

void mset1224()
//  allows user to select between displaying 12 and 24 hour time
{
  //Serial.write(" mset1224 ");
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print("Turn knob slowly"); 
  lcd.setCursor(0,1);
  lcd.print("to select option");
  while (digitalRead(8)==LOW)
  {
    if      (readdial(2,1)==0) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("  12-hour time  "); 
    }
    else if (readdial(2,1)==1) {
      lcd.clear();  
      lcd.setCursor(0,0); 
      lcd.print("  24-hour time  "); 
    }
    delay(100); // stop screen flicker
  }
  switch(readdial(2,1))
  {
  case 0: 
    t1224=0; 
    break;
  case 1: 
    t1224=1; 
    break;
  }
  displaymenu();
}

void msethours()
//  allows user to select hour value
{
  //Serial.write(" msethours ");
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print("Select hour:0~23"); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(24,1));
    delay(100);
  }
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  hour=readdial(24,1);
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  displaymenu();
}

void msetminutes()
//  allows user to select minute value
{
  //Serial.write(" msetminutes ");
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print("Set minute:0~59"); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(60,1));
    delay(100);
  }
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  minute=readdial(60,1);
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  displaymenu();
}

void msetday()
//  allows user to select day of the month
{
  //Serial.write(" msetday ");
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print("  Set day: 1~31 "); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(31,1));
    delay(100);
  }
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  dayOfMonth=readdial(31,1);
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  displaymenu();
}

void msetmonth()
//  allows user to select day of the month
{
  //Serial.write(" msetmonth ");
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print(" Set month: 1~12"); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(12,1));
    delay(100);
  }
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  month=readdial(12,1);
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  displaymenu();
}

void msetyear()
//  allows user to select year
{
 // Serial.write(" msetyear ");
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print(" Set year:00~99 "); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(100,1));
    delay(100);
  }
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  year=readdial(100,1);
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  displaymenu();
}
void msetdow()
//  allows user to select day of week (Monday~Sunday)
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print(" Choose the day "); 
  delay(300); // for debounce
  while (digitalRead(8)==LOW)
  {
    if      (readdial(7,1)==0) {
      lcd.setCursor(0,1); 
      lcd.print("   Sunday       ");
    }
    else if (readdial(7,1)==1) {
      lcd.setCursor(0,1); 
      lcd.print("  Monday        "); 
    }
    else if (readdial(7,1)==2) {
      lcd.setCursor(0,1); 
      lcd.print(" Tuesday        "); 
    }
    else if (readdial(7,1)==3) {
      lcd.setCursor(0,1); 
      lcd.print("  Wednesday     "); 
    }
    else if (readdial(7,1)==4) {
      lcd.setCursor(0,1); 
      lcd.print("   Thursday     "); 
    }
    else if (readdial(7,1)==5) {
      lcd.setCursor(0,1); 
      lcd.print("   Friday       "); 
    }
    else if (readdial(7,1)==6) {
      lcd.setCursor(0,1); 
      lcd.print(" Saturday       "); 
    }
    delay(100); // stop screen flicker
  }
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  dayOfWeek=readdial(7,1)+1;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  displaymenu();
}

void alarmonoff()
//  allows user to turn alarm on or off
{
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0);
  lcd.print("Current: ");
  lcd.print(alarmh);
  lcd.print(":");
  if (alarmm<10)
  {
    lcd.print("0");
  }
  lcd.print(alarmm);
  lcd.print("h");

  while (digitalRead(8)==LOW)
  {
    if      (readdial(2,1)==0) {
      lcd.setCursor(0,1); 
      lcd.print("   Alarm OFF    "); 
    }
    else if (readdial(2,1)==1) {
      lcd.setCursor(0,1); 
      lcd.print("   Alarm ON     "); 
    }
    delay(100); // stop screen flicker
  }
  switch(readdial(2,1))
  {
  case 0: 
    alarmon=0; 
    break;
  case 1: 
    alarmon=1; 
    break;
  }
  delay(300);
  displaymenu();
}

void checkalarm()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  if (alarmh==hour && alarmm==minute)
    // alarm time!
  {
    while (digitalRead(8)==LOW) // do noisy or blinky things until user presses the button
    {
      lcd.clear(); // clear LCD screen
      lcd.setCursor(0,0);
      lcd.print(" * ALARM TIME * ");
      lcd.setCursor(0,1);
      lcd.print("It is now: ");
      lcd.print(alarmh);
      lcd.print(":");
      if (alarmm<10)
      {
        lcd.print("0");
      }
      lcd.print(alarmm);
      lcd.print("h");
      delay(300);
      // insert functio to turn on/off piezo buzzer, etc
    }
    delay(500);
    alarmonoff(); 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Careful: don't");
    lcd.setCursor(0,1);
    lcd.print("go back to sleep");
    delay(60000); 
    lcd.clear();// wait a minute
  }
}

void msetalarmh()
//  allows user to select alarm hour value
{
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print(" Alarm hour:0~23"); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(24,1));
    delay(100);
  }
  alarmh=readdial(24,1);
  displaymenu();
}

void msetalarmm()
//  allows user to select alarm minute value
{
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print("Alarm mins:0~59"); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(60,1));
    delay(100);
  }
  alarmm=readdial(60,1);
  delay(300);
  alarmonoff(); // force user to turn alarm on or off
  displaymenu();
}
// NEW SUBROUTINES

void printTemperatures(void) {
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  //Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  //Serial.println("DONE");
 
  // Read each of our sensors and print the value
  for(int i=0; i < numberOfSensors; i++) {
     float tempC = sensors.getTempCByIndex(i);
    //Serial.print(tempC);
   lcd.setCursor(0,1);
   lcd.print("Probe:");
   lcd.setCursor(8,1);
   lcd.print(tempC);
   lcd.print((char)223);
   lcd.print("C");
   lcd.setCursor(6,1);
   lcd.print(i+1, DEC);
   //delay(750);
   }
  }
   
void checkTemp () {
  for(int i=0; i < numberOfSensors; i++){
    float allTemp[numberOfSensors];
  allTemp[i] = sensors.getTempCByIndex(i);
 // Serial.println(allTemp[i]);
  if (allTemp[0] < Maxtemp)
  {
    lcd.setCursor(0,3);
   // lcd.print("OFF");
    lcd.print(char(1));
      }
  else if (allTemp[0] > Maxtemp)
  {
    lcd.setCursor(0,3);
    //lcd.print(i+1, DEC);
   // lcd.print("ON");
    lcd.print(char(0));
    digitalWrite(13, HIGH);   
  delay(500);               // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
  }
  
}
}
 
  //Serial.println();

int discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  int count = 0;
 
 // Serial.println("Looking for 1-Wire devices...");
  while(oneWire.search(addr)) {
     if ( OneWire::crc8( addr, 7) != addr[7]) {
       // Serial.println("CRC is not valid!");
        return 0;
    }
   // Serial.println();
    count++;
  }
  //Serial.println("That's it.");
  oneWire.reset_search();
  return count;
}

////TIME PROGRAM CONTROLLED FROM COMPUTER
void setDateDs1307comp()                
{
 
   second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
   minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   dayOfWeek = (byte) (Serial.read() - 48);
   dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   I2C_WRITE(zero);
   I2C_WRITE(decToBcd(second) & 0x7f);    // 0 to bit 7 starts the clock
   I2C_WRITE(decToBcd(minute));
   I2C_WRITE(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   I2C_WRITE(decToBcd(dayOfWeek));
   I2C_WRITE(decToBcd(dayOfMonth));
   I2C_WRITE(decToBcd(month));
   I2C_WRITE(decToBcd(year));
   Wire.endTransmission();
}

void getDateDs1307comp()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();
 
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 
  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(I2C_READ() & 0x7f);
  minute    = bcdToDec(I2C_READ());
  hour      = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(I2C_READ());
  dayOfMonth = bcdToDec(I2C_READ());
  month      = bcdToDec(I2C_READ());
  year       = bcdToDec(I2C_READ());
 lcd.setCursor(0,0);
 lcd.print(Mon[month]);
 lcd.print(" ");
 lcd.print(dayOfMonth, DEC);
 lcd.print(" ");
 lcd.print(Day[dayOfWeek]);
 lcd.print(" ");
 if (t1224==0)
  {
    if (hour==0)
    {
      lcd.print("12");
    } 
    else
      if (hour>0 && hour<13)
      {
        lcd.print(hour, DEC);
      }
      else 
        if (hour>=13)
      {
        lcd.print((hour-12), DEC);
      }
  } 
  else if (t1224==1)
  {
    lcd.print(hour, DEC);
  }
  lcd.print(":");
  if (minute<10)
  {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
    
  if (t1224==1)
  {
    lcd.print("h");
  }
  else if (t1224==0)
  {
    if ((23-hour)>11)
    {
      lcd.print(" am");
    }
    if ((23-hour)<12)
    {
      lcd.print(" pm");
    }
  }

}

void settemp()
//  allows user to select max temp value
{
  delay(300); // for debounce
  lcd.clear();  
  lcd.setCursor(0,0); 
  lcd.print(" Max Temp Set:"); 
  while (digitalRead(8)==LOW)
  {
    lcd.setCursor(8,1); 
    lcd.print("     ");    
    lcd.setCursor(8,1);
    lcd.print(readdial(51,1));
    delay(100);
  }
  Maxtemp=readdial(51,1);
  displaymenu();
  1. I plan to have an option of at least 4-5 timers which would drive stuff like lights, pump and servo motors

First, you need to distinguish between timers and times that things should happen. You appear to want some number of the latter. The number of the former is fixed, and depends on which Arduino you have. Which of the timers are available for your use depends on what else your code is doing.

So the first question is, how do i predefine the number of alarms/timers I want. Say when the setup loop kicks in it should ask me "number of alarms" with a default being 5.

How would you answer that question? The Arduino does not, by default, have a display device for the question to be presented on or a keyboard for you to use to type an answer.

The number of "timers" is only a small part of the question. When do the "timers" go off? What happens when they do? Answering these questions, at run time, will be a lot more challenging, especially the what part.

  1. I plan to use 2 PCF8475 to have output to run relays which in turn will run the lights and pumps and servos. How do I let arduino know that at a pre-set time (once the alarm/timer kick in) make a particular pin high. lets say alarm 1 goes off so pin x1 would be high, alarm 2 goes off pin x2 would be high and so forth.

A lot depends on how the Arduino knows what time it is. By default, it does not. It simply knows how long it has been running.