Hello everyone,
I’m working on a timer or clock, but I’m having trouble with the code I have so far. What I imagined this code would do is count up a second every second and display it on the screen. Every time it hits 60 seconds, I’d add a minute to the minute counter and reset the seconds to zero, just like any old stopwatch. I tried uploading this to see if things worked so far, but all I see are white spaces on the top row of my LCD screen.
I know it isn’t a wiring issue because I can upload the Hello World sketch just fine.
The code:
#include <LiquidCrystal.h>
int seconds = 0;
int prevsecstate = 0;
int minute = 0;
int prevminstate = 0;
int hours = 0;
int prevhourstate = 0;
unsigned long previousMillis = 0;
const long timer = 1000;
LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.print("clock");
delay(1000);
lcd.clear();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= timer) {
prevsecstate = seconds;
seconds = prevsecstate + 1;
lcd.setCursor(7, 0);
if (seconds < 10) {
lcd.print("0");
lcd.print(seconds);
}
else {
lcd.print(seconds);
}
}
else {
}
if (seconds == 60) {
seconds = 0;
minute = prevminstate + 1;
lcd.setCursor(4, 0);
if (prevminstate < 10) {
lcd.print("0");
lcd.print(minute);
}
else {
lcd.print(minute);
}
lcd.setCursor(6, 0);
lcd.print(":");
}
}
any assistance would be greatly appreciated. Thanks!
marco