Help with button delay on Uno 7-seg counter

Im new to the Arduino environment and wrote up a sketch that count 0-9 with the pres of a button and display the number on a 7 segment display however I need help dealing with the signal noise and press delay my code is posted below if anyone has any recommendation on a fix it would be greatly appreciated.

I would like to know if I am implementing Debouncing in the correct way because when i run the sketch there is alot of delay between when i press the button and when the 7-segment display changes
thank you

// Counts from 0-9 on 7 segment Display
// Uses Pins 2 -8

byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 },  // = 0
                                                           { 0,1,1,0,0,0,0 },  // = 1
                                                           { 1,1,0,1,1,0,1 },  // = 2
                                                           { 1,1,1,1,0,0,1 },  // = 3
                                                           { 0,1,1,0,0,1,1 },  // = 4
                                                           { 1,0,1,1,0,1,1 },  // = 5
                                                           { 1,0,1,1,1,1,1 },  // = 6
                                                           { 1,1,1,0,0,0,0 },  // = 7
                                                           { 1,1,1,1,1,1,1 },  // = 8
                                                           { 1,1,1,0,0,1,1 }   // = 9
                                                           };
                                                           
                                                           
int displayNum  = 0;
int buttonPin   = 12;
int buttonState; // current reading of button 
int lastButtonState = LOW; // previous reading of input

// Long variables which are used to store debounce time
long lastDebounceTime = 0; // last time output pin was toggled
long debounceDelay =70; // the debounce time

// turns the dot off
void writeDot(byte dot){
    
  digitalWrite(9,0);
}


void sevenSegWrite(byte digit){

  byte pin = 2;
    for(byte segCount = 0; segCount < 7; ++segCount){
        digitalWrite(pin,seven_seg_digits[digit][segCount]);
        ++pin;
    }   
}

                                                          
void setup() {
  
 Serial.begin(9600);
 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
 pinMode(5,OUTPUT);
 pinMode(6,OUTPUT);
 pinMode(7,OUTPUT);
 pinMode(8,OUTPUT);

 
 
 }


void loop(){
 
int reading = digitalRead(buttonPin); //reads the button 
 Serial.println(reading); // prints its state to serial monitor
 delay(1000);
 
// if the switch has changes due to noise
 if(reading != lastButtonState){
   lastDebounceTime = millis(); //resets debouncing timer
 } 
 
 if(( millis() - lastDebounceTime) > debounceDelay){
   // if the reading has been their for longer than the debounce time this is the state
  
    if(reading != buttonState){
       buttonState = reading;
        
          if (buttonState == HIGH){
    
              sevenSegWrite(displayNum);
              displayNum++;
 
          }

    }
            
 }

lastButtonState = reading;

}

Please redo (Modify button) this post and use the CODE ["#"] (ABOVE) BUTTON so people can scroll
through the code.
Please restate or clarify your question .
Are you asking for help to add switch de-bouncing to your program ?
WHY do you need help ?
Please STATE THE PROBLEM . (IE: my program doesn't work or something other than "I need help..."
like for example, you could describe what is happening that prompted you to ask for help...

Here's the link to the tutorial on that:

You can also use a 1k pullup resistor with a 0.1uf capacitor connected to the input line. (resistor from switch to +5V, cap from
switch to ground).

Sorry about the code I modified my post and was wondering if there is anyway to lower the delay time when debouncing while keeping the inputs accurate.

 int reading = digitalRead(buttonPin); //reads the button 
 Serial.println(reading); // prints its state to serial monitor
delay(1000); <==MOVE THIS TO END OF LOOP

I would like to know if I am implementing Debouncing in the correct way...

That's essentially the equivalent of asking if you believe in the correct religion or if you belong to the correct political party.

Don

:grin:
That's funny Don

ma3505,
Did you move your delay to the end of the loop yet ?
How EXACTLY is you switch wired ?
Do you have pullup or pulldown resistors ?
How is each side of the switch wired ?

OK, a few basics about debouncing:

The bounce period is generally going to be a few milliseconds, unlikely to be more than ten.

Putting "delay()" anywhere in code is just going to prevent anything happening for that period, so you really do not want to use "delay()" for any serious function - except to really delay things, which is unlikely. The use of "delay()" is referred to as "busy waiting", which means that the delay itself is being busy, but absolutely nothing else gets done. I've seen a few comparisons as to what the equivalent is in "real life" - such as sitting watching the clock to see when the cake is done.

The proper way to debounce, is to determine if the button has remained in a new state contrary to the previous accepted state for each of a number of time intervals. Typical would be ten intervals of 1 millisecond. This is done not by "busy waiting" or indeed waiting on the "clock" only when a change is detected, but polling at the rate of the time intervals, so that your "outer loop" (within the main loop) looks not at the button, but at the millis() timer and polls the button each new millisecond.

On each of these poll events, the current state of the button is read and compared to its "previous" state. If they match, the debounce count is set to the criterion (such as 10) and nothing further is done. If they do not match, the debounce counter is decremented. Until it reaches zero, nothing further is done. When it does reach zero, this indicates that number of successive polls, each successfully demonstrating the new state, so the "previous" state is set to the new state, the debounce count is again set to the criterion (ready for further polling) and the action according to the particular state change is taken.

Which action needs to be taken can be determined by noting the difference between the current and old "previous" states and this is important because this method can be used just as effectively on a byte value containing eight separate states (or in fact a longer word) as a single Boolean value, so that more than one state may be new (and of course, stable) and some may have changed in one direction and some in another.

So as well as the milliseconds counter, you need a "previous" state variable, a "current" state variable and a debounce counter.

This whole code is an element of a single pass through the main "loop" which may run many times per millisecond - or may not. If any other action in the loop delays it - such as serial output - the effective debounce time will be extended - but only by that amount and all other parts of the loop must abide by the same rules - not delaying the process other than by the actual execution of working code. Any and all time delays are effected by the same process of marking the milliseconds to start the delay and on each pass of the loop, checking how much time has passed to determine if the corresponding series of actions need to be taken.

Which action needs to be taken can be determined by noting the difference between the current and old "previous" states and this is important because this method can be used just as effectively on a byte value containing eight separate states (or in fact a longer word) as a single Boolean value, so that more than one state may be new (and of course, stable) and some may have changed in one direction and some in another.

This technique uses what is called a "vertical counter" if you want to search out more information.

Don

Just to clarify, that's exactly why I asked him to move the serial print delay , which has nothing to do with the debounce delay
to the END of his main loop so it would not effect his debounce time.
It is a standard one second delay that you see after a serial print statement:

Serial.println(reading); // prints its state to serial monitor
 delay(1000);

I haven't made any comments about his debounce issue other than to suggest the standard "old-school"
(circa 1977, TTL COOKBOOK) RC filter with a 1 k pullup (to +5V) resistor and a 0.1uF capacitor (to ground)
on his input line to the Arduino. I'm not sure yet if he has any intention of using that suggestion since it
doesn't seem to relate to his reason for the post.