I got a Arduino Leonardo to play with Without any programming exprience, the first thing I tried to do with it is connecting a RTC and LCD (both I2C) to display date/time/texts in pages.
With help of examples it's working in the basics It starts with showing "Good morning/midday/evening/night" depending on the hour, a welcome-text, the date and the time. Each page is displayed for 3 seconds.
But now I want the time-page to show the running seconds as well. At this moment the seconds are just static, not updated in the 3 seconds it's showing
I want to have the seconds updating during the 3 seconds the time is shown, but I don't know how to do that I already have looked at the "BlinkWithoutDelay" example, but doesn't understand how I can use it in my case.
To make it easier to work with, I've moved the code for showing the time into a separated function tijdtonen(). At this moment the code works but the seconds are not updated during the time it's shown.
In essence, I only need it to run the tijdtonen() part for 3 seconds and then continue where it was in the loop().
This is the whole sketch I have now:
/*
Klok met meerdere paginas.
*/
// Include Wire Library for I2C
#include <Wire.h>
// Include NewLiquidCrystal Library for I2C
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 RTC;
// Define LCD pinout
const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
// Define I2C Address - change if reqiuired
const int i2c_addr = 0x27;
LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
void setup()
{
// Set display type as 16 char, 2 rows
lcd.begin(16,2);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop()
{
DateTime now = RTC.now();
// Backlight aan
lcd.backlight();
// Dagdeel tonen
lcd.setCursor(2,0);
if (now.hour() <= 5) {
lcd.print("Goede nacht!");
}
if (now.hour() <= 11 && now.hour() >= 6) {
lcd.print("Goede ochtend!");
}
if (now.hour() <= 17 && now.hour() >= 12) {
lcd.print("Goede middag!");
}
if (now.hour() <= 23 && now.hour() >= 18) {
lcd.print("Goede avond!");
}
// Wacht 3 secondes
delay(3000);
// LCD leegmaken
lcd.clear();
// Welkom-tekst
lcd.setCursor(5,0);
lcd.print("Ik heet");
lcd.setCursor(3,1);
lcd.print("U welkom!");
// Wacht 3 secondes
delay(3000);
// LCD leegmaken
lcd.clear();
// Datum
lcd.setCursor(0,0);
lcd.print("Het is vandaag:");
lcd.setCursor(1,1);
// Dag-naam tonen
if (now.dayOfTheWeek() == 0) {
lcd.print("zo ");
}
if (now.dayOfTheWeek() == 1) {
lcd.print("ma ");
}
if (now.dayOfTheWeek() == 2) {
lcd.print("di ");
}
if (now.dayOfTheWeek() == 3) {
lcd.print("wo ");
}
if (now.dayOfTheWeek() == 4) {
lcd.print("do ");
}
if (now.dayOfTheWeek() == 5) {
lcd.print("vr ");
}
if (now.dayOfTheWeek() == 6) {
lcd.print("za ");
}
if (now.day() < 10) {
lcd.print("0");
}
lcd.print(now.day(), DEC);
lcd.print('-');
if (now.month() < 10) {
lcd.print("0");
}
lcd.print(now.month(), DEC);
lcd.print('-');
lcd.print(now.year(), DEC);
// Wacht 3 secondes
delay(3000);
// LCD leegmaken
lcd.clear();
// De tijd
tijdtonen();
// Wacht 3 secondes
delay(3000);
// LCD leegmaken
lcd.clear();
}
void tijdtonen()
{
DateTime now = RTC.now();
lcd.setCursor(4,0);
lcd.print("De tijd:");
lcd.setCursor(4,1);
if (now.hour() < 10) {
lcd.print("0");
}
lcd.print(now.hour(), DEC);
lcd.print(':');
if (now.minute() < 10) {
lcd.print("0");
}
lcd.print(now.minute(), DEC);
lcd.print(':');
if (now.second() < 10) {
lcd.print("0");
}
lcd.print(now.second(), DEC);
}