Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

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

You might also like