Debouncing 2 pushbuttons in coin counter project jukebox


I,m looking for help for my project.
I have a jukebox with a coin controller that is unrepairable.
So i wrote a sketch with arduino to make my own coin controller.
It has 2 puls switches inputs.One is the coin switch (pulsePin) and a another puls switch (resetpin)

Both switches have a lot of bouncing so the counter is not accurate while counting up and counting down.

With the puls switch, i count 3 credits everytime it comes high
With the reset switch is pressed i count -1 credit.
So the pulsepin is the credit counter en the resetpin is the played records counter…

A0 is the output for a relay that unlocks the jukebox to select singles.

Can anyone adjust my sketch with debouncig of the two pulse switches?The code is below in txt file

Many many thanks!! Eric

const byte PulsePin = 2;
const byte ResetPin = 3;

void setup()
pinMode(PulsePin, INPUT);
pinMode(ResetPin, INPUT);
pinMode(7, INPUT);
pinMode(A0, OUTPUT);

void loop()
if (digitalRead(PulsePin)) {
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
} else {
//digitalWrite(A0, LOW);


static int PulseCount = 0;
static int ResetCount= 0;
static byte PulseState = LOW, ResetPulseState = LOW;

if (digitalRead(PulsePin) != PulseState)
if (PulseState == LOW)
PulseState = HIGH;
++ PulseCount ;
++ PulseCount ;
++ PulseCount ;
Serial.print("PulseCount = ");
PulseState = LOW;


if (digitalRead(ResetPin) != ResetPulseState)
if (ResetPulseState == LOW)
ResetPulseState = HIGH;
Serial.print ("Pulsecount = ");

ResetPulseState = LOW;

if (PulseCount <= 0) {

digitalWrite(A0, LOW);// Stop music


de juiste counter_voor_jukebox.txt (1.31 KB)

Your program is short so please include it in your Post using the code button </> so that people do not have to download it.

See How to use the forum


Please read the forum instructions and post your code so we can examine it directly.

I can give you really effective debounce code if needed. :sunglasses:

There is a difference between debouncing (which eliminates spurious contacts from swiches) and testing for a change of state - for example the switch changing from not-pressed to pressed.

For debouncing all that is usually needed is a short interval between successive digitalRead()s. Something like this

void loop() {
  static unsigned long prevButtonReadTime = 0;
  if (millis() - prevButtonReadTime >= 50) { // allow at least 50 millisecs between reads
     prevButtonReadTime = millis();
     pulsePinState = digitalRead(pulsePin);
     resetPinState = digitalRead(resetPin);
  // other code

You have not told us how your buttons are wired. Your code uses pinMode(pin, INPUT); so you should have an external pullup or pulldown resistor so the pin is not floating when the switch is not pressed. The more usual thing is to use pinMode(pin, INPUT_PULLUP); which uses the internal pullup resistor. Then you connect your switch so it connects to GND when pressed and digitalRead() will return LOW when it is pressed.


I use 22k pullup resistors on the 2 switches.

Thanks for the code but botch pulsepinstate as resetspinstate are not declared in this scope.
How do i fix that????

Thanks for the code but botch pulsepinstate as resetspinstate are not declared in this scope.
How do i fix that?

By declaring them (as byte) with all the other declarations at the start of your actual code of course. :grinning:

But I am afraid there is more to it.

Please read the forum instructions and post your code using “code” tags by highlighting it and using the “</>” icon in the composition window so we can examine it easily.