0

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

Gerben
11.3k3 gold badges22 silver badges34 bronze badges
asked Sep 15, 2015 at 12:23
2
  • I can't understand what you want it to do. Also; else (); is rather odd. You also might want to look into debouncing the buttons. Commented Sep 15, 2015 at 12:37
  • I want to be able to press and release Button A and then be able to press button one whenever i want to turn the led on Commented Sep 15, 2015 at 12:41

1 Answer 1

1
if (ifturn == HIGH && buttonState1 == HIGH) {
 digitalWrite(ledPin, HIGH);
}
else ();
digitalWrite(ledPin, LOW);

I think you have made a small mistake here.

What you are saying there is "If both these inputs are high then turn on the LED - otherwise don't do anything", and immediately after that "Turn off the LED".

So the LED is turned on when they are both high, and immediately turned off again, so fast you never see the LED lighting up at all.

I think what you meant to have was:

if (ifturn == HIGH && buttonState1 == HIGH) {
 digitalWrite(ledPin, HIGH);
} else {
 digitalWrite(ledPin, LOW);
}

That way it will turn the LED on if both are HIGH, otherwise it will turn the LED off only if the test for both being high fails.

answered Sep 15, 2015 at 14:15
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.