const int buttonPinLeft = 13; // pin for the Up button
const int buttonPinRight = 10; // pin for the Down button
const int buttonPinEsc = 12; // pin for the Esc button
const int buttonPinEnter = 11; // pin for the Enter button
//records which button has been pressed
if (buttonEnterState==HIGH){
lastButtonPushed=buttonPinEnter;
enter = true;
right = false;
left = false;
}else if(buttonEscState==HIGH){
lastButtonPushed=buttonPinEsc;
//escape = true;
enter = false;
right = false;
left = false;
}else if(buttonRightState==HIGH){
lastButtonPushed=buttonPinRight;
right = true;
left = false;
enter = false;
}else if(buttonLeftState==HIGH){
lastButtonPushed=buttonPinLeft;
left = true;
right = false;
enter = false;
What I did was set the digital.Write High in order to enable the pull up resistor according to the manufacture.
Then I send the button state to serial.print and I was suprised to see that the button state was 1 when idle and 0 when pressed.
So I modified the code as following:
void readButtons(){ //read buttons status
int reading;
int buttonEnterState=digitalRead(buttonPinEnter); // the current reading from the Enter input pin
int buttonEscState=digitalRead(buttonPinEsc); // the current reading from the input pin
int buttonLeftState=digitalRead(buttonPinLeft); // the current reading from the input pin
int buttonRightState=digitalRead(buttonPinRight); // the current reading from the input pin
totemos:
I was suprised to see that the button state was 1 when idle and 0 when pressed.
That's a consequence of using pullUP: the pin is taken up to high normally, and is then taken low when you activate the switch. For that reason, this approach is known as "active low".
JimboZA:
That's a consequence of using pullUP: the pin is taken up to high normally, and is then taken low when you activate the switch. For that reason, this approach is known as "active low".