Hi all, i'm facing difficulty with this program, my third pushbutton can't seem to work. my original program is that the first button is an increment button. the second button is a decrement button. my third button is supposed to be the confirm/set button. when the first time the third button is pressed, the hour will be saved, then when the second time the third button is pressed, it will be saved into the min. but i cant seem to enter my third button loop and also, i can't seem to display my numbers at the correct place as well, even after setting the setCursor.
please help me, i really need help);
and this is my full code
#include <LiquidCrystal.h>
#include <Wire.h>
#include <DS1307.h>
const int buttonPin1 = 16; // the pin that the pushbutton is attached to the increment button
const int buttonPin2 = 17; // the pin that the pushbutton is attached to the decrement button
const int Setbutton3 = 18; // the pin that the pushbutton is attached to set/confirm button
const int ledPin = 13; // the pin that the LED is attached to
int i = 0;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonSetCounter = 0; //counter for the number of times set
int buttonState1 = 0; // current state of the Increment button
int lastbuttonState1 = 0; // previous state of the Increment button
int buttonState2=0; //current state of the button2 (decrement button)
int lastbuttonState2=0; //previous state of the button2 (decrement button)
int buttonState3 = 0; // current state of the set button
int lastButtonState3 = 0; // previous state of the set button
int SHITHOUR;
int SHITMIN;
int SHITSEC;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char Digitalchar[] = {'0','1', '2','3','4','5', '6','7','8','9'};
char stringHOUR[3];
char stringMIN[2];
char stringSEC[2];
char stringDATE[2];
char stringMONTH[2];
char stringYEAR[2];
DS1307 clock;
void setup() { //start of void setup
Wire.begin(14,15);
// initialize the button pin as a input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(Setbutton3, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
lcd.begin(16, 2);
clock.begin();
SerialUSB.begin();
} //end of void setup
void loop() { //start of voidsetuptime
// read the pushbutton input pin:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(Setbutton3);
if (buttonState1 != lastbuttonState1) // compare the buttonState to its previous state
{
if (buttonState1 == HIGH) // if the current state is HIGH,
{
buttonPushCounter++; // if the state has changed, increment the counter
lcd.print(buttonPushCounter);
} //end of (if (buttonState1 == HIGH))
lastbuttonState1 = buttonState1; //save the current state as the last state, for the next time through the loop
} //end of(buttonState1 != lastbuttonState1)
if (buttonState2 != lastbuttonState2)
{
if (buttonState2 == HIGH)
{
buttonPushCounter--;
lcd.print(buttonPushCounter);
} //end of (if (buttonState2 == HIGH))
lastbuttonState2 = buttonState2; //save the current state as the last state, for the next time through the loop
} //end of (if(buttonState2 != lastbuttonState2))
else if (buttonState3 != lastButtonState3) {
if (buttonState3 == HIGH) {
delay(100);
if (buttonSetCounter == 0)
{
stringHOUR[0] = Digitalchar[buttonPushCounter/10];
stringHOUR[1] = Digitalchar[buttonPushCounter%10];
// stringHOUR[2] = '\n';
lcd.setCursor(0,0);
lcd.print(stringHOUR);
lcd.print(":");
buttonSetCounter++;
} //end of (if buttonSetCounter==0)
if (buttonSetCounter == 1)
{
stringMIN[0] = Digitalchar[buttonPushCounter/10];
stringMIN[1] = Digitalchar[buttonPushCounter%10];
lcd.setCursor(3,0);
lcd.print(stringMIN);
lcd.print(":");
buttonSetCounter++;
} //end of (if buttonSetCounter==1)
if (buttonSetCounter == 2)
{
stringSEC[0] = Digitalchar[buttonPushCounter/10];
stringSEC[1] = Digitalchar[buttonPushCounter%10];
lcd.setCursor(6,0);
lcd.print(stringSEC);
buttonSetCounter++;
} //end of (if buttonSetCounter==2)
if (buttonSetCounter == 3)
{
stringDATE[0] = Digitalchar[buttonPushCounter/10];
stringDATE[1] = Digitalchar[buttonPushCounter%10];
lcd.setCursor(0,1);
lcd.print(stringDATE);
lcd.print("/");
buttonSetCounter++;
} //end of (if buttonSetCounter==3)
if (buttonSetCounter == 4)
{
stringMONTH[0] = Digitalchar[buttonPushCounter/10];
stringMONTH[1] = Digitalchar[buttonPushCounter%10];
lcd.setCursor(3,0);
lcd.print(stringMONTH);
lcd.print("/");
buttonSetCounter++;
} //end of (if buttonSetCounter==4)
if (buttonSetCounter == 5)
{
stringYEAR[0] = Digitalchar[buttonPushCounter/10];
stringYEAR[1] = Digitalchar[buttonPushCounter%10];
lcd.setCursor(6,0);
lcd.print(stringYEAR);
buttonSetCounter++;
} //end of (if buttonSetCounter==5)
else if (buttonSetCounter == 6)
{
WRITETIME();
WRITEDATE();
clock.setTime();
DISPLAYTIMENDATE();
} //end of (if buttonSetCounter==3)
if(buttonSetCounter > 7)
{
return;
} //end of else if (buttonSetCounter > 3)
} //end of if (buttonState3 == HIGH)
} //end of if (buttonState3 != lastButtonState3)
} //end of void setuptime
void WRITETIME()
{
int hours = (stringHOUR[0]-'1'+1)*10 + (stringHOUR[1]-'1'+1);
int minutes = (stringMIN[0]-'1'+1)*10 + (stringMIN[1]-'1'+1);
int seconds = (stringSEC[0]-'1'+1)*10 + (stringSEC[1]-'1'+1);
clock.fillByHMS(hours ,minutes , seconds);
} //end of write time
void WRITEDATE()
{
int dayofmonth = (stringDATE[0]-'1'+1)*10 + (stringDATE[1]-'1'+1);
int month = (stringMONTH[0]-'1'+1)*10 + (stringMONTH[1]-'1'+1);
int year = (stringYEAR[0]-'1'+1)*10 + (stringYEAR[1]-'1'+1);
clock.fillByYMD(year, month, dayofmonth);
} //end of write date
void DISPLAYTIMENDATE()
{
lcd.print(clock.hour, DEC);
lcd.print(":");
lcd.print(clock.minute, DEC);
lcd.print(":");
lcd.print(clock.second, DEC);
lcd.setCursor(0, 1); //date is bottom row
lcd.print(clock.dayOfMonth, DEC);
lcd.print("/");
lcd.print(clock.month, DEC);
lcd.print("/");
lcd.print(clock.year+2000, DEC);
lcd.print(" ");
lcd.print("*");
switch (clock.dayOfWeek)// Friendly printout the weekday
{
case MON:
lcd.print("MON");
break;
case TUE:
lcd.print("TUE");
break;
case WED:
lcd.print("WED");
break;
case THU:
lcd.print("THU");
break;
case FRI:
lcd.print("FRI");
break;
case SAT:
lcd.print("SAT");
break;
case SUN:
lcd.print("SUN");
break;
} //end of switch case
} //end of DISPLAYTIME()
