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.
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.
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
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 ?