I am currently interfacing my Arduino with a DS1307 RTC chip attached to Analog Inputs 4 and 5, a DS32kHz oscillator, and a momentary button with a pull-down resistor attached to Digital Pin 2. I would like to increment the number of minutes that the RTC is set to by 1 each time the button is pressed, so I am using this code:
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
int time[3];
int fullTime;
int onTime = 700; // time to turn light on
int offTime = 1900; // time to turn light off
volatile int state = LOW;
bool PM = false;
bool useSerialDebug = true; // set to true to give serial output
int minutes = 0;
int hours = 0;
void setup()
{
if (useSerialDebug)
Serial.begin(9600);
setRTC(0, minutes, hours, 0, 0, 0, 0);
pinMode(9, OUTPUT);
attachInterrupt(0, setMins, RISING);
}
void setRTC(int sec, int mins, int hr, int dow, int date, int mth, int yr)
{
RTC.stop();
RTC.set(DS1307_SEC, sec); //set the seconds
RTC.set(DS1307_MIN, mins); //set the minutes
RTC.set(DS1307_HR, hr); //set the hours
RTC.set(DS1307_DOW, dow); //set the day of the week
RTC.set(DS1307_DATE, date); //set the date
RTC.set(DS1307_MTH, mth); //set the month
RTC.set(DS1307_YR, yr); //set the year
RTC.start();
}
void setMins()
{
if (minutes < 60)
minutes = minutes + 1;
else if (minutes == 60)
{
minutes = 0;
if (hours < 24)
hours = hours + 1;
else if (hours == 24)
hours = 0;
}
setRTC(0, minutes, hours, 0, 0, 0, 0);
}
/*
void setHours()
{
if (hours < 24)
hours = hours + 1;
else if (hours == 24)
hours = 0;
setRTC(0, minutes, hours, 0, 0, 0, 0);
}*/
void loop()
{
// get the time, assign it to the array values
time[0] = RTC.get(DS1307_HR, true);
time[1] = RTC.get(DS1307_MIN, false);
time[2] = RTC.get(DS1307_SEC, false);
// create a military-time integer
fullTime = (time[0] * 100) + time[1];
// adjust for 24h time - subtract 12h and set the PM flag
if (time[0] > 12)
{
time[0] = time[0] - 12;
PM = true;
}
// serial stuff
if (useSerialDebug)
{
for (int i = 0; i < 3; i++)
{
if (time[i] < 10)
{
Serial.print("0");
}
Serial.print(time[i]);
if (i < 2)
Serial.print(":");
}
if (PM)
Serial.println("PM");
else
Serial.println("AM");
Serial.print("Full Time String: ");
Serial.println(fullTime);
}
// end serial stuff
// turn off if its earlier than the onTime, or later or equal to the offTime
if (fullTime < onTime || fullTime >= offTime)
digitalWrite(9, LOW);
else
digitalWrite(9, HIGH);
delay(1000);
}
The code runs fine and shows serial output until I press the button. Once I press the button, I stop receiving serial output. Pushing the button again and again makes no noticeable difference. I have to restart the Arduino to get it running again. What am I missing?