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(power,INPUT);
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)
{
#ifdef COMMON_ANODE
green = 255 - green;
#endif
analogWrite(power, green);
}