Using Arcade Buttons [solved]

I am attempting to use these Arcade Buttons:

With an Arduino Mega2560

I have hooked up the button like the attached picture (I tried to put it in-line but it wouldn't let me post).

The code I am running is this:

#include <ezButton.h>
ezButton button(6);
// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
// the loop function runs over and over again forever
void loop() {
  button.loop(); // MUST call the loop() function first

  int d = 1000;
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(d);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(d); 

  int btnState = button.getState();
  Serial.println(btnState);

  if(button.isPressed()) {
    Serial.println("The button is pressed");
  }

  if(button.isReleased()) {
    Serial.println("The button is released");
  }
  delay(d); // wait for a second
}

I think the ezButton library should take care of the pullup behavior and the debounce stuff. (Which I was not able to figure out on my own) However the output I get on the serial monitor indicates that neither button.isReleased() or button.isPressed() is coming back true. I am not sure what is wrong with the code. I wonder if the pin labeled digital pin 2 on the mega2560 is not pin 6, but I cannot find anything online to definitively tell me that. Anything I can find makes me thing it IS pin 6.

Here is the output from the serial monitor, as you can see only the Serial.println(btnState); line seems to be printing:

20:37:10.177 -> 1
20:37:13.182 -> 1
20:37:16.185 -> 1
20:37:19.194 -> 1
20:37:22.180 -> 1
20:37:25.194 -> 1
20:37:28.175 -> 1
20:37:31.176 -> 1
20:37:34.180 -> 1
20:37:37.186 -> 1

I have tried two different buttons, so I don't think it is a DOA button. I do not have multimeter readings available at this time. The LED on the button is flashing as per the code.

Any help would be appreciated. Let me know if there is any other information I could provide that would help with answering my question.

What does the button.loop() function actually do? Have you tried commenting it out just to see what happens?

traptw1thin:
I think the ezButton library should take care of the pullup behavior and the debounce stuff. (Which I was not able to figure out on my own)

Your link to the button is bad.
Try adding a pullup yourself.

Assuming button.loop() reads the button (I don’t know what it does) then returns you then have 3 seconds of delay where nothing much happens, so even if button.loop is correctly reading the button you will have to hold it pressed for 3 seconds to get a reliable output.

You have written your code to read pin 6 but connected your button to pin 2.

There’s lots of questions on here about reading buttons successfully, do a search and read the help others have had. I personally have never heard of the library you are using.

Here is some code the reads some buttons, it’s not perfect and I know it can be improved, but it does work.

/* Simple button debounce for 4 buttons. Increments a count and sends the updated count to the serial monitor once per button press */
/* Tested on an Uno */
/* Connect simple push to make buttons between 0V and pin 2, 0V and pin 3, 0V and pin 4 and between 0V and pin 5 */

#define noOfButtons 4     //Exactly what it says; must be the same as the number of elements in buttonPins
#define bounceDelay 20    //Minimum delay before regarding a button as being pressed and debounced
#define minButtonPress 3  //Number of times the button has to be detected as pressed before the press is considered to be valid

const int buttonPins[] = {2, 3, 4, 5};      // Input pins to use, connect buttons between these pins and 0V
uint32_t previousMillis[noOfButtons];       // Timers to time out bounce duration for each button
uint8_t pressCount[noOfButtons];            // Counts the number of times the button is detected as pressed, when this count reaches minButtonPress button is regared as debounced 
uint8_t testCount[noOfButtons];             //Test count, incremented once per button press

void setup() {
  uint8_t i;
  uint32_t baudrate = 115200;
  Serial.begin(baudrate);
  Serial.println("");
  Serial.print("Serial port connected: ");
  Serial.println(baudrate);
  for (i = 0; i < noOfButtons; ++i) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    Serial.print("Testcount ");
    Serial.print(i);
    Serial.print(" = ");
    Serial.println(testCount[i]);
  }
}

void loop() {
  debounce();
  delay(10);     //Your other code goes here instead of this delay. DO NOT leave this delay here, it's ONLY for demonstration.
}

void debounce() {
  uint8_t i;
  uint32_t currentMillis = millis();
  for (i = 0; i < noOfButtons; ++i) {
    if (digitalRead(buttonPins[i])) {             //Input is high, button not pressed or in the middle of bouncing and happens to be high
        previousMillis[i] = currentMillis;        //Set previousMillis to millis to reset timeout
        pressCount[i] = 0;                        //Set the number of times the button has been detected as pressed to 0
      } else {
      if (currentMillis - previousMillis[i] > bounceDelay) {
        previousMillis[i] = currentMillis;        //Set previousMillis to millis to reset timeout
        ++pressCount[i];
        if (pressCount[i] == minButtonPress) {
          doStuff(i);                             //Button has been debounced. Call function to do whatever you want done.
        }
      }
    }
  }
}

// Function to do whatever you want done once the button has been debounced.
// In this example it increments a counter and send the count to the serial monitor.
// Put your own functions here to do whatever you like.
void doStuff(uint8_t buttonNumber) {
  ++testCount[buttonNumber];
  Serial.print("Button ");
  Serial.print(buttonNumber);
  Serial.print(" testcount = ");
  Serial.println (testCount[buttonNumber]);
}

This should be very simple.
I think relying on the button library might be causing confusion.

You should be able to wire the button with 5v and to a digial input and read it high or low

You should be able to feed the button 5v with a resistor and have an LED go to ground and see the LED lgiht when you press the button.

Often we worry too much on de-bounce when the problem does no exist in our projects.

dave-in-nj:
Often we worry too much on de-bounce when the problem does no exist in our projects.

Generally it matters when the button or switch is a toggle, or is responsible in some manner for counting.

IMHO there are fundamentals and there are enhancements.

Getting the switch to work is fundamental
Adding debounce is an enhancement

Hi,
Welcome to the forum.

Do you have a DMM?
If so with your project powered up, measure the voltage across the open contacts of the button.
If pull-ups has been activated you should read about 5V accross the contacts.

Did you try this example code and see if the button could be read?

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
 *
 * This example reads the state of a button without debounce and print it to Serial Monitor.
 */


#include <ezButton.h>


ezButton button(6);  // create ezButton object that attach to pin 7;


void setup() {
  Serial.begin(115200);
}


void loop() {
  button.loop(); // MUST call the loop() function first


  int btnState = button.getState();
  Serial.println(btnState);
}

I have edited it for your project.

Tom… :slight_smile:

Re: SteveMann

I fixed the button link, thank you.

Re: PerryBebbington

I wrote the code with 6 for the button based on some pin mappings info I found online. I have since found that this was in fact the issue. I was so convinced it couldn’t be 2 because of a project I did with some other Arduinos a while back where I learned that the pin numbers on the board were not the numbers to use in the code. I also had a friend who does a lot more arduino projects than me tell me that he was pretty certain I should use 6 in the code if I was connected to silkscreen pin 2. Since changing the code to use pin 2, this is now working.

Thank you everyone for your help. I am sorry I did not get back to this earlier, I must have my notifications set up wrong as I didn’t even know anyone had responded until today.

Thanks for the update. Not many people do it but it helps if you go back to your original post and edit the title to something like
Using Arcade Buttons [solved]

I'm sure everyone here would appreciate some Karma too.