Hi there!
I wanted to build a clock without RTC module.
The code works well, but I've got a problem with the screen:
I use I2C module for LCD's:
``
This is the code:
#include <Timers.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int hour=0;
int minute=0;
int second=0;
char cmd[100];
byte cmdIndex;
Timer secAdd;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16,2);
lcd.print("Waiting for COM");
secAdd.begin(1000);
while(!Serial){}
lcd.clear();
}
void exeCmd() {
if (cmd[0] == 'h') {
unsigned int val = atof(cmd + 2);
hour=val;
}
if (cmd[0] == 'm') {
unsigned int val = atof(cmd + 2);
minute=val;
}
if (cmd[0] == 's') {
unsigned int val = atof(cmd + 2);
second=val;
}
}
void lcdPrint() {
lcd.clear();
lcd.write("TIME ");
lcd.write(char(hour));
lcd.write(":");
lcd.write(char(minute));
lcd.write(":");
lcd.write(char(second));
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
char c = (char)Serial.read();
if (c == '\n') {
cmd[cmdIndex] = 0;
exeCmd();
cmdIndex = 0;
} else {
cmd[cmdIndex] = c;
if (cmdIndex < 99) cmdIndex++;
}
}
if(secAdd.available()){
second=second+1;
secAdd.restart();
lcdPrint();
}
if(second==60){
second=0;
minute=minute+1;
lcdPrint();
}
if(minute==60){
minute=0;
hour=hour+1;
lcdPrint();
}
if(hour==24){
hour=0;
lcdPrint();
}
}
Thank you in advance!

