Unvalidate Changed Button State

For my code below, the power and secondPin must be HIGH to start the code and firstPin must change state to as well.

I want the LED to go LOW if: (topPinstate == HIGH or LOW) && (secondPinstate == LOW)

The firstPinstate change state is the problem I think. When I take power away from either pin the LED stays HIGH.

const int power = 12; // power button
const int  topPin = 11; // top sensor
const int secondPin=10; //second sensor
const int ledPin = 9;   // LED output

int topPinstate = 0;   // current state of the button
int lastPinstateswitch = 0;  // previous state of the top pin
int powerState=0;
int secondPinstate = 0;
int ledPinstate=0;

#define COMMON_ANODE //common anode led requirement 

void setup() {
  pinMode(topPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(secondPin, INPUT);

void loop() {
  powerState = digitalRead(power);
  if (powerState == HIGH){ //make sure the power is on
  topPinstate = digitalRead(topPin);
  ledPinstate= digitalRead(ledPin);
  secondPinstate = digitalRead(secondPin);

  // compare the buttonState to its previous state
  if (topPinstate != lastPinstateswitch) {
    // if the state has changed go ahead 
  if (topPinstate== HIGH && secondPinstate == HIGH) { digitalWrite(ledPin, HIGH);
                                     //THIS IS WHERE I RUN INTO TROUBLE
  else if ((topPinstate == LOW)&& secondPinstate == LOW || HIGH ) { digitalWrite(ledPin, LOW);

  else if (powerState == LOW) {digitalWrite(ledPin,LOW);}      
void setColor(int green)
   green = 255 - green;
  analogWrite(power, green);

If you don't care whether secondPinstate is HIGH or LOW, then just omit the && and everything else up to but not including the ).

If you want something else, then you need to explain more clearly.

Ok so it's actually the opposite of what you are saying. I don't care if topPinstate is HIGH or LOW, but I do care when secondPinstate is LOW.

topPinstate will go LOW by default but I still want the code to run. But...when secondPinstate goes LOW, then I want LED LOW.

I hope that explains it more clearly.


So perhaps you intended this:

                                     //THIS IS WHERE I RUN INTO TROUBLE
  else if (secondPinstate == LOW) { digitalWrite(ledPin, LOW);

To post:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

You did #2 but not #1. That makes it harder than necessary to follow your code.

You talk about “topPinstate will go LOW by default…” and “…the code…” but I just don’t understand. Sorry.