Arduino counter with OLED

Hello. Im fairly new to arduino, but finished my project not too long ago. Right now im trying to make a counter that goes down by 1 number each time a button is pressed. Ive got the code right without any errors, yet it doesnt seem to work. I press the button but nothing happens. I tried playing around with different pins, LOWS and HIGHS, etc.

here’s my code:

#include <OLED_I2C.h>
#include <Button.h>

OLED myOLED(SDA, SCL, 8 );

int increasebutton = 4;

int buttonstate;
int lastbuttonstate = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;

extern uint8_t SmallFont;
extern uint8_t BigNumbers;

int value = 25;

void setup(){
Serial.begin(9600);
myOLED.begin();
myOLED.setFont(BigNumbers);
pinMode(increasebutton,INPUT_PULLUP);
}

void loop()
{
increaseParameter();
myOLED.printNumI(value,CENTER,24);
myOLED.update();
myOLED.clrScr();
}
void increaseParameter()
{
int reading = digitalRead(increasebutton);

if (reading != lastbuttonstate) {
lastDebounceTime = millis();
}

if((millis() - lastDebounceTime) > debounceDelay) {

if(reading != buttonstate){
buttonstate = reading;

if(buttonstate == HIGH) {
value–;
}
}
}
}

whenever there is a change in button state you reset lastDebounceTime.

you should reset lastDebounceTime only inside the condition when > denounceDelay AND there is a button state change

gcjr:
whenever there is a change in button state you reset lastDebounceTime.

you should reset lastDebounceTime only inside the condition when > denounceDelay AND there is a button state change

so what should i change up? could you fix the code? sorry, im a visual learner, and due to me being new to arduino, its hard for me to understand what exactly i have to do.

I tried to fix it. What else is lacking??

#include <OLED_I2C.h>
#include <Button.h>

OLED myOLED(SDA, SCL, 8 );

const int decreasebutton = 4;

int buttonstate;
int lastbuttonstate = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;

extern uint8_t SmallFont;
extern uint8_t BigNumbers;

int value = 25;

void setup(){
Serial.begin(9600);
myOLED.begin();
myOLED.setFont(BigNumbers);
pinMode(decreasebutton,INPUT_PULLUP);
}

void loop()
{
decreaseParameter();
myOLED.printNumI(value,CENTER,24);
myOLED.update();
myOLED.clrScr();
}
void decreaseParameter()
{
int reading = digitalRead(decreasebutton);

if (reading != lastbuttonstate) {
lastDebounceTime = millis();
}

if((millis() - lastDebounceTime) > debounceDelay && buttonstate == HIGH) {

if(reading != buttonstate){
buttonstate = reading;

if(buttonstate == HIGH) {
value–;
}
}
}
lastbuttonstate = reading;
}

dainyzas:
sorry, im a visual learner, and due to me being new to arduino, its hard for me to understand what exactly i have to do.

very good. you tried

void increaseParameter()
{
    int reading = digitalRead(increasebutton);

    if((millis() - lastDebounceTime) > debounceDelay) {
        if(reading != buttonstate){
            buttonstate = reading;
            if(buttonstate == HIGH) {
                value--;
            }

            lastDebounceTime = millis();
        }
    }
}

gcjr:
very good. you tried

void increaseParameter()

{
   int reading = digitalRead(increasebutton);

if((millis() - lastDebounceTime) > debounceDelay) {
       if(reading != buttonstate){
           buttonstate = reading;
           if(buttonstate == HIGH) {
               value–;
           }

lastDebounceTime = millis();
       }
   }
}

Thank you, sadly the number still doesnt go down. could you please check it over again? ^^’
#include <OLED_I2C.h>
#include <Button.h>
OLED myOLED(SDA, SCL, 8 );
const int decreasebutton = 4;
int buttonstate;
int lastbuttonstate = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;
extern uint8_t SmallFont;
extern uint8_t BigNumbers;
int value = 25;
void setup(){
Serial.begin(9600);
myOLED.begin();
myOLED.setFont(BigNumbers);
pinMode(decreasebutton,INPUT_PULLUP);
}
void loop()
{
increaseParameter();
myOLED.printNumI(value,CENTER,24);
myOLED.update();
myOLED.clrScr();
}
void increaseParameter()
{
int reading = digitalRead(decreasebutton);
if((millis() - lastDebounceTime) > debounceDelay) {
if(reading != buttonstate){
buttonstate = reading;
if(buttonstate == HIGH) {
value–;
}
lastDebounceTime = millis();
}
}
}

I press the button but nothing happens.

this is the problem i fixed

Thank you! It works now!