Why is debounce button not working??

A little while ago I made a function to debounce a button and return its value. The void loop then read the button to know whether or not to blink the LED. It worked perfectly. I am now using this exact same function to Serial.println when button is pressed, but this isn't working... the state of the button keeps reading zero no matter what. Any ideas?

boolean lastButton = LOW;
long lastDebounce = 0;
const int BUTTON = 4;
const int buttonVal = LOW;

void setup() {
  // put your setup code here, to run once:
pinMode(BUTTON, INPUT);
}

boolean debounce(boolean lastButton, long lastDebounce, int BUTTON)
{
  boolean buttonState;
  boolean current = digitalRead(BUTTON);
  if (current != lastButton){ //if the button state has changed
    lastDebounce=millis();  //reset timer
    }

  if ((millis()-lastDebounce) > 5)  //if 50 milliseconds has passed since last bounce
  {  
    buttonState = current ;  //read value again now that bouncing is over
    }
  return buttonState;
} 

void loop() {
  // put your main code here, to run repeatedly:
    boolean buttonVal = debounce(lastButton, lastDebounce, BUTTON);
  lastButton = buttonVal;
  Serial.println(buttonVal);
  delay(500);
}

In case you need it, here is the LED program

boolean lastButtonState=LOW;
long lastDebounceTime=0;
boolean ledState=LOW;
boolean currentButtonState=LOW;
const int button = 2;
const int LED = 4;

void setup() {
  
pinMode(button,INPUT);
pinMode(LED,OUTPUT);
}

boolean debounce(boolean lastButton, long lastDebounce, int BUTTON)
{
  boolean buttonState;
  boolean current = digitalRead(BUTTON);
  if (current != lastButton){ //if the button state has changed
    lastDebounceTime=millis();  //reset timer
    }
 
  if ((millis()-lastDebounce) > 5)  //if 50 milliseconds has passed since last bounce
  {  
    buttonState = current ;  //read value again now that bouncing is over
    }
  return buttonState;
} 

void loop() {
Serial.begin(9600);  
currentButtonState = debounce(lastButtonState, lastDebounceTime, button); //currentButtonState equals returned value of debounce function

if (lastButtonState==LOW && currentButtonState==HIGH) //if state of button has changed from low to high
{ 
  ledState= !ledState;  //reverse button state
}
lastButtonState=currentButtonState; //reset last button state

digitalWrite(LED, ledState);  //power on LED
}

Remove the delay(). You're missing button presses while you're waiting.

long lastDebounce;
should be:
unsigned long lastDebounce;

if ((millis()-lastDebounce) > 5) //if 50 milliseconds has passed since last bounce
5 is not 50 ms

Having argument names with the same names as global variables is NOT a good idea. Passing global values to functions is pointless.

Using three variables, buttonState, current, and lastButton, to determine whether the state of a pin changed is one too many.

Yes, I know you copied the example, but the example is crap.

@PaulS;
I've heard you say many times that a debounce only requires 2 variables, could you post an example for those of us with depleted uranium skulls? :smiley:

could you post an example for those of us with depleted uranium skulls?

The current state is something.
The previous state was something.

Either the current state is the same as the previous state, or it isn't.

If the state has changed, the question is do we care? Is this a valid change or did the switch bounce? The answer to that question does not involve another state. It is a matter of the interval between this change and the last change.

If the interval is tiny, we consider that the switch bounced, so we want to ignore that change. We do that by NOT updating the previous state to match the current state.

I think that, given this explanation, anyone could write the code.

Ok maybe I am just a crappy programmer, but the point is, the debounce function worked fine for the LED. Why does it not work for Serial.println?

  boolean buttonState;

What is the value in buttonState now? You have NO idea.

You only sometimes assign a value to buttonState, before you return it. You should make sure that you ALWAYS assign a value to buttonState, regardless of which path through the function gets followed.

but the point is, the debounce function worked fine for the LED.

You got lucky.

Why does it not work for Serial.println?

Can't win the lottery every time.

The logic is surprisingly complicated, something like:

unsigned long change_time = 0 ;
boolean changing = false ;
boolean previous = false ;
boolean button_state = false ;

boolean check_button ()
{
  boolean current = digitalRead (pin) ;
  if (current != previous)   // detect a change
  { 
    changing = true ;
    change_time = millis () ;  // (re)start the timer, so to speak
    previous = current ;
  }
  else if (changing && millis () - change_time > 50)  // detect time-out so can actually report debounced state
  {
    changing = false ;
    button_state = current ;
  }
  return button_state ;
}

Which is why you want to use a library for this kind of thing - its got a lot of messy details
(for instance I've arbitrarily initialized my variables to false at reset without checking the
button's pin at all.

I tried fixing my program and I can now get it to print the lines I wanted it to, but it won't stop printing the lines. I cant get the button state to change to LOW.

const int rLed = 3;
const int gLed = 2;
const int tempSensor = A0;

void setup() {
  
pinMode(rLed, OUTPUT);
pinMode(gLed, OUTPUT);
pinMode(9, INPUT);
Serial.begin(9600);
}

boolean debounce()
{
  static boolean buttonState;
  static boolean lastButtonState;
  static boolean currentState = digitalRead(4);
  static unsigned long lastDebounceTime;
  if (currentState != lastButtonState){ //if the button state has changed
    lastDebounceTime=millis();  //reset timer
    }
 
  if ((millis()-lastDebounceTime) > 50)  //if 50 milliseconds has passed since last bounce
  {  
    buttonState = digitalRead(4);  //read value again now that bouncing is over
    lastButtonState = buttonState;
    }
  return buttonState;
} 

void loop() {

int temp = analogRead(tempSensor);
float tempVolt = ((5000.0*temp)/1023.0);
float tempRead = ((9.0*tempVolt)/50.0 - 58.0);

if(temp > 150){
 
  if(temp < 190){
  digitalWrite(gLed,HIGH);
  digitalWrite(rLed,LOW);
  }

  }
else{
  digitalWrite(rLed,HIGH);
  digitalWrite(gLed,LOW);
  }
  
//Debounce Function
  boolean lastButtonVal;
  boolean buttonVal = debounce();
  static unsigned long lastPrint;
  if (buttonVal == HIGH && lastButtonVal == LOW){ 
    if(millis() - lastPrint > 2000){
  Serial.print("F =");
  Serial.println(tempRead);
  Serial.print("D =");
  Serial.println(temp);
  Serial.print("mV =");
  Serial.println(tempVolt);
  Serial.println();
  lastButtonVal = buttonVal;
  lastPrint = millis();
  }
 
  }
  
}

Cross-posted here and here.

DRLDavis:
I tried fixing my program and I can now get it to print the lines I wanted it to, but it won't stop printing the lines.

Try not cross-posting. Some of us visit multiple sites and don't want to answer the same question twice.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.