Hi,
I've been having trouble getting my project to work. I am creating a digital alarm clock using the arduino uno and a ds1307 rtc. I am using the Button.h library for arduino 1.0. I am trying to use a button to allow the user to reprogram the time. Essentially, the first time the time button (tbutton) is pressed go into the timeset function, stop the rtc and allow the user to increment the hours using another button (incbutton). The second time the time button (tbutton) is pressed allow the user to set the minutes using the incbutton. The third time tbutton is pressed restart the RTC and return to normal mode with the new time set.
When I first load the program, or reset the board, it goes straight into the timeset function and increments an hour without any buttons being pushed. I am sure it is in the timeset function when this happens as I have a serial output to confirm my suspicion. I am fairly new to this and would really appreciate any ideas as to what I may be doing wrong as well as any suggestions.
Thanks in advance!!
Here is my code: (Please excuse any commented out code etc. as all is a work in progress and yet to be cleaned up)
#include <Button.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Function Headers
bool AM (bool, int); //determines am or pm
//int 12hour(int); //converts 24 hour format to 12 hour format
void printDOW(int); //print int day of week as a string
int pressCount=0;
void timeSet(Button, Button, int, int, int);
Button tbutton = Button(10,BUTTON_PULLUP);
Button incbutton = Button(6,BUTTON_PULLUP);
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
//pinMode(10, INPUT);
//pinMode(6, INPUT);
digitalWrite(13,HIGH);
lcd.begin(20, 4);
pinMode(10,INPUT);
RTC.stop();
RTC.set(DS1307_SEC,45); //set the seconds
RTC.set(DS1307_MIN,59); //set the minutes
RTC.set(DS1307_HR,9); //set the hours
RTC.set(DS1307_DOW,7); //set the day of the week
RTC.set(DS1307_DATE,15); //set the date
RTC.set(DS1307_MTH,2); //set the month
RTC.set(DS1307_YR,12); //set the year
RTC.start();
}
void loop()
{
//variables
int _hour = RTC.get(DS1307_HR, true);
int _min = RTC.get(DS1307_MIN, false);
//int hour2 = 12hour(_hour); //sets hour in 12 hour format from RTC
bool am = AM(am, _hour); //sends the bool expression for am/pm and the current hour to the am/pm function
if(_hour > 12)
_hour = _hour - 12;
else if (_hour == 0)
_hour = 12;
//
if(tbutton.isPressed())
{
pressCount++;
timeSet(tbutton, incbutton, _hour, _min, pressCount);
};
lcd.setCursor(5,2);
/* if(_hour<10)
{
lcd.print("0");
}*/
lcd.print(_hour);
// Serial.print(RTC.get(DS1307_HR,true));
lcd.print(":");
if(RTC.get(DS1307_MIN,false) < 10) lcd.print("0");
lcd.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
lcd.print(":");
lcd.print(RTC.get(DS1307_SEC,false)); //read seconds (currently disabled)
if(am == true)
lcd.print("am");
else
lcd.print("pm");
lcd.setCursor(0,0);
lcd.print(RTC.get(DS1307_MTH,false));//read month
lcd.print("/");
lcd.print(RTC.get(DS1307_DATE,false));//read date
lcd.print("/");
lcd.print(RTC.get(DS1307_YR,false)); //read year
lcd.setCursor(16,0);
printDOW(RTC.get(DS1307_DOW,false));
delay(1000);
}
bool AM (bool AM, int hour)
{
if (hour_ < 12)
{
return true; //am
}
else
{
return false; //pm
}
}
/*int 12hour(int hour)
{
if(hour < 13)
return hour; //since hour is less then 12, does nothing
else
{
hour = hour - 12;
return hour; //returns hour after 12 has been subtracted, to display in 12 hour format
}
}*/
void printDOW(int _DOW)
{
switch(_DOW)
{
case 1:
lcd.print("Sun");
break;
case 2:
lcd.print("Mon");
break;
case 3:
lcd.print("Tue");
break;
case 4:
lcd.print("Wed");
break;
case 5:
lcd.print("Thu");
break;
case 6:
lcd.print("Fri");
break;
case 7:
lcd.print("Sat");
break;
}
}
void timeSet(Button tbutton, Button incbutton, int _hour, int _min, int pressCount)
{RTC.stop();
while(pressCount==1)
{
if (incbutton.isPressed())
{
_hour++;
Serial.print("in timeset"); //serial print is to check if program is going into this function upon startup
}
if (tbutton.isPressed())
{ pressCount++;
RTC.set(DS1307_HR,_hour); //set the hours
}
}
while(pressCount==2)
{
if(incbutton.isPressed())
_min++;
if(tbutton.isPressed())
{pressCount=0;
RTC.set(DS1307_MIN,_min);
}
}
RTC.start();
return;
}
Here is the link to the Button.h and Button.cpp I am using.. figured it would be easier to view then on the forum... http://github.com/tigoe/Button/archives/master