Problems with a oled i2c counter with push buttons

I have a arduino uno r3. I am trying to create a display which says shots left, and every time you push a button, the number reduces by one. However, when i tried it, not only will it not respond when i push my button, it also counts down automatically when i did nothing. Another thing is when in my code, the initial number is 125. As it gets down to 124,123,122...and when it reaches a 2 digit number,99, for some reason it will display 990,980,970... Same thing happens when it gets to a 1 digit number,10, it will become 100,900,800... However, when i altered my program to count up from zero, it doesn't have the digit problem.
My code is as follow:

static const unsigned char PROGMEM  LOGO[] =
{ 
/* 0X00,0X01,0X80,0X00,0X1F,0X00, */
0X00,0X38,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0XFE,0X00,0X00,0X00,0X70,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X01,0XFF,0X0F,0XBF,0XF7,0X7F,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X01,0XFF,0X8F,0X1F,0XFF,0XFF,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X03,0XFF,0X8F,0XDF,0XFF,0XFF,0XC0,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X07,0XFF,0XCF,0XDF,0XFF,0XFF,0XE0,0X3F,0X07,0XE6,0X0F,0XC3,0X9F,0XEF,0X81,0XE0,
0X07,0XFF,0XCF,0X9F,0XFF,0XFF,0XE0,0X3F,0X8F,0XEE,0X0F,0XCF,0XDF,0XEF,0XE3,0XF0,
0X07,0XFF,0XCF,0X7F,0XFF,0XFF,0XE0,0X3F,0X0F,0X0E,0X0E,0X0E,0X67,0X8E,0XE7,0X38,
0X0F,0XFE,0X6F,0X70,0X00,0XFF,0XF0,0X7F,0X0F,0X0E,0X0E,0X1C,0X67,0X8E,0XE7,0X38,
0X08,0X30,0X6F,0XE0,0X00,0XFF,0XF0,0X7F,0X0F,0XCE,0X0F,0X9C,0X07,0X8E,0XE7,0X3C,
0X08,0X00,0X6F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XDD,0X8C,0X00,
0X01,0X06,0X8D,0XFF,0XFF,0XF8,0X1F,0XFE,0X00,0X11,0XC7,0XE3,0XF1,0XFC,0XFC,0X00,
0X00,0X1B,0X89,0XFF,0XFF,0XF8,0X0F,0XFC,0X00,0X00,0X01,0X80,0XC0,0X00,0X70,0X00,
0X00,0X09,0X8B,0XFF,0XFC,0XF8,0X0F,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X0D,0XC0,0X00,0X00,0X00,0X0F,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X07,0XC0,0X00,0X00,0X00,0X07,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X07,0X80,0X00,0X00,0X00,0X00,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X01,0X00,0X00,0X00,0X00,0X00,0X08,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>         //download here: https://www.electronoobs.com/eng_arduino_Adafruit_GFX.php
#include <Adafruit_SSD1306.h>     //downlaod here: https://www.electronoobs.com/eng_arduino_Adafruit_SSD1306.php
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);


int counter=125;


int buttonPin=3;

int buttonState=LOW;
void setup() {
pinMode(buttonPin, INPUT);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32 or 64 from eBay)
  delay(100);
  display.clearDisplay();
  display.display();
  delay(100);
  display.setTextSize(1);    
  display.setCursor(0,0);
  display.setTextColor(WHITE);
  display.println(F("SHOTS LEFT")); 
}

void loop() {

buttonState=digitalRead(buttonPin);
if (buttonState=HIGH);
{counter=counter-1;}
 
  display.setTextSize(2);    
  display.setCursor(33,8);
  display.setTextColor(WHITE,BLACK);
  display.println(counter); 
  display.display();//Finally display the created image
 
 
 if (counter<0){counter=124;}

  }

I connected my oled(128x64) by
VCC to 5V
GND to GND
SCL to A5
SDA to A4.
My button i connected as in the photo.

p.jpg

If the number is less than 100, print a space in front.
If it is less than 10, print another space.

It would be much simpler to put the resistor away, and use INPUT_PULLUP as the mode, with one leg of the switch connected to ground and the other leg connected to the digital pin.

Pressed == LOW. Not pressed == HIGH.

if (buttonState=HIGH);

One line of code. Two fundamental mistakes. I'll leave it to you to find both of them.

Please just tell me I’m a noob

ttapeX:
Please just tell me I’m a noob

Don't you already know that?

Look at the documentation for an if statement. Does an if statement end with a semicolon?

Look at the documentation for the equality operator. Does the equality operator look anything like the assignment operator?

AWOL:
If the number is less than 100, print a space in front.
If it is less than 10, print another space.

But I wrote counter=counter-1, how can I add space?

Do your space-printing just before display.println(counter);

But I wrote counter=counter-1,

sp. "counter--;"

AWOL
You mean add 2 spaces before the display.println(counter), or the line above?

ttapeX:
AWOL
You mean add 2 spaces before the display.println(counter), or the line above?

Suppose that you have a 3 character field that you want to fill.

Suppose that the number you want to fill that field with is 17. How many spaces do you need to print? Before or after the number?

Suppose that the value is 49. How many spaces do you need to print? Before or after the number?

Suppose that the value is 123. How many spaces do you need to print? Before or after the number?

Suppose that the value is 3. How many spaces do you need to print? Before or after the number?

You are making this far tougher than it needs to be.

Thing is I use the count- function, so I can’t specifically type how many spaces I need

Thing is I use the count- function, so I can't specifically type how many spaces I need

count--; is NOT a function. It is a statement. After it is executed, you CAN determine how many spaces.

if(count < 100)
   lcd.print(' ');
if(count < 10)
   lcd.print(' ');

If the value is more than 100, don't print any spaces. If the value is less than 100, print one space (so that 10 through 99 print properly). If the value is less than 10, print a second space (so that 0 to 9 print properly).

PaulS:
count--; is NOT a function. It is a statement. After it is executed, you CAN determine how many spaces.

if(count < 100)

lcd.print(' ');
if(count < 10)
  lcd.print(' ');




If the value is more than 100, don't print any spaces. If the value is less than 100, print one space (so that 10 through 99 print properly). If the value is less than 10, print a second space (so that 0 to 9 print properly).

So I press the space bar in between the ‘ ’?

ttapeX:
So I press the space bar in between the ‘ ’?

If you don't want to do that, you can use '\x20' instead.

So I press the space bar in between the ' '?

Why don't you experiment with different characters between the single quotes, and see what appears on the LCD? Seeing IS believing, and you'll remember what you learned far longer than if we just tell you yes.

PaulS:
Why don't you experiment with different characters between the single quotes, and see what appears on the LCD? Seeing IS believing, and you'll remember what you learned far longer than if we just tell you yes.
My code now:
static const unsigned char PROGMEM LOGO[] =
{
/* 0X00,0X01,0X80,0X00,0X1F,0X00, */
0X00,0X38,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0XFE,0X00,0X00,0X00,0X70,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X01,0XFF,0X0F,0XBF,0XF7,0X7F,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X01,0XFF,0X8F,0X1F,0XFF,0XFF,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X03,0XFF,0X8F,0XDF,0XFF,0XFF,0XC0,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X07,0XFF,0XCF,0XDF,0XFF,0XFF,0XE0,0X3F,0X07,0XE6,0X0F,0XC3,0X9F,0XEF,0X81,0XE0,
0X07,0XFF,0XCF,0X9F,0XFF,0XFF,0XE0,0X3F,0X8F,0XEE,0X0F,0XCF,0XDF,0XEF,0XE3,0XF0,
0X07,0XFF,0XCF,0X7F,0XFF,0XFF,0XE0,0X3F,0X0F,0X0E,0X0E,0X0E,0X67,0X8E,0XE7,0X38,
0X0F,0XFE,0X6F,0X70,0X00,0XFF,0XF0,0X7F,0X0F,0X0E,0X0E,0X1C,0X67,0X8E,0XE7,0X38,
0X08,0X30,0X6F,0XE0,0X00,0XFF,0XF0,0X7F,0X0F,0XCE,0X0F,0X9C,0X07,0X8E,0XE7,0X3C,
0X08,0X00,0X6F,0XE0,0X00,0XFF,0XF8,0X7F,0X0F,0XCE,0X0F,0X9C,0X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0XFE,0X00,0X11,0XC7,0XE3,0XF1,0XFC,0XFC,0X00,
0X00,0X1B,0X89,0XFF,0XFF,0XF8,0X0F,0XFC,0X00,0X00,0X01,0X80,0XC0,0X00,0X70,0X00,
0X00,0X09,0X8B,0XFF,0XFC,0XF8,0X0F,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X0D,0XC0,0X00,0X00,0X00,0X0F,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X07,0XC0,0X00,0X00,0X00,0X07,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X07,0X80,0X00,0X00,0X00,0X00,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X01,0X00,0X00,0X00,0X00,0X00,0X08,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h> //download here: Adafruit_GFX library Arduino examples download
#include <Adafruit_SSD1306.h> //downlaod here: Adafruit_SSD1306-master library Arduino examples download
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);

int counter=125;

int buttonPin=7;

void setup() {
pinMode(7,INPUT_PULLUP);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32 or 64 from eBay)
delay(100);
display.clearDisplay();
display.display();
delay(100);
display.setTextSize(1);
display.setCursor(0,0);
display.setTextColor(WHITE);
display.println(F("SHOTS LEFT"));
}

void loop() {

if (digitalRead(buttonPin)==LOW)
{counter--;}

display.setTextSize(2);
display.setCursor(33,8);
display.setTextColor(WHITE,BLACK);
display.println(counter);
display.display();

if( 10< counter <100)
display.print(' ');
if(counter<10)
display.print(' ');//Finally display the created image

if (counter<0){counter=124;}

}
The button works, but the digits still dont work for those <99

  if( 10< counter <100) What's that?

Please remember to use code tags when posting code

AWOL:
  if( 10< counter <100) What's that?

Please remember to use code tags when posting code

well logically if counter<10 is included in counter<100, in the code someone gave me before in this topic, so to be logical i wrote that.

What the compiler sees is "Is counter greater than 10?"
The result is either true (1) or false (0).
Then it sees "Is true/false less than 100?".

Which is always true.

even with your extra :confused: :confused: if program it still doesnt work...

ttapeX:
even with your extra :confused: :confused: if program it still doesnt work...

I have no idea what that means.