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).
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;
}
}
}
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
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.
#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;
}
}
}
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
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.