Hello all,
I want to display these two words "Charging Battery" where the letters print in sequence with an interval of say delay(100);
After I gave up finding similar string of characters or array to summarize the code in a few lines I did the following:
Inside the for loop your variable a is an integer number that has nothing to do with your string. In fact as you are using the same name, the string will not be available inside the for loop.
Don't waste your time and memory using String objects. Especially on AVR processors, like the Arduino Uno, they cause memory problems and crashes. Use C-strings instead (zero terminated character arrays);
Well, a lot to learn! Lots of thanks for all.
All posts worked fine, however, I couldn't play around with groundFungus's.
I need to get a flashing effect displaying/ clearing the text and so on while the battery is charging.
I tried reversing syntax by replacing 0's with 1's and ++ with -- and if not or = etc but I only got ccccccs even if I add delays for the text to fully display and then clear.
Also (all posts) found obstacles to get the text displayed on (0,1) the second line, as I need the first line to display the battery state of charge and temperature. They all print on the first line by default. lcd.Setcursor(0,1); works when placed in setup.
/* On the way for a fancy Lithium Ion battery charger* October 2023*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int LED = 13;
//char text[] = "battery charging complete";// too long for LCD, OK for serial
char text[] = "Charing Battery";// for LCD, array of text characters
byte index = 0; // indices and pointers
bool pause = 0;// boolean algebra 0, 1.
unsigned long timer = 0;
//unsigned long interval = 1000; // delay between chars
unsigned long interval = 50; // delay between chars, fast enough a flashing effect!
void setup()
{
lcd.init();
lcd.backlight();
pinMode(LED, OUTPUT);
Serial.begin(115200);//
//lcd.setCursor(0,1);
}
void loop()
{
//digitalWrite (LED, HIGH);
//delay(200);
//digitalWrite (LED, LOW);
//delay(200);
//DisplayText0();
//DisplayText1();
DisplayText2();
}
void DisplayText0()// worked fine but prints on the first line lcd(0,0) by default!
{
String S = {"Charging Battery"};
for (int a = 0; a<16; a++)
{
lcd.print (S[a]); // let's display whatever character is the a-th position of S
//lcd.print(S[a],1)//I mean the second line! It doesn't work, replacing the 1 with 0 doesn't work either!
delay(50);
}
//delay(100);
lcd.clear();
for (int a = 0; a<16; a++)
{
lcd.print (S[a]); // let's display whatever character is the a-th position of S
delay(50);
}
}
void DisplayText1()
{
char s[]="Charging Battery";
for (int i=0; i<strlen(s); i++)
{
lcd.print(s[i]);
delay(50);
}
delay(200);
lcd.clear();
}
void DisplayText2()
{
char c = text[index];
if (c != NULL && pause == 0)
{
Serial.print(c);
lcd.print(c);
index++;
timer = millis();
pause = 1;
}
if (pause == 1 && millis() - timer >= interval)
{
pause = 0;
}
//delay(100);
//lcd.clear();
//index--;
// timer = millis();
// lcd.home();doesn't work!
//lcd.clear(); doesn't work!
//lcd.print(0);doesn't work either!
//lcd.clear();
}
It can get a bit complex if you are going to be using the display in other parts of the code while displaying the text. That will change the cursor position, so you need to set the cursor position before displaying each character. To get a flashing effect, you could just erase the text completely and start again from the beginning, or erase the letters individually. It would also be nice to be able to change the text, in case you want other messages to be displayed.
/* On the way for a fancy Lithium Ion battery charger* October 2023*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long interval = 50; // delay between chars, fast enough a flashing effect!
void setup()
{
lcd.init();
lcd.backlight();
Serial.begin(115200);
}
void loop()
{
if (((millis() / 30000) % 2) == 0) { //changes text every 30 seconds
DisplayText("Charging Battery");
} else {
DisplayText("Discharging Battery");
}
//this code displays a seconds count on the LCD
static unsigned int time = 0;
if ((millis() / 1000) != time) {
time = millis() / 1000;
lcd.setCursor(0, 0);
lcd.print(millis() / 1000);
}
}
void DisplayText(const char* s)
{
static size_t index = 0;
static unsigned long timer = millis();
static bool erase = false;
static const char* sPrevious = NULL;
//reset if the text has changed
if (s != sPrevious) {
sPrevious = s;
index = 0;
erase = false;
timer = millis() - interval;
}
if ((millis() - timer) >= interval) {
timer = millis();
if (s[index]) { //false for terminating NULL, true for any other value
lcd.setCursor(index, 1); //set the cursor position, assuming other display code has changed it
if (erase) {
lcd.print(' ');
} else {
lcd.print(s[index]);
}
index++;
} else { //reset index and change erase state when end of text is reached
erase = !erase;
index = 0;
}
}
}