The layout right now is documented (kinda) in the code. I have buttons connected between ground and pins 9 and 10 - hour and minute incrementing buttons. When I press the hour button, hours increments. When I press the minute button, nothing happens. When I press both, timesPressed increments and I change to the next screen. Why is the minute button not incrementing on its own?
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
#include <DallasTemperature.h>
double maxtemp = 0;
bool done = false;
double mintemp = 255;
byte up[8] = {
0x04, 0x0E, 0x15, 0x04, 0x04, 0x04, 0x04, 0x00 };
byte down[8] = {
0x04, 0x04, 0x04, 0x04, 0x15, 0x0E, 0x04, 0x00 };
int time[3];
int fullTime, hours, timesPressed = 0;
int ledState = LOW;
bool PM = false;
//-------------------------------------
//-----------DEFINITIONS---------------
//-------------------------------------
bool useSerialDebug = true; // set to true to give serial output
int onTime = 700; // time to turn light on
int offTime = 1900; // time to turn light off
int lightPin = 11; // light to turn on and off
int minsButtonPin = 9; // button to increment minutes by 1
int hoursButtonPin = 10; // button to increment hours by 1
static const int tempSensorPin = 12; // temp sensor pin
//-------------------------------------
//---------CREATING INSTANCES----------
//-------------------------------------
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
NewOneWire oneWire(tempSensorPin);
DallasTemperature tempSensor(oneWire, 0);
void setup()
{
tempSensor.begin();
tempSensor.setResolution(9);
if (useSerialDebug)
Serial.begin(9600);
pinMode(lightPin, OUTPUT);
pinMode(minsButtonPin, INPUT);
pinMode(hoursButtonPin, INPUT);
pinMode(13, OUTPUT);
digitalWrite(minsButtonPin, HIGH); // turn on internal pull-up
digitalWrite(hoursButtonPin, HIGH);
lcd.begin(2, 16);
}
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()
{
time[1]++;
if (time[1] == 60)
{
time[1] = 0;
time[0]++;
if (time[0] == 24)
time[0] = 0;
}
setRTC(0, time[1], time[0], 0, 0, 0, 0);
}
void setHours()
{
time[0]++;
if (time[0] == 24)
time[0] = 0;
setRTC(0, 0, time[0], 0, 0, 0, 0);
}
//returns true if a button is pressed or has been pressed since the last call
// will not return true again until the button is released and pressed again
boolean minsButtonPressed()
{
static boolean buttonState; // the current reading from the input pin
static boolean lastButtonState = LOW; // the previous reading from the input pin
static boolean result = false; // the value returned by function
static long lastDebounceTime = 0; // the last time the output pin was toggled
const long debounceDelay = 50; // the debounce time; increase if the output flickers
// read the state of the switch into a local variable:
int reading = digitalRead(minsButtonPin);
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if( buttonState != reading){ // if the state has changed
if(reading == LOW) // change this to HIGH if using pull-down resistors
result = true;
buttonState = reading; // stable so in state long enough
}
}
lastButtonState = reading;
if( result == true){
result = false; // only return true once per press
return true;
}
return false;
}
boolean hoursButtonPressed()
{
static boolean buttonState; // the current reading from the input pin
static boolean lastButtonState = LOW; // the previous reading from the input pin
static boolean result = false; // the value returned by function
static long lastDebounceTime = 0; // the last time the output pin was toggled
const long debounceDelay = 50; // the debounce time; increase if the output flickers
// read the state of the switch into a local variable:
int reading = digitalRead(hoursButtonPin);
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if( buttonState != reading){ // if the state has changed
if(reading == LOW) // change this to HIGH if using pull-down resistors
result = true;
buttonState = reading; // stable so in state long enough
}
}
lastButtonState = reading;
if( result == true){
result = false; // only return true once per press
return true;
}
return false;
}
void loop()
{
//-------------------------------------
//--------GET TIME, FILL ARRAY---------
//-------------------------------------
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];
//-------------------------------------
//----------BUTTON STUFF---------------
//-------------------------------------
if (minsButtonPressed() && hoursButtonPressed())
{
timesPressed++;
done = false;
if (timesPressed == 3)
timesPressed = 0;
}
else if (minsButtonPressed() && !hoursButtonPressed() && timesPressed == 0)
setMins();
else if (hoursButtonPressed() && !minsButtonPressed() && timesPressed == 0)
setHours();
//-------------------------------------
//------MAKE THE TIME PRETTIER---------
//-------------------------------------
if (time[0] > 12)
{
hours = time[0] - 12;
PM = true;
}
else if (time[0] == 12)
{
hours = time[0];
PM = true;
}
else if (time[0] == 0)
{
hours = 12;
PM = false;
}
else
{
hours = time[0];
PM = false;
}
//-------------------------------------
//----------SERIAL STUFF---------------
//-------------------------------------
if (useSerialDebug)
{
// hours
Serial.print(hours);
Serial.print(":");
// minutes
if (time[1] < 10)
Serial.print("0");
Serial.print(time[1]);
Serial.print(":");
// seconds
if (time[2] < 10)
Serial.print("0");
Serial.print(time[2]);
// AM / PM
if (PM)
Serial.println("PM");
else
Serial.println("AM");
// full time string for testing
Serial.print("Full Time String: ");
Serial.println(fullTime);
}
//-------------------------------------
//-----------TEMP STUFF----------------
//-------------------------------------
float temp = tempSensor.getTemperature();
//-------------------------------------
//------------LCD STUFF----------------
//-------------------------------------
if (timesPressed == 0)
{
if (!done) // clear old stuff off
{
lcd.clear();
done = true;
}
lcd.setCursor(3,0);
// hours
lcd.print(hours);
lcd.print(":");
// minutes
if (time[1] < 10)
lcd.print("0");
lcd.print(time[1]);
lcd.print(":");
// seconds
if (time[2] < 10)
lcd.print("0");
lcd.print(time[2]);
// AM / PM
if (PM)
lcd.print("PM "); // trailing space for when the time moves
else
lcd.print("AM ");
// temperature on second line
lcd.setCursor(4,1);
lcd.print(temp);
lcd.write(223);
lcd.print("C");
}
else if (timesPressed == 1)
{
if (!done)
{
lcd.clear();
done = true;
}
lcd.home();
lcd.print("tp == 1");
}
else if (timesPressed == 2)
{
if (!done)
{
lcd.clear();
done = true;
}
lcd.home();
lcd.print("tp == 2");
}
//-------------------------------------
//-----------RELAY STUFF---------------
//-------------------------------------
// turn off if its earlier than the onTime, or later or equal to the offTime
if (fullTime < onTime || fullTime >= offTime)
digitalWrite(lightPin, LOW);
else
digitalWrite(lightPin, HIGH);
//-------------------------------------
//--------DEBUG LED STUFF--------------
//-------------------------------------
ledState = !ledState;
digitalWrite(13, ledState);
}