4 digit seven segment display counter

#include "SevSeg.h"
SevSeg sevseg; 
const int  buttonPin = A0;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonState = 0;
int buttonCounter = 0;

void setup() {
  byte numDigits = 4;
  byte digitPins[] = {10, 11, 12, 13};
  byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};

  bool resistorsOnSegments = true; 
  bool updateWithDelaysIn = true;
  byte hardwareConfig = COMMON_CATHODE; 
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(90);
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the button1State to its previous state
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      buttonCounter++;
      delay(250);
      //Serial.println("on");
      Serial.println(buttonCounter);
      
    }
      sevseg.setNumber(buttonCounter);
      sevseg.refreshDisplay();
  }

I would like to display continuous values as long as I press the button. But now the display is getting incremented by 4, even though the serial monitor shows the variable is incremented by 1.
Can anyone help me correctly display the counter variable?
Thanks in advance

Isn't the scope issue there a little limiting?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.