Modify a four-digit counter which counts up become to counter increment

Hello,I got a code which function is "It shows the current value of a four-digit counter which counts up // every 1 second."
and now,i'd like to modify it to become a press button then counter increment.but
,I didn't make it.the modified code below.i tried to keep the 4digit 7segment display always on.but now it shows value then turns off,repeatly.please help.thanks for everybody...

I have already fingered it out.the problem is
"delay(n);" ,but i need it,and how can i modify the code...

the modified code

#include <Four7Seg74hc595.h>

/*
* PRESS BUTTON COUNTER INCREMENT
*/
esl::Four7Seg74hc595 display( 10,9,8 );
// Pin connected to Pin 14 of 74HC595 (Data=DIO,arduino pin8)
// Pin connected to Pin 12 of 74HC595 (Latch=RCLK,arduino pin9)
// Pin connected to Pin 11 of 74HC595 (Clock=SCLK,arduino pin10)

char sbuf[5];
uint16_t Counter;
uint32_t ts1, ts2;

const int buttonPin = 2;                 // 按鈕(pushbutton)
const int relayPin = 13;                 // 繼電器(Relay)
unsigned long detectingtime = 15000;     // 偵測電位的時
int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button
  
void setup() {
  Serial.begin(9600);                     // 開啟 Serial port, 通訊速率為 9600 bps
  pinMode(buttonPin, INPUT);             // 把 buttonPin 設置成 INPUT
  pinMode(relayPin, OUTPUT);             // 把 relayPin 設置成 OUTPUT 

  for (uint8_t i=0; i < 100; i++) {
    display.setDigits( "----", 4 );
    display.update();
    delay(10);
  }
  delay(1000);
  buttonPushCounter = 0;
  sprintf( sbuf, "%04u", buttonPushCounter );
  display.setDigits( sbuf, 4 );
  display.update();
  ts1 = ts2 = millis();
}

uint32_t ts;

void loop() {
  ts = millis( ); 
  if ( ts - ts2 >= 5 ) {
     display.setDigits( sbuf, 4 );
     display.update();
     ts2 += 5; // display-update interval = 5msec
     
    Serial.print("ts: ");          
    Serial.print(ts);
    Serial.println(" microseconds");  
    Serial.print("ts2: ");          
    Serial.print(ts2);
    Serial.println(" microseconds");  


}


  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {    // 檢查按鈕是否被按下(pressed)
  // 有的話,buttonState 會是 HIGH

  if (buttonState == HIGH) {    
        // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
      Serial.print("number of button pushesON:  ");
      Serial.println(buttonPushCounter,DEC);
      sprintf( sbuf, "%04u", buttonPushCounter );
      delay(15000);
      digitalWrite(relayPin, HIGH);
      delay(1000); 
      digitalWrite(relayPin, LOW); 
       }
      
  else if (buttonState == LOW) {    
       // if the current state is LOW then the button
      // went from on to off:
      buttonPushCounter++;
      Serial.print("number of button pushesOFF:  ");
      Serial.println(buttonPushCounter,DEC);
      sprintf( sbuf, "%04u", buttonPushCounter);
      delay(3000); 
      digitalWrite(relayPin, HIGH);
      delay(1000); 
      digitalWrite(relayPin, LOW);  
      }     
   }  
lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop
}

the original code below.

//////////////////////////////////////////////////////////////////////////
// Author: RSP @ Embedded System Lab (ESL), KMUTNB, Thailand
// File: Four7Seg74hc595.cpp
// Last Modified: 2014-06-23
// Description: This Arduino sketch shows how to use the
//   Four7Seg74hc595 class to interface with the 4-digit 7-segment
//   display module with dual 74hc595 shift regsiters.
//   It shows the current value of a four-digit counter which counts up
//   every 1 second.
//////////////////////////////////////////////////////////////////////////

#include "Four7Seg74hc595.h"

esl::Four7Seg74hc595 display( 10,9,8 );

char sbuf[5];
uint16_t count;
uint32_t ts1, ts2;

void setup() {
  for (uint8_t i=0; i < 100; i++) {
    display.setDigits( "----", 4 );
    display.update();
    delay(10);
  }
  delay(1000);
  count = 0;
  sprintf( sbuf, "%04u", count );
  display.setDigits( sbuf, 4 );
  display.update();
  ts1 = ts2 = millis();
}

uint32_t ts;

void loop() {
  ts = millis();
  if ( ts - ts1 >= 1000) {  
     count = 1;
     sprintf( sbuf, "%04u", count );
     ts1 += 1000; // increment counter by 1 every 1sec
  }
  if ( ts - ts2 >= 5 ) {
     display.setDigits( sbuf, 4 );
     display.update();
     ts2 += 5; // display-update interval = 5msec
  }
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Author: RSP @ Embedded System Lab (ESL), KMUTNB, Thailand
// File: Four7Seg74hc595.cpp
// Last Modified: 2014-06-23
//////////////////////////////////////////////////////////////////////////
#include "Four7Seg74hc595.h"

using namespace esl;

Four7Seg74hc595::Four7Seg74hc595( int sclk, int rclk, int dio, boolean invert )
   : sclk_pin(sclk), rclk_pin(rclk), dio_pin(dio)
{
   pinMode( sclk_pin, OUTPUT );
   pinMode( rclk_pin, OUTPUT );
   pinMode( dio_pin, OUTPUT );
   digitalWrite( sclk_pin, LOW );
   digitalWrite( rclk_pin, LOW );
   invert_mask = (invert) ? 0xff : 0x00;
   delay_usec = 500;
}

void Four7Seg74hc595::setDigits( const char *str, uint8_t len ) {
  char ch;
  uint8_t digit;
  if ( len == 0 ) {
    len = strlen(str);
  }
  if ( len > num_digits ) {
    len = num_digits;
  }
  for ( uint8_t i=0; i < len; i++ ) {
     ch = tolower(str[i]);
     if ('0' <= ch && ch <= '9') {
       digit = (ch - '0');
       raw_digits[i] = DIGITS_7SEG[digit];
     }
     else if ('a' <= ch && ch <= 'f' ) {
       digit = (ch - 'a') + 10;
       raw_digits[i] = DIGITS_7SEG[digit];
     }
     else {
       switch (str[i]) {
         case 'r': raw_digits[i] = 0b01010000; break;
         case 'h': raw_digits[i] = 0b01110100; break;
         case 'H': raw_digits[i] = 0b01110110; break;
         case 'L': raw_digits[i] = 0b00111000; break;
         case '-': raw_digits[i] = 0b01000000; break;
         default: raw_digits[i] = 0; break; // ignored (off)
       }
     }
  }
}

void Four7Seg74hc595::update() {
  uint8_t value, max_index = num_digits-1;
  for ( uint8_t i=0; i < num_digits; i++ ) {
     value = raw_digits[ max_index-i ] ^ invert_mask;
     shiftOut( dio_pin, sclk_pin, MSBFIRST, value ); // seven segments + dot (common-anode type)
     shiftOut( dio_pin, sclk_pin, MSBFIRST, (1 << i) ); // select digit
     digitalWrite( rclk_pin, HIGH );
     digitalWrite( rclk_pin, LOW );
     delayMicroseconds( delay_usec );
  }
  shiftOut( dio_pin, sclk_pin, MSBFIRST, 0xff ); // seven segments + dot (common-anode type)
  shiftOut( dio_pin, sclk_pin, MSBFIRST, 0 ); // select digit
  digitalWrite( rclk_pin, HIGH );
  digitalWrite( rclk_pin, LOW );
}

const uint8_t Four7Seg74hc595::DIGITS_7SEG[] = {
 //  gfedcba
  0b00111111, // 0
  0b00000110, // 1
  0b01011011, // 2
  0b01001111, // 3
  0b01100110, // 4
  0b01101101, // 5
  0b01111101, // 6
  0b00000111, // 7
  0b01111111, // 8
  0b01101111, // 9
  0b01110111, // A
  0b01111100, // b
  0b00111001, // C
  0b01011110, // d
  0b01111001, // E
  0b11110001  // F
};

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Author: RSP @ Embedded System Lab (ESL), KMUTNB, Thailand
// File: Four7Seg74hc595.h
// Last Modified: 2014-06-23
// Note:
//   A C++ class for Arduino that can be used to drive
//   a four-digit 74HC595-based 7-Segment display module.
//////////////////////////////////////////////////////////////////////////

#ifndef __FOUR7SEG_74HC595_H
#define __FOUR7SEG_74HC595_H

#include <Arduino.h>

namespace esl {
  class Four7Seg74hc595 {
     public:
       Four7Seg74hc595( int sclk, int rclk, int dio, boolean invert=true );
       void setDigits( const char *str, uint8_t len=4 );
       void update();

     private:
       int sclk_pin, rclk_pin, dio_pin;
       uint8_t invert_mask;
       uint8_t raw_digits[ 4 ];
       static const uint8_t DIGITS_7SEG[];
       static const uint8_t num_digits=4;
       uint16_t delay_usec;
  }; // end class
} // end namespace

#endif // __FOUR7SEG_74HC595_H

//////////////////////////////////////////////////////////////////////////
count = 1;
     sprintf( sbuf, "%04u", count );

This bit of the supposed original code looks strange. You say it counts up each second ? It would appear that is resets the value to 1, each second.

You also have mismatched { } in your modified code. It probably won't even compile.

void loop()

  if ( ts - ts2 >= 5 ) {
     display.setDigits( sbuf, 4 );
     display.update();
     ts2 += 5; // display-update interval = 5msec
  }

For this to work, you would need the line " ts = millis( ); " to get the current millis, at the start of each iteration of loop( )

The sequential order of your logic makes little sense.

I would suggest that you check the state of the button, and if it has changed, increment the counter, then recalculate sbuf, then display.