Button still press for up-counting

Please let me know that about up-counting using Arduino UNO with button connect to pin 2 and 7-segment display.

When I press button it counts only one time.
I want to count-up and still counter going up until I release the button.

But I tried but not working

Can anyone one interested to tell me?

 reading2 = digitalRead(CTAmp_button);
    
    if (reading2 != lastButtonState2)
    {
    lastDebounceTime2 = millis();
    }
    if ((millis() - lastDebounceTime2) > debounceDelay2)
    {
      if (reading2 != buttonState2)
      {
      buttonState2 = reading2;
       
       if (buttonState2 == HIGH)
       {        
        Counter ++;          
       }      
      }      
    }
     lastButtonState2 = reading2; 
  
     Num   = Counter;
     a     = Num / 1000;     //If Num=1234 Then a=1;
     temp1 = Num % 1000;     //If Num=1234 Then temp1=234;
     b     = temp1 / 100;    //If temp1=234 Then b=2;
     temp2 = temp1 % 100;    //If temp1=234 Then temp2=34;
     c     = temp2 / 10;     //If temp2=34 Then c=3;
     d     = temp2 % 10;     //If temp2=34 Then d=4; 

     display (a);
     display (b);
     display (c);
     display (d);

Hi,
Please post your whole code.

Thanks.. Tom.. :slight_smile:

I need only continous up count during button press.

Hi,
Because you are debouncing, you are only counting on the change of your button, not the state of the button.

Your code can be simplified to accomplish what you need, below.
I have just commented out your bit of code and added the edit.

The variable countrate sets how fast the count occurs, the larger the number the slower the rate.

reading2 = digitalRead(CTAmp_button);
/*
    if (reading2 != lastButtonState2)
    {
    lastDebounceTime2 = millis();
    }
    if ((millis() - lastDebounceTime2) > debounceDelay2)
    {
      if (reading2 != buttonState2)
      {
      buttonState2 = reading2;

       if (buttonState2 == HIGH)
       {
        Counter ++;
       }
      }
    }
     lastButtonState2 = reading2;
*/
//  START ADDED CODE
if (reading2 == HIGH)
{
  count++
  delay(countrate);
}
// END ADDED CODE
Num   = Counter;
a     = Num / 1000;     //If Num=1234 Then a=1;
temp1 = Num % 1000;     //If Num=1234 Then temp1=234;
b     = temp1 / 100;    //If temp1=234 Then b=2;
temp2 = temp1 % 100;    //If temp1=234 Then temp2=34;
c     = temp2 / 10;     //If temp2=34 Then c=3;
d     = temp2 % 10;     //If temp2=34 Then d=4;

display (a);
display (b);
display (c);
display (d);

You need to add before void setup()
int countrate = 100;

Hope it does what you need.

Tom... :slight_smile:

"still counter going up until I release "

while( buttonIsPushed )
{
counter = counter + 1;
}

Thank you TomGeorge

I will try and tell you.