Help, running 2 if statement on 1 click

Hi, I want know what's is wrong my code here. What I want is to have the first button click to add then revert back to last value on the second click. However when I do the first button click it also prints/run the second statement outputting "1 0" where it should 1 first then 0. Thank you

const int buttonPin = 2;
int buttonState = 0;
int currentState = 0;

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);

}

void loop() {
buttonState = digitalRead(buttonPin);
if ((currentState == 0) && (buttonState != HIGH)) {
currentState++;
Serial.println(currentState);
delay(500);
}
if ((currentState == 1) && (buttonState != HIGH)) {
currentState--;
Serial.println(currentState);
delay(500);
}
}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

1 Like

Make the second if an else/if so that only one of the if statements is evaluated each time through loop()

What does that do?

1 Like

it's more convention to have a single block of code the detects a button press which then performs whatever action if required depending on state versus having separate blocks for each state.

enum { Off = HIGH, On = LOW };

#undef MyHW
#ifdef MyHW
const int buttonPin = A1;
#else
const int buttonPin = 2;
#endif

int buttonState = Off;
int currentState;

void setup() {
    pinMode(buttonPin, INPUT_PULLUP);
    Serial.begin(115200);
}

void loop() {
    byte but = digitalRead(buttonPin);

    // detect change in state
    if (buttonState != but) {
        buttonState = but;

        // process button press
        if (On == buttonState)  {
            // toggle state
            currentState = ! currentState;
            Serial.println(currentState);
        }

        delay (10);     // debounce
    }
}
1 Like

Thank you for the response GCJR code works fine. However how can I insert my command that If currentState = 0 then do this if currentState = 1 then do this. I want to dit it with one button only that's why I have this currentState++; and currentState--; so it would increment and decrement after successful click. Please help thank you

const int buttonPin = 2;
int buttonState = 0;
int currentState = 0;

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);

}

void loop() {
buttonState = digitalRead(buttonPin);
if ((currentState == 0) && (buttonState != HIGH)) {
currentState++;
Serial.println(currentState);
delay(500);
}
if ((currentState == 1) && (buttonState != HIGH)) {
currentState–;
Serial.println(currentState);
delay(500);
}
}
1 Like

It decrement the currentState after getting increment by first button click

1 Like

Try your loop like this..

void loop() {

  buttonState = digitalRead(buttonPin);                  // Read the button, save in buttonState.
  if (buttonState != HIGH) {                             // If its not high.. Or if its LOW (grounded)
    if (currentState == 0) {                             // If current state = 0..
      currentState++;                                    // Bump up current state.
      Serial.println(currentState);                      // Let the user see what we have.
    } else {                                             // Else, current state was = 1..
      currentState--;                                    // Pop one off current state.
      Serial.println(currentState);                      // Show Mrs user what we got.
    }
    while (!digitalRead(buttonPin)) {                    // While the button is being held down..
      delay(500);                                        // Waste a 1,000 years.
      if (!digitalRead(buttonPin)) {                     // Are they done yet?
         Serial.println("Let go of the damn button!");   // Yell at the user.
      }
    }
  }
}

-jim lee

1 Like

What you spelled, "currentState–" does not. The line does nothing as is.

You got "currentState++" right, now you need to learn to spot typing/syntax errors through practice.

1 Like

Logical false is zero and logical true while all not-zero usually generates a one.

if ( 0 ) { never executes };
if ( !0 ) { always executes }; // ! is logical NOT

Bit logic might just blow your mind, it's for experienced beginners.

1 Like

Thank you for all the answers, will take note all of this. Really appreciate it!

0 and 1 is 2 possible states, binary false and true.

With decimal, 11 is 1x10 + 1x1.
With binary, 11 is 1x2 + 1x1 is decimal 3.

Computer runs on binary. A byte is 8 bits in a row; 128,64,32,16, 8,4,2,1

Bit math is a power tool for code.

https://playground.arduino.cc/Code/BitMath/

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.