I'm making a project where when you press a button, the number on the LCD increases by 1. My issue is when the Arduino starts, the LCD is empty, as soon I press the button the LCD shows the wanted number already summed up with 1, but, pressing the button again, nothing happens at all.
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
#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);
lcd.begin(16,2);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
int stock = 20;
int number = 1;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
int plus = stock + number;
lcd.setCursor(0, 0);
lcd.print("Number: ");
lcd.print(plus);
}
}
Unless you want the number to increase rapidly when you get the program working you will need to detect when the button becomes pressed rather than when it is pressed. See the StateChangeDetection eaxmple in the IDE.
As to the problem, this int number = 1;sets number back to 1 each time through loop()
Either make it a global variable and remove int or add the word static to the front of its declaration.
UKHeliBob:
Unless you want the number to increase rapidly when you get the program working you will need to detect when the button becomes pressed rather than when it is pressed. See the StateChangeDetection eaxmple in the IDE.
As to the problem, this
int number = 1;
sets number back to 1 each time through loop()
Either make it a global variable and remove int or add the word static to the front of its declaration.
I made it a global variable and removed int and it says " number was not declared in this scope ", and also adding static and leaving it under loop, gives the same error and also with "does not name a type".
gfvalvo:
Always re-post the complete code after making changes.
Sorry
Here the one with "static":
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
#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);
lcd.begin(16,2);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
int stock = 20;
static number = 1;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
int plus = stock +number;
lcd.setCursor(0, 0);
lcd.print("Number: ");
lcd.print(plus);
}
}
And the one with global
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
#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
number = 1;
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
lcd.begin(16,2);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
int stock = 20;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
int plus = stock +number;
lcd.setCursor(0, 0);
lcd.print("Number: ");
lcd.print(plus);
}
}
PaulS:
Do you REALLY expect to get different answers when you add 20 and 1 after pressing the switch for the 2nd time? The third time? The 27,000,000th time?
If you do, well, never mind.
Yes, I actually do, my point is increasing the number by 1 and as it seems I can't find a way. Something simple like this has to be possible.
Well, I found a way, instead of adding 1 to the variable, I used "++", but it turned out it adds 1 whenver the button is held, which means it adds like 100 in half a second, so I used "delay(250)", and it works perfectly. Thanks again to anyone who tried to help me out.
XGamer223:
Well, I found a way, instead of adding 1 to the variable, I used "++", but it turned out it adds 1 whenver the button is held, which means it adds like 100 in half a second, so I used "delay(250)", and it works perfectly. Thanks again to anyone who tried to help me out.
See my last post for a solution that does not require delay().
Well, I found a way, instead of adding 1 to the variable, I used "++", but it turned out it adds 1 whenver the button is held, which means it adds like 100 in half a second,
No surprise there.
As I said in reply #2
Unless you want the number to increase rapidly when you get the program working you will need to detect when the button becomes pressed rather than when it is pressed. See the StateChangeDetection eaxmple in the IDE.