I am creating a program that turns on and off an led. When two buttons are considered "high". But there is a twist, The first button needs to stay "high" until the second button is pressed and the led turns on. Here is my code so far.
// Boolean logic for Button Switch
boolean currentState = LOW; //stroage for current button state
boolean lastState = LOW; //storage for last button state
boolean ifcondState = LOW; //storage for the current state of the ifturn (off/on)
// set pin numbers:
const int buttonPinA = 3; // the number of the pushbutton pin
const int buttonPin1 = 4; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int ifcond = 12; // the number of the ifcond pin
const int ifturn = 11; // the number of the ifturn pin
// variables will change:
int buttonStateA = 0; // variable for reading the pushbutton status
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonStateIf = 0; // variable for reading the If Condition status
void setup() {
//initialize Serial connection
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinA, INPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
// If Condictional Pin
pinMode(ifcond, OUTPUT);
// If Turnon Pin
pinMode(ifturn, INPUT);
}
void loop() {
// read the state of the pushbutton values:
currentState = digitalRead(buttonPinA);
buttonState1 = digitalRead(buttonPin1);
buttonStateIf = digitalRead(ifturn);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (currentState == HIGH && lastState == LOW){ // if button has just been pressed
Serial.println("pressed");
//delay(1);
//toggle the state of the ifcond
if (ifcondState == HIGH){
digitalWrite(ifcond, LOW);
ifcondState = LOW;
}
else {
digitalWrite(ifcond, HIGH);
ifcondState = HIGH;
}
}
lastState = currentState;
if (ifturn == HIGH && buttonState1 == HIGH) {
digitalWrite(ledPin, HIGH);
}
else ();
digitalWrite(ledPin, LOW);
}
I have Pin 12 (ifcond) And pin 11(ifturn) Jumped together. Pin 12 acts like a button to pin 11, When it goes high pin 11 reads the change.
The code works like this, When ButtonpinA goes high pin 12 goes high and stays high until the next button push. This is the part that doesn't work Pin 11 should read if pin 12 if high or not. If it reads high and Button1 is also high it should turn on ledpin which it does not.
I am sorry for any confusion Thanks
else ();
is rather odd. You also might want to look into debouncing the buttons.