Sorry for the frequent Forum Topics, but this is a different issue.
I have a Clock Code which runs on a lcd and I'm trying to make the clock adjustable with 2 buttons. One button toggles between units in the clock and the other adjusts it's value.
Heres my Clock code:
#include <LiquidCrystal.h>
#include <DateTimeStrings.h>
#include <DateTime.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup(){
DateTime.sync(DateTime.makeTime(0, 15, 11, 21, 10, 2009));
lcd.begin(16,2);
}
void loop(){
if(DateTime.available()) {
unsigned long prevtime = DateTime.now();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;
DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock
}
}
void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits,DEC);
}
void digitalClockDisplay(){
lcd.home();
// digital clock display of current time
lcd.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
lcd.setCursor(0,1);
lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
lcd.print(" ");
lcd.print(DateTimeStrings.monthStr(DateTime.Month));
lcd.print(" ");
lcd.print(DateTime.Day,DEC);
}
And here is my ATTEMPTED Clock with two buttons code:
#include <LiquidCrystal.h>
#include <DateTimeStrings.h>
#include <DateTime.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int button = 0;
int val;
int state;
int press = 0;
int button2 = 1;
int val2;
int state2;
int press2 = 0;
int ax = 0;
int bx = 0;
int cx = 0;
int dx = 0;
int ex = 0;
void setup(){
pinMode(button, INPUT);
pinMode(button2,INPUT);
Serial.begin(9600);
state = digitalRead(button);
state2 = digitalRead(button2);
DateTime.sync(DateTime.makeTime(0, 0, 0, 0, 0, 2009));
lcd.begin(16,2);
}
void loop(){
if(DateTime.available()) {
unsigned long prevtime = DateTime.now();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;
DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock
}
DateTime.sync(DateTime.makeTime(ax, bx, cx, dx, ex, 2009));
val = digitalRead(button);
val2 = digitalRead(button2);
if (val != state) {
if (val == LOW) {
press++;
}
}
if (press >=6) {
press = 0;
}
state = val;
if (val2 != state2) {
if (val2 == LOW) {
press2++;
}
}
state2 = val2;
switch (press) {
case 1:
ax = press2;
break;
case 2:
bx = press2;
break;
case 3:
cx = press2;
break;
case 4:
dx = press2;
break;
case 5:
ex = press2;
break;
}
if (ax >= 60) {
ax = press2 % 60;
}
if (bx >= 60) {
bx = press2 % 60;
}
if (cx >= 24) {
cx = press2 % 24;
}
if (dx >= 32) {
dx = press2 % 31;
}
if (ex >= 13) {
ex = press2 % 12;
}
}
void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits,DEC);
}
void digitalClockDisplay(){
lcd.home();
// digital clock display of current time
lcd.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);
lcd.setCursor(0,1);
lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
lcd.print(" ");
lcd.print(DateTimeStrings.monthStr(DateTime.Month));
lcd.print(" ");
lcd.print(DateTime.Day,DEC);
}
The second code does not work and I've tried positioning it in other ways, structuring it, linking it to buttons, using a third button to activate a 'void'. I have yet to figure it out.
If (anything is confusing) {
I will clarify;
}
else {
Thank you very much;
}