Simple LCD Program

I have an LCD screen and a digital push button. I want to print out a number to the LCD screen and with one bush button increment the variable printed on the LCD.

I am relatively new to arduino coding.

Any advice or help?

Hi and welcome.

Please read this (click !) if you haven’t done that yet.

Next, do you have an idea about how many “LCD screens” there are ?
To get help, try to give as much information as possible.
Like brand and type of your LCD screen, or a link to the product.

Also, have a look at the examples.
You can find them in the IDE File menu.
Do some of the examples.
You’ll learn how to use a pushbutton.

Hi,

In your IDE, there are examples “lcd hello world” “debounce”

open the lcd hello world example, and solder a header to your lcd display

i am using a 16x2 lcd display

#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 7, 6, 5, 3); // open your hello world example you will see all connections
                                      // I would suggest you solder a header to your lcd display

const byte buttonPin = 2;    // const = constant     it means your buttonpin is FIXED 
                             // for pins you just use byte  no need to use int 
                             // it is a waste of memory if you use int(integer) for your pins
int counter = 0;             // set your counter to zero to begin with


byte buttonState;             // the current reading from the input pin
byte lastButtonState = HIGH;   // the previous reading from the input pin

// the following variables are unsigned long ( no negative numbers. long is to hold a very big number) because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  Serial.begin(9600);  // for debugging only   you can view everything on your serial monitor on your IDE
  pinMode(buttonPin,INPUT_PULLUP); // use the internal pullup resistor of your chip  now the buttonpin, 
                                   // digital pin 2 is HIGH
 lcd.begin(16, 2);           //  for 2x16 lcd display

}

void loop() {
  // read the state of the switch into a local variable:
 byte reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from HIGH to LOW),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) { 
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) >= debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

   
      if (buttonState == LOW) {   // from HIGH(INPUT_PULLUP) to low(when you press the button)
       counter ++;         
       Serial.println(counter);  // you can view the increased number on your serial monitor and the //second line of your lcd display if you are using a 16x2 lcd display
                             
         lcd.setCursor(0, 1);
         lcd.print(counter);
      }
    }
  }


 //save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}


/*hook up your button to digital pin 2 
   digital pin 2 -----one leg of your button ------ the other leg of your button----arduino ground
   double check all connections of your LCD 
   don't forget to  turn the  potentiometer to see if your LCD display is working */

How would I duplicate this program for four LCD screens?

Do you want to control all 4 LCDs parallely like all together having same outputs or do you want to control them individually using 4 different buttons for 4 ifferent LCDs ?

4 Different LCDs controlled independently I have tried to no avail to get it to work.

If you are using an Arduino Uno just forget it. You will need 6 I/O pins on the Arduino for Each LCD and totally you will need 24. Plus you are adding 4 buttons which will addup to 28.But you have only 14 I/O pins on the Uno. If you have the Mega 2560/ADK/Due which has more I/O pins then we can see about it.

Use an I2C backpack to convert your displays to serial? But make sure you use ones where you can change the address otherwise they will all display the same data.

I am using an Arduino Mega 2560. The LCD screens share the data lines and the RS. The only unique pin is the enable. You can still individually address each LCD.

https://www.hackmeister.dk/2010/08/4-lcd-displays-on-1-arduino/

Reference above shows how to connect 4 to one arduino.

RoboRaider881: The LCD screens share the data lines and the RS. The only unique pin is the enable. You can still individually address each LCD.

That is correct. If it does not work, there is clearly some slight glitch in the code. You are clearly on the right track. Apologies that I cannot look into it just now. :grin:

SagarDev: If you are using an Arduino UNO just forget it. You will need 6 I/O pins on the Arduino for Each LCD and totally you will need 24.

Wrong. Even if the library did not facilitate this, it would be entirely possible to write code for sharing of all pins bar the enable.

The problem I'm having is duplicating the code for 4 screens and 4 switches