0

Functionality:

When user presses the big dome push button, the state of Arduino should turn from '0'/LOW to '1'/HIGH at the serial monitor of the Arduino.

Issue:

When I trigger on the big dome push button, the state did not trigger from LOW to HIGH, it still remained LOW.

I have connected the "Push To Make" side of the connection to digital pin 2, following the connection write-up from: BIG DOME PUSH BUTTON.

However at this point, the trigger state is not working, please assist.

const int buttonPin = 2; //the number for the pushbutton pin (DIGITALPIN)

uint8_t btnCnt = 1;

bool outputState = false;

void setup() {

  Serial.begin(9600);
  //for Push button pin
  pinMode(buttonPin, INPUT);

}

void loop() {

  outputState |= digitalRead(buttonPin); // if pushButton is high, set outputState (low does nothing)

  // Print the output
  if (outputState) {

    switch (btnCnt++) {
      case 100:
        --btnCnt;
        outputState = false;
        break;
    }

    Serial.println("1");
  } else {

    Serial.println("0");
    btnCnt = 0;
  }

  delay(100);
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Luke
  • 982
  • 1
  • 7
  • 27
  • The code `outputState |= digitalRead(buttonPin);` assumes that returning `HIGH` will be equivalent to `true` and `LOW` is equivalent to `false`. Try to replace by `outputState = (HIGH == digitalRead(buttonPin));`. – J. Piquard Mar 05 '17 at 09:57
  • @J.Piquard, still not triggering – Luke Mar 06 '17 at 01:19
  • 1
    @Luke this is an issue that it's easy to debug having the hardware at your disposal.. you could start by adding `Serial.println(outputState)` after the aforementioned instruction, and see what value is inside that variable. – Patrick Trentin Mar 06 '17 at 09:25

1 Answers1

1

The statement outputState |= digitalRead(buttonPin); is using an OR assign so once outputState is set to 1 (HIGH), it will never go back to 0 (LOW) again. This is because performing an OR with anything and a 1 will always result in 1.

If you change this line to just an assignment as follows, you should see the state change you are expecting.

outputState = digitalRead(buttonPin);
cmar8919
  • 26
  • 3