I am building a watch based off the Hello World example wiring (minus the potentiometer) and I added some neat binary patterns in the background to give it some flare, you know? Anyways, I didn't want to make an if statement with millis() 32 times for every binary digit, so i made the decals run on a delay between each digit change for 6 seconds, then repeated it 10 times. However when i checked back on my watch around a half hour later, it was running 45 seconds behind! I checked through my code multiple times but couldn't find a timing inconsistency. Please help!
My code (sorry it's only partially documented):
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int Minute = 8;
int Ten = 4;
int Hour = 5;
int Hour2 = 0;
bool ap = false;
long binary;
byte Line[8] = {
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100
};
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
randomSeed(analogRead(0));
}
void loop() {
//=======This part writes all the info down
lcd.setCursor(6, 0);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
lcd.setCursor(6, 0);
lcd.print(Hour2);
lcd.setCursor(7, 0);
lcd.print(Hour);
lcd.setCursor(8, 0);
lcd.print(Ten);
lcd.setCursor(9, 0);
lcd.print(Minute);
lcd.setCursor(7, 1);
lcd.createChar(0, Line);
lcd.setCursor(5, 0);
lcd.write(byte(0));
lcd.setCursor(5, 1);
lcd.write(byte(0));
lcd.setCursor(10, 0);
lcd.write(byte(0));
lcd.setCursor(10, 1);
lcd.write(byte(0));
lcd.setCursor(7, 1);
if (ap == true) lcd.print("AM");
if (ap == false) lcd.print("PM");
//=======Adds one minute
Minute = Minute + 1;
//=======Binary Decals / Timer
for(int i = 0; i < 10; i++)
{
delay(250);
binary = random(0, 2);
lcd.setCursor(15, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(15, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(14, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(14, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(13, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(13, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(12, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(12, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(11, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(11, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(4, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(4, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(3, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(3, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(2, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(2, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(1, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(1, 0);
lcd.print(binary);
delay(250);
binary = random(0, 2);
lcd.setCursor(0, 1);
lcd.print(binary);
binary = random(0, 2);
lcd.setCursor(0, 0);
lcd.print(binary);
delay(250);
lcd.setCursor(15, 1);
lcd.print(" ");
lcd.setCursor(15, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(14, 1);
lcd.print(" ");
lcd.setCursor(14, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(13, 1);
lcd.print(" ");
lcd.setCursor(13, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(3, 1);
lcd.print(" ");
lcd.setCursor(3, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(2, 1);
lcd.print(" ");
lcd.setCursor(2, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(1, 1);
lcd.print(" ");
lcd.setCursor(1, 0);
lcd.print(" ");
delay(250);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(" ");
delay(1000);
}
if (Minute == 10) Ten = Ten + 1;
if (Minute == 10) Minute = 0;
if (Ten == 6) Hour = Hour + 1;
if (Ten == 6) Ten = 0;
if (Hour == 10) Hour2 = Hour2 + 1;
if (Hour == 10) Hour = 0;
lcd.setCursor(7, 1);
if (ap == true) lcd.print("AM");
if (ap == false) lcd.print("PM");
if (Hour2 == 1 && Hour == 3) {
Minute = 0;
Ten = 0;
Hour = 1;
Hour2 = 0;
ap = !ap;
lcd.setCursor(7, 1);
if (ap == true) lcd.print("AM");
if (ap == false) lcd.print("PM");
}
}