Addition of numbers using button won't work

Hello everyone,

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

Do you have a pull-down resistor on pin 2?

Paul

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.

Paul_KD7HB:
Do you have a pull-down resistor on pin 2?

Paul

Yes I do, a 10k ohm one.

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".

Always re-post the complete code after making changes.

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

You need a type for the variable.

As static:

static int number = 1;

As global (above setup() function):

int number = 1;

gfvalvo:
You need a type for the variable.

As static:

static int number = 1;

As global (above setup() function):

int number = 1;

Both work without an error, but still it's the same as before, the number doesn't change no matter how often I press the button. Any more advice?

  pinMode(buttonPin, INPUT);

So, tell us HOW the switch AND the external resistor (you MUST have one) are wired.

Or, use INPUT_PULLUP as the mode, and you won't need an external resistor.

   int plus = stock +number;

stock never changes. number never changes. You can push the switch until your finger falls off, and plus will ALWAYS be assigned the same value.

PaulS:

  pinMode(buttonPin, INPUT);

So, tell us HOW the switch AND the external resistor (you MUST have one) are wired.

Or, use INPUT_PULLUP as the mode, and you won't need an external resistor.

   int plus = stock +number;

stock never changes. number never changes. You can push the switch until your finger falls off, and plus will ALWAYS be assigned the same value.

This is how it's connected. Note, the button works, when you press it the LCD starts showing the number, but it doesn't increase by pressing it again.

but it doesn't increase by pressing it again.

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.

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.

Is there anything else I can do?

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.

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.

It IS possible. Stupidly simple, even.

What, exactly, are you counting? Where do you actually try to increase a number by 1?

Try this:

int pressCount = 0;
int stockCount = 20;

byte prevState = LOW;
byte currState;

byte switchPin = 2;

void setup()
{
   Serial.begin(115200);
   pinMode(switchPin, INPUT);
}

void loop()
{
   currState = digitalRead(switchPin);
   if(currState != prevState)
   {
      if(currState == HIGH)
      {
         pressCount++; // Increment the count of presses

         int plus = pressCount + stockCount;
         Serial.print("plus = ");
         Serial.println(plus);
      }
   }
   prevState = currState;
}

Upload that code, and open the serial monitor. Set the baud rate to 115200.

Press the switch a few times.

Then, examine the code to see the one line I snuck in there that your code doesn't have.

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.