I'm looking for help to code a very basic text scroll with just two buttons and an lcd (which I have wired already).
My end result goal:
Text 1
Text 2
Text 3
Text 4
Text 5
I would like the LCD to show only one text at a time, where the down button shows the next text each time it is pressed and the up button shows the previous text each time it is pressed. It doesn't seem too complex but I can't seem to get it started. Any help is much appreciated, thanks.
Code so far:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int upButton = 7;
int downButton = 8;
void setup(){
lcd.begin(16,2);
}
void loop(){
int upButtonState = digitalRead(upButton);
int downButtonState = digitalRead(downButton);
}
start by detecting if either of the buttons were pressed:
I am using the internal Pull-Up resistors for the switches, so you would wire the pushbutton switches to the pin and to ground, without any resistor...
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int lastUpState;
int lastDownState;
int upButton = 7;
int downButton = 8;
void setup()
{
Serial.begin(115200);
lcd.begin(16,2);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
}
void loop()
{
int upButtonState = digitalRead(upButton);
if (upButtonState == LOW && lastUpState == HIGH)
{
Serial.println("You pressed UP");
// up pushed stuff
}
lastUpState = upButtonState;
//
int downButtonState = digitalRead(downButton);
if (downButtonState == LOW && lastDownState == HIGH)
{
Serial.println("You pressed DOWN");
// Down push stuff
}
lastDownState = downButtonState;
}
Thanks for the help! I was playing with it but was unable to get it to keep scrolling through multiple texts. Its difficult for me to explain but it seems very simple. I would imagine the code to look something like this (with 5 different texts):
Program starts- blank lcd
Press up button- display text1
Press up button again- clear text1 and display text2
etc...until text 5 is reached, then the up button would display text 1 again and start over
Press down button- go to previous text
OR
Program starts- blank lcd
Press down button- display text 5
press down button again- display 4
etc...repeating in a type of loop
I'm imagining this as the up and down buttons scrolling through each text in a loop so even when the program starts you could hit the up or down button to scroll through.
Understood, you could try something like this then (not tested)...
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int lastUpState;
int lastDownState;
int state = 0;
int lastState = -1;
int upButton = 7;
int downButton = 8;
void setup()
{
Serial.begin(115200);
lcd.begin(16,2);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
}
void loop()
{
int upButtonState = digitalRead(upButton);
if (upButtonState == LOW && lastUpState == HIGH)
{
Serial.println("You pressed UP");
state++;
if (state > 5) state = 5;
}
lastUpState = upButtonState;
//
int downButtonState = digitalRead(downButton);
if (downButtonState == LOW && lastDownState == HIGH)
{
Serial.println("You pressed DOWN");
state--;
if (state < 0) state = 0;
}
lastDownState = downButtonState;
if (lastState != state)
{
switch (state){
case 0:
Serial.println("The present state is zero");
break;
case 1:
Serial.println("The present state is one");
break;
case 2:
Serial.println("The present state is two");
break;
case 3:
Serial.println("The present state is three");
break;
case 4:
Serial.println("The present state is four");
break;
case 5:
Serial.println("The present state is five");
break;
default:
;//
}
}
lastState = state;
}