Button press to change LCD screen

Hi, I need help with the programming of my latest project. So I have a 16 x 2 LCD display and I had it displaying Something. So every time a button is pressed, I want the screen to change its text. the button is connected to port 2. Thanks for your help!

Please show us your codes, what you have so far. how connect the button?

Hi, I will be using ports (7, 8, 9, 10, 11, 12) for the lcd and port 2 for the button. So on the first button click it will print "Hi" and on the second click on the button it will print "How are You" I need help on how to program it. Thanks!

If you have any code post it so we can see it.

If you have no code I suggest you start by writing a simple program using Serial.println() to show things on the Arduino Serial Monitor. Study the examples that come with the Arduino IDE.

...R

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// set up a constant for the tilt switchPin
const int switchPin = 2;

int hits = 0;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

void setup() {
// set up the number of columns and rows on the LCD
lcd.begin(16, 2);

// set up the switch pin as an input
pinMode(switchPin,INPUT);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hit the button");
lcd.setCursor(0, 1);
lcd.print("to increment");
}

void loop() {
// check the status of the switch
switchState = digitalRead(switchPin);

// compare the switchState to its previous state
if (switchState != prevSwitchState) {
if (switchState == LOW) {
hits = hits + 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Help is en-route");
lcd.setCursor(0, 1);
lcd.print("Press to Cancel");

}

}
// save the current switch state as the last state
prevSwitchState = switchState;
}

You have code that displays a message if (switchState == LOW) so I think you just need to add (after that) a similar block of code prefaced by else

And please post code within code tags (the scroll icon with <> on it)

so it looks like this

...R

Hi, I just got my Arduino yesterday and the code is like copied from a counter code. Do you mind to help me make a new code like on the first screen it says "Hello" and when I press the button it says "World". Thanks a lot I will appreciate it a ton! :smiley: :smiley:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// set up a constant for the tilt switchPin
const int switchPin = 2;

static int hits = 0;

// variable to hold the value of the switchPin
 int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

void setup() {  
  // set up the number of columns and rows on the LCD 
  lcd.begin(16, 2);

  // set up the switch pin as an input
  pinMode(switchPin,INPUT);
Serial.begin(9600);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Hit the button");
  lcd.setCursor(0, 1);
  lcd.print("to increment");
}

void loop() {  
  // check the status of the switch
 switchState = digitalRead(switchPin);
   Serial.print("switchState:");Serial.println(switchState);
  if (switchState != prevSwitchState) {
    if (switchState == HIGH) {
          hits = hits + 1;
          delay(10);
    }
  }
  
  
  Serial.print("hits:");Serial.println(hits);
  if(hits==1)
  {
    Serial.println("hello");
    lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Help is en-route");
  }else
  if(hits==2)
  {
   Serial.println("welcome");
    lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("press is cancel"); 
  }else
  
  if ( hits>=3)
  {
     hits = 0;
     Serial.println("couter is reset:");
  }
  Serial.println("...............");  
delay(1000);
}

Check above code

Hi Thanks for your help! Well ignore my old code I need a code where the first screen is "Welcome" and on the second button press it displays "Hello" and on the last button press is "World". Much appreciated :smiley:

HaroldTech:
I need a code where the first screen is "Welcome" and on the second button press it displays "Hello" and on the last button press is "World".

This is not a complex problem - it just needs a little thought by you. I think it would be a better learning exercise for you to try to figure it out than for me to write the code for you.

Start by just sending the words to the Arduino Serial Monitor.

Using plain language (not code) write down the necessary steps - each on a separate line
Imagine you had children with the words written on boards which they should hold up at the correct time
How would you instruct them?

Have a look at the Thread planning and implementing a program which shows how to turn the steps into a program

...R

Hi, you made a really good post. The thing is I need the code urgently. I am currently reading the tutorial BTW. I really hope you can help. Much thanks!

HaroldTech:
Hi Thanks for your help! Well ignore my old code I need a code where the first screen is "Welcome" and on the second button press it displays "Hello" and on the last button press is "World". Much appreciated :smiley:

first let me Know how many buttons you have.

R u using single button to control the display or 2 differnet buttons to control.

elaborate your problem statement properly. else no one help here.

There is only a single button. It will be connected to port 2.

HaroldTech:
The thing is I need the code urgently.

If you want someone else to write code for you then you should ask in the Gigs and Collaborations section and expect to pay for it.

If you want to do it yourself "urgently" then I suggest it is time to start planning your program and writing some code.
I am happy to help. But I am not prepared to do this for you. There is probably only 10 or 15 lines of code needed. What did you do in the hour and 13 minutes between Replies #8 and #10?

...R

include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int switchPin = 8; // momentary switch on 8, other side connected to ground
int Display = 0;

void setup()
{
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // turn on pullup resistor
}

void loop()
{
if (digitalRead(switchPin) == HIGH){
delay(500); // delay to debounce switch

Display ++;
if(Display > 3){
lcd.clear();
Display = 1;
}

switch (Display) {
case 1: {
lcd.setCursor(0, 0);
lcd.print("page 1");

break;
}

case 2: {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Page 2");
break;
}

case 3: {
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("Page 3");
break;
}
}
}
}

So you've dragged up a 3 year old post. Was there a question?

Steve

I want to use multiple buttons instead of a single button how can I alter the above code for it?

Hi,
Is it possible that on the start the screen says "welcome"
push button 1 press displays "message 1 started"
push button 2 press displays "message 2 started" along with previous message.
push button 1press again removes "message 1 started" but "message 2 started" continues

Yes that's certainly possible. Give it a try and see how you get on. If you post your program here we can help you sort out any problems you have.

Steve