debonce return

Scope: I am trying to use the debounce example built into the IDE and modifying it to return button's state. Once I debounce the button, i wrote the 2nd piece of code below to track the button presses by incrementing numbers in an array.

Problem: However, my debounce function keeps returning low even though all inputs are INPUT_PULLUP HIGH. When the debounce function is run continually on its own, it corrects itself, but always returns low its first runs through, throwing a false button press to exit my while loop.

I have gone through the example and commented in my own words to try to ease my understanding of the code. Thanks for your help :D !

int debounce(int pin) {

int reading = digitalRead(pin); //checs initial state of pin passed to this function

// If the switch changed for any reason (noise or actual press) if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } //when the previous if statement is false, it skips the reset // this means that on false, the next if loop will execute as true // since lastdebouncetime was not updated

if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer // than the debounce delay, so take it as the actual current state:

// if the button state has changed back, ending the button press if (reading != buttonState) { buttonState = reading; // sets buttonState = to the reading if they are different, why?

//i want to return the buttonstate here return buttonState; } } lastButtonState = reading; //sets the last button state equal to what was read, before the program is executed again }

int setDispenseTime()//if any of the buttons were pressed, the button increments the array { //in the correct time slot while(debounce(12) == LOW); //allows an exit out of setdispensetime loop when while condition is false { if(debounce(10) == LOW) //pin 10 = hour { timeArray[0] = timeArray[0] + 1; // increments the day counter in the array if(timeArray[0] = 13) //makes hour loop at 24 {timeArray[0]=1;} } if(debounce(11) == LOW) //pin 11 = minute { timeArray[1] = timeArray[1] + 1; if(timeArray[1] = 61) {timeArray[1] = 0;} }

} }

return buttonState;
  lastButtonState = reading;

If the function has returned, how do you expect it to do the last line?

Do you really need any debounce ?