Arduino lcd counter

So I need to setup a counter on an lcd that starts at 0 and increases or decreases by 1 depending on which button is pressed. i have no idea what I'm doing and I'm not sure if my code actually works or not.

code:

===========================================================================

//header
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6,5,4,3,2); //tells the arduino the location
const int buttonPin1 = 9; // of the LCD
const int buttonPin2 = 8;
int button1State = 0; //state of button 1
int button2State = 0; //state of button 2
int count_value = 0; //lcd current number
int prevstate = 0; //lcd previous state

//setup
void setup(){
pinMode(buttonPin1, INPUT); //Both buttons take input
pinMode(buttonPin2, INPUT);
lcd.setCursor(0,0);
lcd.print("Counter:");
lcd.setCursor(9,1);
lcd.print(count_value); //count value is currently at 0
} //end of setup

//loop
void loop(){
button1State = digitalRead(buttonPin1); //reads the state of
button2State = digitalRead(buttonPin2); //both buttons
if(button1State == HIGH && button2State == LOW){
count_value = (prevstate + 1); //if the first button is on
lcd.setCursor(0,0); //and the second button is off
lcd.clear(); //the lcd will print the current
lcd.print ("Counter:"); //value + 1 and mark the count
lcd.setCursor(0,9); //value as the previous value
lcd.print(count_value);
prevstate = (count_value);

} //end of if statement
else if(button2State == HIGH && button1State == LOW){
count_value = (prevstate - 1);
lcd.setCursor(10,1); //same as the first if statement
lcd.print(count_value); //but the otherway around
prevstate = (count_value);
} //end of else if statement
} //end of loop

===========================================================================
Help would be appreciated.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.


Why don’t you check to see if it works ? :thinking:


Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.