I apologize if the post is not the subject of this forum (because of the board used).
Hi!
I do a little work in this free time and I started to run this sketch (it is not my work 100%) with an NTP clock and LCD 16x2 display based on an ESP32 board (this is the entire circuit).
I made small changes, many lines are commented, but my problem is the piece of program located between //****** //****** of the local function "printLocalTime()". This function is called every 1s in the loop. This "piece of program" should introduce an effect to the displayed text, and it has "String txtMsg = day_string;" as input , but it also includes two "delay(50)" that produce another delay when the data is displayed on the LCD.
So, I tried to replace the "delay" functions with millis, but now I can only see the last letter of the day, for example "y" from "Monday", nothing else happens. I anticipate that "currentMillis" from the local function is not updated, or quick reset, I think (I declared a global variable, and incremented in the loop.). However, what would you think? Maybe I can do something.
I have had fights with this type of situation before, and I can't say that I won. ![]()
#include <WiFi.h>
#include "time.h"
#include "sntp.h"
#include <LiquidCrystal_I2C.h>
//0x3F or 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2); //LCD Object
const char* ssid = "$$$$$";
const char* password = "$$$$$$";
const char* ntpServer1 = "ro.pool.ntp.org";
const char* ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 7200;
const int daylightOffset_sec = 3600;
const unsigned long eventInterval = 3000;
unsigned long previousTime = 0;
char *daynames[7] = {
"Duminica",
" Luni",
" Marti",
"Miercuri",
" Joi",
" Vineri",
" Sambata"
}; // put them in this order as tm struct 0 = Sunday
//*****************************************************************************
int startPoint;
int endPoint;
int i, j;
// speed of the text movement
int speed = 50;
// text to display
//String txtMsg = "Hello my Arduino!";
**unsigned long previousMillis = 0; **
**unsigned long previousMillis1 = 0;**
**unsigned long previousMillis2 = 0;**
**unsigned long currentMillis = 0;**
//*****************************************************************************
void printLocalTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("No time available (yet)");
return;
}
//lcd.print(&timeinfo, "%A%H:%M:%S"); // e.g Tuesday 10:30:05
//lcd.print(&timeinfo, "%d %B %Y"); //e.g November 22 2022
//Display Time
lcd.setCursor(0, 0);
lcd.print(&timeinfo, "%H:%M:%S"); // 10:30:05
// Display Date
char *day_string = daynames[timeinfo.tm_wday];
char buff[64] = "";
snprintf(buff, 64, "Day: %s", day_string);
Serial.println(buff);
//lcd.setCursor(0, 1);
//lcd.print(day_string);
//*********************************************************************
String txtMsg = day_string;
startPoint = 0; //set starting point
endPoint = 8; //set ending point
lcd.setCursor(0, 1);
//lcd.clear();
lcd.print(" ");
//for each letter of the string starting from the last one.
//unsigned long currentMillis = millis();
for (i = txtMsg.length() - 1; i >= 0; i--)
{
//unsigned long currentMillis = millis();
startPoint = 0;
//for each position on the LCD display
**if (currentMillis - previousMillis >= 50) {**
** previousMillis = currentMillis;**
for (j = 0; j < endPoint; j++)
{
**if (currentMillis - previousMillis1 >= 50) {**
** previousMillis1 = currentMillis;**
lcd.setCursor(startPoint, 1);
lcd.print(txtMsg[i]);
}
//**delay(speed);** // need to be replaced
if (startPoint != endPoint - 1) {
lcd.setCursor(startPoint, 1);
lcd.print(' ');
}
startPoint++;
}
endPoint--;
}
//**delay(speed);** // need to be replaced
}
//********************************************************************
} // printLocalTime
// Callback function (get's called when time adjusts via NTP)
void timeavailable(struct timeval *t)
{
Serial.println("Got time adjustment from NTP!");
printLocalTime();
}
void setup()
{
Serial.begin(115200);
// Setup LCD with backlight and initialize
lcd.init();
lcd.backlight();
// set notification call-back function
sntp_set_time_sync_notification_cb( timeavailable );
/**
NTP server address could be aquired via DHCP,
NOTE: This call should be made BEFORE esp32 aquires IP address via DHCP,
otherwise SNTP option 42 would be rejected by default.
NOTE: configTime() function call if made AFTER DHCP-client run
will OVERRIDE aquired NTP server address
*/
sntp_servermode_dhcp(1); // (optional)
/**
This will set configured ntp servers and constant TimeZone/daylightOffset
should be OK if your time zone does not need to adjust daylightOffset twice a year,
in such a case time adjustment won't be handled automagicaly.
*/
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
lcd.clear();
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print(ssid);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
lcd.clear();
lcd.print("CONNECTED");
delay(2000);
} // setup
void loop()
{
**currentMillis = millis();**
** //delay(1000);**
** if (currentMillis - previousMillis2 >= 1000) {**
** previousMillis2 = currentMillis;**
** printLocalTime(); // it will take some time to sync time :)**
** }**
}