Hello folks,
I'm currently trying to make a simple project when you press first button, a number decreases, and using second button to increase a number (later will be developed into something much more complicated...). Anyways my trouble is that when I use my first button (without the second button used in the code) the number shown on my LCD does it's job, a press adds 1 to the number, but when I write the code for the second button, the number decreases automatically every second no matter if I press the second button or not... What am I doing wrong? p.s I'm still a beginner at programming and Arduino
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin, INPUT);
lcd.begin(16,2);
}
static int stock = 20;
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
delay(250);
int plus = ++stock;
lcd.setCursor(0, 0);
lcd.print("Number: ");
lcd.print(plus);
}
buttonState = digitalRead(buttonPin2);
if (buttonState == HIGH) {
delay(250);
int plus = --stock;
lcd.setCursor(0, 0);
lcd.print("Number: ");
lcd.print(plus);
}
}
I hope someone can help me out, thanks in advance.