I am trying to edit some code that will display time for 5 minutes or so on 16 by 2 lcd display. The following code works only for a few seconds and resets after that. Is it possible to achieve this using Arduino without using any external module?
#include<LiquidCrystal.h>
int contrast=100;
int h=9;
int m=12;
int s=9;
int bl=10;
const int backlight=120;
static uint32_t last_time, now = 0;
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
analogWrite(6,contrast);
lcd.begin(16,2);
analogWrite(bl,backlight);
now=millis();
}
void loop()
{
lcd.setCursor(0,1);
lcd.print("Time ");
if(h<10)lcd.print("0");
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
for ( int i=0 ;i<4 ;i++)
{
while ((now-last_time)<250)
{
now=millis();
}
last_time=now;
}
s=s+1;
if(s==60){
s=0;
m=m+1;
}
if(m==60)
{
m=0;
h=h+1;
}
if(h==13)
{
h=1;
}
}
The Arduino can keep time and format it for a display using the Arduino Time library <TimeLib.h>.
However, it won't keep time very accurately, because the CPU clock is not particularly accurate. The Time library can be synchronized to external sources for accurate timekeeping.
Example:
//time library demo
#include <TimeLib.h>
void setup() {
Serial.begin(115200);
delay(2000); //wait for connection
Serial.print("Setting clock to ");
// set internal clock time and date.
// setTime(hr,min,sec,day,mnth,yr);
setTime(12,0,0,9,3,2024);
print_date_time();
}
void print_date_time() { //easy way to print date and time
char buf[40];
sprintf(buf, "%02d/%02d/%4d %02d:%02d:%02d", day(), month(), year(), hour(), minute(), second());
Serial.println(buf);
}
void loop() {
static int minutes_past_midnight, last_print_time = 0;
// check the clock
minutes_past_midnight = hour() * 60 + minute(); //useful for alarms and timing functions
// every minute, print the data and time
if (minutes_past_midnight != last_print_time) {
last_print_time = minutes_past_midnight;
print_date_time();
}
}
Time keeping variables should be of type unsigned (uint32_t).
I think the for loop should be removed. What is it supposed to do?
For proper line out of your code press ctrl-t.
The following code works only for a couple of minutes. It resets after that. It appears that there is no way out.
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long startTime; // Variable to store the start time
void setup() {
analogWrite(6,100);
lcd.begin(16, 2);
Wire.begin();
startTime = millis(); // Save the start time
}
void loop() {
// Calculate elapsed time since the start
unsigned long elapsedTime = millis() - startTime;
// Calculate current time
int hour = (elapsedTime / 3600000 + 9) % 24; // Calculate hours, adding 9 hours for start time
int minute = (elapsedTime % 3600000) / 60000; // Calculate minutes
int second = (elapsedTime % 60000) / 1000; // Calculate seconds
// Display the time on LCD
lcd.clear();
lcd.setCursor(5, 0);
if (hour < 10) {
lcd.print("0");
}
lcd.print(hour);
lcd.print(":");
if (minute < 10) {
lcd.print("0");
}
lcd.print(minute);
lcd.print(":");
if (second < 10) {
lcd.print("0");
}
lcd.print(second);
// Delay for one second
delay(1000);
}
For my Arduino devices attempting time tracking without a Real Time Clock device, I have global variables for the hours, minutes, and seconds (unsigned ints). I use the “Blink Without Delay” example to update the time once per 1000 millis with a cascading series of if statements to handle the rollover for seconds, minutes, and hours in that order.
So, something like this:
// Arduino Time Code
/* code by jeremy as typed on a tablet from memory
Example use only. */
unsigned int secondCount = 0;
unsigned int minuteCount = 0;
unsigned int hourCount = 9;
unsigned long previousMillis = 0;
unsigned int secondInterval =1000;
void setup() {
// <setup stuff here>
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > secondInterval) {
previousMillis = currentMillis;
secondCount++;
if(secondCount >59) {
secondCount =0;
minuteCount++;
if(minuteCount >59) {
minuteCount =0;
hourCount++;
if(hourCount >23) {
hourCount =0;
// <insert midnight tasks here>
}
}
}
}
}