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

byte one = digitalRead(BUTTON_1);
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.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)