Scroll and stop text on LCD

Hello!

I created the program for scrolling text on LCD display and now I want to stop this move using the button. When I stop, the text moves to the starting position, but I want to stop on the position before the using button (for example on the middle of display).

Does anyone have ideas how to do this? :slight_smile:

Need the program !!

Read this first

https://forum.arduino.cc/index.php?topic=97455.0

Then post your complete code here so folks can advise you properly.

I'm sorry. I'm new here. So, this is my code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           

int screenWidth = 16;
int screenHeight = 2;

String line1 = "line 1";
String line2 = "line 2";

int stringStart, stringStop = 0;
int scrollCursor = screenWidth;

int lcd_key     = 0;
int adc_key_in  = 0;

#define btnLEFT   3
int read_LCD_buttons() {           
    adc_key_in = analogRead(0);  
}

  

void setup()
{   lcd.begin(16, 2);  
} // start the library



 void loop() 
 {
{  lcd.setCursor(scrollCursor, 1);
  lcd.print(line2.substring(stringStart,stringStop));
  lcd.setCursor(0, 0);
  lcd.print(line1);
  delay(300);
  lcd.clear();
  
  if(stringStart == 0 && scrollCursor > 0){
    scrollCursor--;
    stringStop++;
  } else if (stringStart == stringStop){
    stringStart = stringStop = 0;
    scrollCursor = screenWidth;
  } else if (stringStop == line2.length() && scrollCursor == 0) {
    stringStart++;
  }
    else if (btnLEFT){      
        lcd.print(line2.substring(stringStop));
  } 
  else {
    stringStart++;
    stringStop++;
  }
}
}

I'm beginner and I wrote this program with help on one internet site. It works, so I think it is good. But it doesn't work as I would like.

Hello! :slight_smile:

I want to do easy control of coursor with using buttons on LCD display. When I push Right, the cursor is going to right and etc.

I had idea to using lcd.setCursor(i,i) to the start position and next moving the coursor like this lcd.setCursor(i,i++);, but it doesn't work. What am I doing wrong?

Here is all of my code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel
// LiquidCrystal(rs, enable, d4, d5, d6, d7) 

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3


int read_LCD_buttons(){               // read the buttons
    adc_key_in = analogRead(0);       // read the value from the sensor 

}
 

void setup(){
   lcd.begin(16, 2);               // start the library
 }
 
void loop(){
int i = 0;
   lcd.setCursor(i,i);             
   lcd.print("x");      
   //lcd.print(adc_key_in);

   lcd_key = read_LCD_buttons();   

   switch (lcd_key){               
    
       case btnRIGHT:{             
            lcd.setCursor(i,i++);
            break;
       }
       case btnLEFT:{
             lcd.setCursor(i,i--); 
             break;
       }    
       case btnUP:{
             lcd.print("UP    ");  
             break;
       }
       case btnDOWN:{
             lcd.print("DOWN  ");  
             break;
       }
       
       
   }
}
   int i = 0;
   lcd.setCursor(i,i);

Using the same variable for both column (character position) and row (line) is not going to work for very long.

case btnRIGHT:{             
            lcd.setCursor(i,i++);

The syntax for setCursor is

lcd.setCursor(column, line);

Thanks. So I should change my way of thinnking and don't use variables? Or use 2 different variables?

Or use 2 different variables?

There you go.

What types of function can I use to stop this scrolling?

Thank you!

I changed the code like this:

void loop(){
int i = 0;
int a = 0;
   lcd.setCursor(i,a);             
   lcd.print("x");      
   //lcd.print(adc_key_in);

   lcd_key = read_LCD_buttons();   

   switch (lcd_key){               
    
       case btnRIGHT:{             
            lcd.setCursor(i, a++);
            break;
       }

But it doesn't work. "x" is still on the same position and pressing the button changes nothing :frowning:

void loop(){
int i = 0;
int a = 0;

because a is destroyed at the end of loop() and recreated and initialized to zero at the beginning of loop()

try:

void loop(){
int i = 0;
static int a = 0;

and note what happens...

Please post your full program when posting code. I don't know how variables and constants are declared, the code for any functions nor the rest of loop(). And , at some point, I might want to run your code which I can't do if it is not complete.

void loop(){
int i = 0;
int a = 0;
   lcd.setCursor(i,a);

Every time loop() starts i and a are set to 0, then those values are used to position the cursor at 0,0. Then later you change one or both, but as soon as loop starts they are set back to 0. Make i and a global in scope or make them static so that changes in their value are persistent.

Nothing will show till you print something to the LCD.

case btnRIGHT:{     
lcd.setCursor(i, a++);

You are incrementing the line (row 0-1) number, not the column (character position 0-15) number. I pointed that out before.

This is my full program:

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel
// LiquidCrystal(rs, enable, d4, d5, d6, d7) 

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3


int read_LCD_buttons(){               // read the buttons
    adc_key_in = analogRead(0);       // read the value from the sensor 

}
 

void setup(){
   lcd.begin(16, 2);               // start the library
 }
 
void loop(){
int i = 0;
static int a = 0;
   lcd.setCursor(i,a);             
   lcd.print("x");      
   //lcd.print(adc_key_in);

   lcd_key = read_LCD_buttons();   

   switch (lcd_key){               
    
       case btnRIGHT:{             
            lcd.setCursor(i++, a);
            break;
       }
       case btnLEFT:{
             lcd.setCursor(i--, a); 
             break;
       }    
       case btnUP:{
             lcd.setCursor(i, a++);  
             break;
       }
       case btnDOWN:{
             lcd.setCursor(i, a--);  
             break;
       }
       
       
   }
}

I added static int a = 0;
and nothing was changed

int i = 0;

oopsie

plus, when do you initialize all these button pins as inputs?

aleoo:
What types of function can I use to stop this scrolling?

perhaps try opening yet another thread...

Hmm. I don't know. I have the sample program and it works without this initialization and I wanted modify this code. But maybe you're right and I should initialize these buttons button pins as inputs :slight_smile:

I also add

static int i = 0;
static int a = 0;

and then I have the whole display in "xxxxx"

I should initialize these buttons button pins as inputs

I don't think that that will help.

int read_LCD_buttons(){               // read the buttons
    adc_key_in = analogRead(0);       // read the value from the sensor
}

analogRead returns a number between 0 and 1023. Your read_LCD_buttons() should return an int but since there is no return statement, the function returns nothing. If the read_LCD_buttons did return a meaningful number, the number would be between 0 and 1023, but your switch only reacts to 0, 1, 2 3. See how the buttons are handled in this tutorial.

groundFungus:
I don't think that that will help.

Like I said the first prgram (show the text when the button is pressed) is working without initialization, so I think the problem is somewhere else..

int read_LCD_buttons(){               // read the buttons
    adc_key_in = analogRead(0);       // read the value from the sensor
}

What should I do with this lines? Is it wrong?

what happens in the Serial monitor when you load this and press buttons:

int read_LCD_buttons() 
{
  return analogRead(0);       // read the value from the sensor
}

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  if(int newPress = read_LCD_buttons())
  {
    Serial.print(F("Button Pressed:"));
    Serial.println(newPress);
  }
}

BulldogLowell:
what happens in the Serial monitor when you load this and press buttons:

I have full screen with text:

Button Pressed:1023
Button Pressed:1023
Button Pressed:1023
Button Pressed:1023