I am want to make a boxing timer with Two button and a LCD.
I have problem with my two button design.
one button consider to select value.
one button consider to confirm.
I tried to use do while on void setup() with those code.
The first do while is run sucessfully.
then I use the same do while code below the first one.
but why the second one didn't run and pass.
I use serial monitor to read the digitalRead value of next_btn and it is reported 1.
I think that is why the second do while is pass.
When you press a button, and slow down time, it is not simply on or off, but it makes contact, breaks contact and makes contact again a couple of times in a split second, as the contacts "bounce" against one another. Your arduino is fast enough to pick this up, and your code then mistakenly thinks the button was pushed and released and pushed again, when actually you are still trying to push it. Debouncing is the art of eliminating this bounce effect. It can be done in hardware by something simple like a capacitor or as complex as a debouncing IC, or it can be done in software (my preference).
In software, you would read the button state, and when it changes, note the time (millis() or micros()). Then give it a while (maybe 20 milliseconds) to settle, and see if the buton is still in the new state or not. during this time you could also monitor the state and include it in your processing. Only once the button is settled in a state for at least 20 milliseconds, do you accept this as it's new state, and only then process it as being pushed / released. You could also search for debouncing algorithms, and pick one that makes sense to you, and fits your project.
A quick look at your code shows most of the main program is in the setup() routine but should be in the loop() routine. Setup() is only run once after the arduino is powered to initialize ports, hardware etc, then loop is executed for ever more (or until power is removed).
Below is a simple routine I use to check button presses on buttons with pull down resistors. If your using pullup then reversing the HIGH/LOW logic should work. When called it reads button pin and if it's pressed will wait debounceDelay time and read the pin again and return this as button state. If button is not pressed it returns immediately.
const long debounceDelay = 20; //The button debounce time
// Button = pin number to read
int checkButton(int Button) {
int buttonState = digitalRead(Button); // Read button
if (buttonState == HIGH) { // If button pressed then wait a bit to allow for debounce
long Time = millis();
while ((millis() - Time) < debounceDelay) {
}
buttonState = digitalRead(Button); // Read button again
return buttonState; // Return button state
}
else {
return LOW;
}
}