Just need a help on my small project. I just want to have a fixed text on the first row of the LCD. Then, I want a scrolling text on the second row of the LCD. I have already done that using some sample codes available in the net. Then, I have this pushbutton to clear the previous display (fixed and the running text) with a new display. However, I can do this after the whole scrolling text is displayed. My problem is that, I want to change the display as long as the PB is pressed, not just by waiting the entire scrolling text to be displayed.
I have this code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(32,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
int PB = 2;
void scrollMessage(int row, String message, int delayTime, int totalColumns) {
for (int i=0; i < totalColumns; i++) {
message = " " + message;
}
message = message + " ";
for (int position = 0; position < message.length(); position++) {
lcd.setCursor(0, row);
lcd.print(message.substring(position, position + totalColumns));
delay(delayTime);
}
void setup() {
pinMode(PB, INPUT_PULLUP);
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
while(digitalRead(PB) == 1){
lcd.setCursor(1,0);
lcd.print("Hello World);
delay(500);
scrollMessage(1, "Welcome!", 250, 16);
}
if (digitalRead(PB) == 0 )
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Let's begin!")
}
Your code does not compile. Did you copy the code from somewhere else ?
This is your code that does compile:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(32, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display
int PB = 2;
void scrollMessage(int row, String message, int delayTime, int totalColumns)
{
for (int i = 0; i < totalColumns; i++)
{
message = " " + message;
}
message = message + " ";
for (int position = 0; position < message.length(); position++)
{
lcd.setCursor(0, row);
lcd.print(message.substring(position, position + totalColumns));
delay(delayTime);
}
}
void setup()
{
pinMode(PB, INPUT_PULLUP);
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop()
{
while (digitalRead(PB) == 1)
{
lcd.setCursor(1, 0);
lcd.print("Hello World");
delay(500);
scrollMessage(1, "Welcome!", 250, 16);
}
if (digitalRead(PB) == 0)
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Let's begin!");
}
}
The scrolling works in Wokwi simulation:
You ask a question about a simple and small project, but a scrolling text or a "news ticker" is actually hard to make, even for me Perhaps there is a library that can do that.
Can you remove the while-statement from the loop(). If you put a delay(500); at the end of the loop, then you can scroll the text each time the loop() runs.
You keep adding spaces to the String, that means that the SRAM memory is getting filled and after a while the sketch will crash.
The digitalRead() returns a HIGH or LOW, can you use that instead of 1 and 0 ?
you should reconsider if you really meant that were "simple".
Still awful code, but could do somehow what you asked for:
some phrases to google and read just in case you would ask:
Arduino BlinkWithoutDelay
Arduino StateChangeDetection
Arduino Debounce
The Evils of Arduino Strings
C++ structure
C++ array
C++ auto based for loop
C++ static keyword
if that's not enough, I guess the answer to the next question will be google for:
Arduino Finite State Machine