Push button to reset total coin value

Hi,
My project is a coin counter, which sorts the coins by size and in each column is an LDR and LED, so when the coin comes in between, the LDR reads a LOW value and adds the coin value to the total coin value displayed on the LCD screen. I want to add a pushbutton that when pressed it resets the total coin value (displayed on LCD screen) so the coins can be emptied and a new count can be started. How will the code for the button be written? would an if function work? e.g.,

if (digitalRead(PUSHBUTTON)==LOW);
resetCoinTotal();

void resetCoinTotal() {
  for (int i = 0; i < 6; i++) {
    coinNumber[i] = 0;

drop the semi colon at the end of that line. it should be like

if (digitalRead(PUSHBUTTON)==LOW) resetCoinTotal();

or if you have more than one statement in the if() the you group them with a compound statement with the { }

if (digitalRead(PUSHBUTTON)==LOW) {
  resetCoinTotal();
  ... // other instructions here
}

remember buttons can bounce, I'd suggest you look at one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ... to manage your button if you want to do the reset only once.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.