I am a beginner Arduino user and having made a few things following instructions, I am trying to branch out a bit and make things myself. I have made a circuit where a 16x2 LCD screen and a push button with pull up resistor are connected to my Arduino Uno R3. The LCD screen displays a message when the button is pressed.
I had success with a static short message (Hello World) and then decided to try scrolling a longer message.
I will put the code below. The scrolling is working but the problem I have is that at the end of the message, it starts to write it again until the first dispay of the message completely disappears to the left.
Using the example below, before the 'y' of display disappears, I already have 'Real' on the RHS as it starts the message again.
I have tried adding spaces and changing the maximum position counter number but that doesn't help.
I just want the message to display once and not start a second time. How do I do that?
Thanks.
// include the library code:
#include <LiquidCrystal.h>
/* initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to*/
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int ButtonPin = 10;
int ButtonVal = 0;
void setup() {
pinMode(ButtonPin, INPUT_PULLUP);
// set up the LCD's number of columns and rows
lcd.begin(16, 2);
}
void loop() {
ButtonVal = digitalRead(ButtonPin);
if (ButtonVal == LOW) {
// set the cursor to column 0, line 0
lcd.setCursor(0, 0);
lcd.print("Really long message to display");
// scroll 23 positions (string length) to the left to move it offscreen left:
for (int positionCounter = 0; positionCounter < 30; positionCounter++) {
// wait a bit
delay(250);
// scroll one position left:
lcd.scrollDisplayLeft();
}
//clear the screen
lcd.clear();
}
}
Thanks for the welcome. I saw the code thing in a reply when I was looking for answers!
Sorry, Right Hand Side.
What I want to prevent is the message starting again as the end of scrolls away to the left. You can see that happening in the photo. I just want the message to be displayed once and then stop. At the moment it stops when the 'y' of 'display' goes but by then I already have 'Rea' on the right hand side.
I modified an existing WOKWI-simulation and analysed how the LCD-code reacts on different lengths of the text.
For a single line you can print 40 characters
So by inserting spaces at the beginning the existing scroll-function will scroll in blanks which are not distinguishable from no printing at all
As your code uses a for-loop and delay() both things are blocking code
as long as the delay is active no other code can be executed
as long as you are in the for-loop only the code inside the for-loop is executed and no other code
This WOKWI-Simulation uses a non-blocking timer-function which enables to blink the blue LED all the time even in parallel with the scrolling of the text.
Doing things in parallel means more and a little bit more complex code.
It is very normal that you will need some time to understand it
Here is a tutorial about the basic priciple of non-blocking timing
Thank you very much for taking the time to do that. I have never seen the Wokwi simulator before, what a great tool!
I will work through the code and see if I can begin to understand it a bit more and then put it to use. I've got the hang of a few things just by changing bits and seeing what happens.