Display print change upon button trigger

Hi I want to write a Arduino code for showing my 3 friends name one by one upon single button trigger

Example

When I powered the Arduino display will show “Hello All”

When I press button it will show my 1st friend name “Jon”

again when I press same button 2nd friend name will show “Jimmy”

again pressing same button 3rd friend name will display “Sid”

Please help me

Have a look at the state change detection example. Use the buttonPushCounter variable and a series of if, if else statements or a switch case structure to choose which name to display.

can anyone please write code for me please ...

I can't. I don't know what display method that is required. I don't know how the button is wired. Besides, this is not a code writing service. We are happy to help you get your code to work, but you have to at least try.

ok sir i trying and will get back to you soon with my code :slight_smile:

hi i tried to write code for 1st friend but not getting idea for other friends name

#include <LiquidCrystal.h>

const int buttonPin = 2;
int buttonState = 0;

void setup()
{
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Hello All");

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

if (buttonState == HIGH)

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Jon");

}

void loop()
{

}

What happens when you upload and run that code? Does it do what you expect? What happens when you push the button? What is displayed?

yes but button is not working :frowning:

All of your code is in setup(). The setup() function runs once and then program control goes to loop(). So if you are not holding the button down during start up it will not be read as HIGH. And "Jon" is printed whether or not the button is pushed. There are other things wrong with the code such as the lcd constructor is called in setup() so the lcd will not exist anywhere else (see scope). Make the lcd constructor global. And you only need one lcd.begin statement. Also pin 2 is used for both the button and the LCD. That won't work.

Have you read the pages that I linked in reply#1? Will you answer all of the questions that I asked? Does the button switch have a pull down resistor?

Please read the "how to use the forum-please read" stickies to see how to format and post code.

I believe that you would benefit by reading some basic Arduino coding tutorials.
Basic Arduino programming.
Basic C++ tutorial

Thanks sir for reply :slight_smile:

I used loop at end bcoz i want to run code once only not in loop. I updated my circuit as per img ref below

Updated code

#include <LiquidCrystal.h>

const int buttonPin = 7;
int buttonState = 0;

void setup()
{
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Hello All");

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

if (buttonState == HIGH)

// Print a message to the LCD.
lcd.print("Jon");

}

void loop()
{

}

but still its not working only showing "Hello All "

If the code is in setup() you will have to hold the button pressed and reset the Arduino to ever print "Jon" and what will be printed is "Hello AllJon". The lcd constuctor

void setup()
{
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

is still in setup(). It should be outside of setup().

Here is code to print "Hello All" at start up and cycle through "Jon", "Jimmy" and "Sid" at each button press. Since there is no switch debounce it may act a bit funny, but should show a way to do what you want. The links that I provided and the comments in the code will explain much of the concepts in the code.

#include <LiquidCrystal.h>

const byte buttonPin = 7;

int buttonPushCounter = 0;   // counter for the number of button presses
boolean buttonState = LOW;         // current state of the button
boolean lastButtonState = LOW;     // previous state of the button

// lcd constructor made global in scope so the whole program can sse it
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Hello All");
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  
}


void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;  // add one to counter
      lcd.clear();  
      if (buttonPushCounter > 3) // if couter over 3 reset the counter to 1 to show "Jon"
                                 // and not "Hello All"
      {
        buttonPushCounter = 1;
      }
      Serial.println(buttonPushCounter);
      switch (buttonPushCounter) // choose what to display based on buttonPushCounter value
      {
        case 0:
          lcd.print("Hello All"); // show "Hello All until first button press
          break;
        case 1:
          lcd.print("Jon");
          break;
        case 2:
          lcd.print("Jimmy");
          break;
        case 3:
          lcd.print("Sid");
          break;
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }
}

thanks a lot sir, just want to confirm code provided by you will work with same circuit diagram shared by me? i have to use resistance of how much ohm 's ?

10K ohms. And I tested the code on an Uno with LCD wired per your sketch and switch wired per the provided Fritzing image.

Thank you so much sir, Circuit is working as I required.

Can you post a diagram of how you connected the display to the breadboard?

The pin connections are defined in the LCD constructor.

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Arduino pin = LCD pin:
pin 12 = RS, 11 = EN, 5 = D4, 4 = D5, 3 = D6, 2 = D7, RW to ground and Vo (contrast) to ground through 1K resistor.