Hey,
after buying an Arduino Nano and a couple of sensors, I started my project as a newcomer in the arduino world.
It's going quite good yet but I'm recieving wrong button associations when using two or more digitalRead();
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
//button3State = digitalRead(button3Pin);
This code is ending in the following Result when just pressing button 2:
Button 2
Button 1
Button 2
Button 2
Button 1
while this code:
//button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
//button3State = digitalRead(button3Pin);
Button 2
Button 2
Button 2
Button 2
Button 2
Is there any way to fix that? All buttons are connected to a seperare pins.
Regards,
Denis
Thank you for using code tags. However, post ALL the code.
And please post a circuit diagram showing what you have connected to the Arduino and how everything is wired. Buttons switch to ground? With pull up resistors or using internal pullup?
Steve
Also, as a rule of thumb, the moment you start using numbers in your variable names you should have a serious look at using arrays instead.
First of all, thanks for the fast replies.
slipstick:
And please post a circuit diagram showing what you have connected to the Arduino and how everything is wired. Buttons switch to ground? With pull up resistors or using internal pullup?
Steve
Thanks for the question because you made me rethink my connections. The problem was, I connected one 10k resistor to GND and then connected all 3 buttons to this resistor. The wires going to the arduino were connected to the same button pin as GND instead of the other side of the button. Using 3 10k resitors and switching the button pin for GND solved the problem.
I searched for some webapplications to draw a circuit diagram but wasn't able to find a good one yet. Do you have any recommendations?
wvmarle:
Also, as a rule of thumb, the moment you start using numbers in your variable names you should have a serious look at [using arraysp/url] instead.
[/quote]
Thanks for the advice! I'm going to change that. :)](https://www.arduino.cc/en/Tutorial/Arrays)
Buttons you can wire between the pin and GND, then enable the internal pull-up (set pinMode to INPUT_PULLUP). No need for external resistors normally.