-1

I tried to make a controller box with multiple buttons. Sadly, when I tried to connect the buttons, the Arduino Pro Micro didn't recognize them being pressed. I even tried to connect a wire directly (and a directly a resistor from GND to the ports) from GND to the ports but it doesn't seem to matter.

OUTPUT works on the same ports, but INPUT/INPUT_PULLUP does not. Is there something wrong with the code?

My Arduino Pro Micro is a Chinese knockoff. Keyboard and Mouse are working, OUTPUT is working. It doesn't seem to work with the input.

PS: I have worked with Arduino UNO before (and also the ESP family (ESP8266, ESP32)).

PS2: Sometimes, if I touch the microcontroller's back side some values register (like I pushed all the buttons at once)

#define LED_PIN 4
#define BUTTON_1 10
#define BUTTON_2 16
#define BUTTON_3 14
#define BUTTON_4 9

byte ledState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_1, INPUT_PULLUP);
  pinMode(BUTTON_2, INPUT_PULLUP);
  pinMode(BUTTON_3, INPUT_PULLUP);
  pinMode(BUTTON_4, INPUT_PULLUP);
}

void loop() {

  byte one = digitalRead(BUTTON_1);
  byte two = digitalRead(BUTTON_2);
  byte three = digitalRead(BUTTON_3);
  byte four = digitalRead(BUTTON_4);

  if (one == HIGH) {
    Serial.println("BUTTON_1");
  }
  if (two == HIGH) {
    Serial.println("BUTTON_2");
  }
  if (three == HIGH) {
    Serial.println("BUTTON_3");
  }
  if (four == HIGH) {
    Serial.println("BUTTON_4");
  }

  digitalWrite(LED_PIN, ledState);
  ledState = !ledState;
  delay(1000);
}

Edit: corrected the code

Mention: I wired the buttons the following way (I have to mention it, because the Arduino Pro Micro doesn't have a sepparate 3V/5V pin, it only has RAW output that by my undertanding can fry the board)

Tutorial from where I inspired the button wiring: https://arduinogetstarted.com/tutorials/arduino-button

9
  • 4
    try byte one = digitalRead(BUTTON_1); Commented Sep 1, 2023 at 15:15
  • 2
    Or closer to the form you have it: digitalRead(BUTTON_1) == HIGH; though the form jsotola suggests is better. What you have is a typo, unless you basically didn't already know this was incorrect and need an explanation as to why the placement of ) matters. Commented Sep 1, 2023 at 18:21
  • 1
    As mentioned it really matters where you put parentheses. As none of your button pins is pin 1, comparing them with HIGH (== 1) always results into 0 and that exact 0 was used for all digitalReads. Commented Sep 1, 2023 at 18:41
  • Sorry....I modified the code in the meanwhile (I observed after I posted that I haven't copyed the good code....it looks like so, because the if's were looking the following way, but I started using variables instead: if(digitalRead(BUTTON_1) == HIGH) to rule out the compiler problems (and when I deleted the parantheses, I deleted the wrong ones) => I have corrected the code in the meantime but still it doesn;t work) Commented Sep 2, 2023 at 21:31
  • With your latest schematic/code (a) the buttons will be LOW when pressed, not HIGH (b) your schematic shows the connection of a button on pin 7 but your code has no entry for pin 7 and (c) the delay of 1 second in the loop means you may have to hold the button for up to 1 second to see a response. If touching pins on the board has the same effect as pressing a button then the buttons are in a floating state indicating that the wiring is incorrect. Commented Sep 3, 2023 at 4:31

2 Answers 2

1

The way you have it wired, INPUT_PULLUP will make the buttons look HIGH, so in your code you need to test if they are going LOW, which is what grounding the button would do.

1
  • Yes, this was also a problem. I tried setting the pins previously to HIGH/LOW but it worked only on the first button press, after that it worked as before (could not read button state correctly). An improvement to the code can be adding a previous state check also. Commented Sep 11, 2023 at 21:58
0

After looking at many tutorials and trying many approaches with different Arduinos (Uno, because it's easyer to prototype) (and taking a short brake so I can try again from 0 with a new mindset) I made it work.

Before, I could do it because I was using Microcontrollers that had a 3/5V output also (ex. Uno, Mega, Nano, STM32, ESP8266, ESP32), so I could do a pull-up/down circuit, but because the Arduino Micro doesn't have such pins (it has only RAW as output, which can fry the board if it's externally powered) I had to use this method where I only use a GND pin and a Digital Input without any ressistors wired to the button.

Thanks for the answers and suggestions!

The new code (without the LED functionality):

#include "Keyboard.h"

// defining button pin numbers
// TODO: rename variables according to functionality
#define BUTTON_10 10
#define BUTTON_16 16
#define BUTTON_14 14
#define BUTTON_9 9

// giving them a first-state of HIGH
int previousButtonState10 = HIGH;
int previousButtonState16 = HIGH;
int previousButtonState14 = HIGH;
int previousButtonState9 = HIGH;


void setup() {
  // initializing the buttons as INPUTS with PULLUP (internal pull-up resistor from Arduino)
  pinMode(BUTTON_10, INPUT_PULLUP);
  pinMode(BUTTON_16, INPUT_PULLUP);
  pinMode(BUTTON_14, INPUT_PULLUP);
  pinMode(BUTTON_9, INPUT_PULLUP);

  // only for test
  // TODO: Move this only to the function needed when more functionality is added
  Keyboard.begin();

}

void loop() {
  // checking if any of the buttons were pressed and saving the state in a variable
  previousButtonState10 = checkButton(BUTTON_10, previousButtonState10);
  previousButtonState16 = checkButton(BUTTON_16, previousButtonState16);
  previousButtonState14 = checkButton(BUTTON_14, previousButtonState14);
  previousButtonState9 = checkButton(BUTTON_9, previousButtonState9);
}

// function: reads the button state and compares it with the previous state to find out if it was pressed or not
// params: buttonPin => the pin number of the button; previousButtonState => the state corresponding for each button
// return: it returns the red state, so it can be saved as previousState in the coresponding parameter
int checkButton(int buttonPin, int previousButtonState) {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW && previousButtonState == HIGH) {
    Keyboard.println(buttonPin);
    // some minimal debouncing
    // TODO: WIll be a problem when using millis() in the future
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState == LOW) {
    // not needed, but some functionality could be added in the future
    // Keyboard.println("release");
    delay(50);
  }

  return buttonState;
}

Now I can work further on improving and extending the code to make the "Macro Board" project with mouse movement (and I also have to solder the buttons and LEDs to the board :P )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.