My button latching/debounce code, can anyone help make it work better?

Let me say I combined 2 peoples pieces of code so I could get an understanding of what is going on. I pretty much understand the code except maybe 1 part or 2. The thing is the debounce isnt working too great. If you hold the switch down too long it will bounce, also if you hold it too light it will bounce a lot.

The latching code itself's debounce is terrible ( no offense) also it prints

on
number of button pushes: 1
off
on
number of button pushes: 2
off
on

in the serial console. Im assuming this is cause of a bounce or just an error in the original code i havent been able to figure out yet (ive only been messing with it for a half hour)

I used part of Nick Gammon's Debounce code which Again didnt work perfectly but it worked better than the one that came with the latching code, so i combined both.

I even left the original authors comments because im not trying to be a thief im trying to learn and this has helped me a lot. Does anyone know a better way to debounce this so holding down the button wont cause bounces and or holding too little pressure wont cause bounces, or is it just the cheap tactile switches I have and not really the code lol.

Also if anyone wants to this could be posted in a list of Latching/Debounce code so others can learn from it, because I had a hard time finding the right info. Luckily I do have a programming background, just not with something that only uses a start up and a loop, im using to visual c++, scripting languages, some C but not containing all my code in a loop hehe.

Thanks all

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

//Debounce variables
const unsigned long debounceTime = 150; //milliseconds
unsigned long switchPressTime; //when the switch last changed state

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
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() 
{
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  
}


void loop() {
  // read the pushbutton input pin:
 int buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) 
  {

    //debounce
    if(millis() - switchPressTime >= debounceTime)
      {
        switchPressTime = millis(); //when we closed the switch
        // save the current state as the last state, for next time through the loop
        lastButtonState = buttonState;
        // if the state has changed, increment the counter
        if (buttonState == HIGH) // if the current state is HIGH then the button went from off to on:
        {   
          buttonPushCounter++;
          Serial.println("on");
          Serial.print("number of button pushes: ");
          Serial.println(buttonPushCounter);
        } 
      
        else 
        {
          // if the current state is LOW then the button went from on to off:
          Serial.println("off");
        }  
   
    // Delay a little bit to avoid bouncing
    //delay(150);
      }
      

      // turns on the LED every four button pushes by checking the modulo of the
      // button push counter. the modulo function gives you the remainder of the
      // division of two numbers:
      if (buttonPushCounter % 2 == 0) 
      {
        digitalWrite(ledPin, HIGH);
      } 
  
      else 
      {
        digitalWrite(ledPin, LOW);
      }
  }
}

The thing is the debounce isnt working too great. If you hold the switch down too long it will bounce, also if you hold it too light it will bounce a lot.

How is the switch wired to the arduino? Are you using any external pullup or pulldown resistors?

It will be easier in the long run if you learn to use pinMode (buttonPin, INPUT_PULLUP) which uses the internal pull ups of the processor chip. One side of the switch goes to ground and the other goes to the input pin. The logic will be reversed, and a normally open button will read LOW when pressed, and HIGH when not pressed.

cattledog:
How is the switch wired to the arduino? Are you using any external pullup or pulldown resistors?

It will be easier in the long run if you learn to use pinMode (buttonPin, INPUT_PULLUP) which uses the internal pull ups of the processor chip. One side of the switch goes to ground and the other goes to the input pin. The logic will be reversed, and a normally open button will read LOW when pressed, and HIGH when not pressed.

using external pull down resistor. I dont want the switch to be low when pressed and high when not pressed as I want to turn on the LEDs when the switch is pressed not turn them off. Wouldnt that be a problem with pull up resistors.

Astaldoath:
using external pull down resistor. I dont want the switch to be low when pressed and high when not pressed as I want to turn on the LEDs when the switch is pressed not turn them off. Wouldnt that be a problem with pull up resistors.

Of course not. Why would that be a problem? You can turn LEDs on or off at will. You can read a switch state. Just treat a LOW on the switch as an active state. It's a very common practice.

You can:

#define pushed LOW
.
.
.
if (buttonState == pushed)

Astaldoath:
The thing is the debounce isnt working too great. If you hold the switch down too long it will bounce, also if you hold it too light it will bounce a lot.

Sounds like your switch has bad contacts. Perhaps a better switch would produce better results.

Astaldoath:
I dont want the switch to be low when pressed and high when not pressed as I want to turn on the LEDs when the switch is pressed not turn them off. Wouldnt that be a problem with pull up resistors.

Before you do anything else, you need to understand that there is absolutely nothing that relates HIGH to turning something ON, or LOW to turning something OFF in regards to button or LED.

They are completely unrelated mechanisms. You probably have a light in your house that is controlled by 2 switches on the wall. Sometimes pushing one UP will turn it on, sometimes pushing one DOWN will turn it on.

Or perhaps, 'wax on, wax off'- either one can be in either hand. Right hand doesn't always have to be the wax on.

Your button, when pressed, can either put 5V into the IO pin, or it can put 0V into the IO pin. The resistor you choose will make sure that when you're not pressing the button, it will stay LOW or stay HIGH, respectively.

The board has internal PULLUP resistors that you can activate by using pinMode(thePin, INPUT_PULLUP).
This makes sure that an unpressed button will be seen by your program as HIGH, instead of some unknown state. When you press your button, you put 0V into the IO pin and it will read a definite LOW.

Your code options are

if (thePin == LOW){ //this means button is pressed
<do something, such as turn on the LED>
};

if (thePin == HIGH){ //this means button is not pressed
<do something else, such as turn off the LED>
};

Hope that makes sense.
Once it does, you also need to further understand that
digitalWrite (ledPin, HIGH);
does not necessarily turning an LED ON. It only means 5V at the ledPin. It depends on whether the anode or cathode of the LED is coming out of the IO pin, and the other going into GND or 5V, respectively.
If it's (IO pin)-(LED anode)-(LED cathode)-(GND), that digitalWrite HIGH will turn it on.
If it's (IO pin)-(LED cathode)-(LED anode)-(5V), that digitalWrite HIGH will turn it off.
I left out resistors for simplicity.