Buttons and LED

Hello, 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

liljoey112: I have Pin 12 And pin 11 Jumped together.

Why? This is unusual.

So you are saying that in order to turn on the LED, they need to press and hold the first button and then press the second button? IS that what you mean by 'press the second button at a different time'? Or do you mean that they need to press and release the first button and then press the second button?

How does ifcond turn off again? Oh - by the looks of your code, you have to do the same thing again.

I'm finding your naming confusing, BTW. button 1 goes into a variable named state1, but button a goes into a variable named state. Outputs are named 'LED' and inputs 'button', but the two pins named 'ifXXX', one is an input and one is an output. You store the state in variables, some of them are boolean, some of them are int.

To toggle the state of the ifcond (what is an ifcond? Some kind of elephant?), why not:

  ledState = !ledState;
  digitalWrite(ifcond, ledState);

Your final if - the one that turns ledPin HIGH - checks if buttonPin1 is HIGH. You probably mean buttonState1, and that's probably the thing causing your bug.

I find it weird that ledState controls the behaviour of ifcond, and that ifturn controls the behaviour of ledPin. Isn't that crossways? Maybe it isn't - I don't know exactly what behaviour you are trying to produce.

I can't help noticing that there is nothing in your code to turn ledPin LOW again. Once it goes HIGH, it stays that way. Is that what you intended?

  currentState = digitalRead(buttonPinA);
  buttonState1 = digitalRead(buttonPin1);

Consistency is good. The pin names should reflect the purpose of the pin. It would appear that there is a button connected to the pin. A switch would be better. But, A and 1 don't mean squat.

Storing the states in currentState and buttonState1 is not consistent.

The first button needs to stay high

You can't make a switch stay HIGH. You can ignore changes to the state of the pin that the switch is connected to.

int buttonStateIf = 0;         // variable for reading the If Condition status

I can't imagine what an If button is...

I am sorry for the confusion, im not used to having other people read my code so i cleaned it up alttle and added a new description in the first post. Ill answer some questions here too. The ifcond controls "If the buttonA is High then turn HIGH" I know its a confusing name but i couldn't think of a better one. The ifturn controls "If ifcond is high then i am high also" ButtonA doesnt not need to be held down, it needs to be pressed and stay high until repressed.

ButtonA doesnt not need to be held down, it needs to be pressed and stay high until repressed.

You need to look at the state change detection example, and count each time the switch becomes pressed. When the count is odd, do one thing. When it is even, do something else. Switch pins are input. That means that the switch controls the state of the pin, not the code. You can not make an input stay HIGH except by holding the switch down.

Yes but correct me if i am wrong, When buttonA is pressed the output turns on 'ifcond' and that triggers the next stage not the actual button.

When buttonA is pressed the output turns on 'ifcond' and that triggers the next stage not the actual button.

That may be true, but it has nothing to do with your statement that you want to the input to remain HIGH until the switch is pressed again.

I think we have a misunderstanding because im not being clear. 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

liljoey112: I think we have a misunderstanding because im not being clear. 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

Can you develop that more?

be able to press button one whenever i want to turn the led on

Once? Many times?

If I track the status of the button A and it is pressed (state 1) and released (state 2) then does that ever change again?

Is this a thing that can return to the starting state and be done again and again? If so, describe the whole cycle.

Without linking etc. You only need pull down resistors on the buttons (but I hope you already have them) or use the internal pull ups (and enable them in code). I use the Bounce2 library for the buttons because that makes is soooooooooooo easy :slight_smile:

#include <Bounce2.h>


// Boolean logic for Button Switch
bool buttonApressed = false;


// set pin numbers:
const int ButtonPinA = 3;     // the number of the pushbutton pin
const int ButtonPinB = 4;     // the number of the pushbutton pin
const int LedPin =  13;      // the number of the LED pin


//Button objects 
Bounce buttonA;
Bounce buttonB;

void setup() {
  //initialize Serial connection 
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(LedPin, OUTPUT);
  
  //Attach the buttonPins to the objects
  buttonA.attach(ButtonPinA, INPUT);
  buttonB.attach(ButtonPinB, INPUT);
}

void loop() {
  //Check for button A
  buttonA.update();
  //If pressed, save that
  if(buttonA.rose()){
    buttonApressed = true;
    Serial.println(F("Button A pressed, ready for B"));
  }
  
  //Check for button B
  buttonB.update();
  //if button A was pressed befor and now button B became pressed
  if(buttonApressed && buttonB.rose()){
    //Toggle the LED
    digitalWrite(LedPin, !digitalRead(LedPin));
    //reset the state
    buttonApressed = false;
  }
}

GoForSmoke: Can you develop that more?

Once? Many times?

If I track the status of the button A and it is pressed (state 1) and released (state 2) then does that ever change again?

Is this a thing that can return to the starting state and be done again and again? If so, describe the whole cycle.

Button A needs to be pressed once time. Button A needs to be pressed once then Button 1 needs to be pressed to turn the led on

septillion:
Without linking etc. You only need pull down resistors on the buttons (but I hope you already have them) or use the internal pull ups (and enable them in code). I use the Bounce2 library for the buttons because that makes is soooooooooooo easy :slight_smile:

#include <Bounce2.h>

// Boolean logic for Button Switch
bool buttonApressed = false;

// set pin numbers:
const int ButtonPinA = 3;    // the number of the pushbutton pin
const int ButtonPinB = 4;    // the number of the pushbutton pin
const int LedPin =  13;      // the number of the LED pin

//Button objects
Bounce buttonA;
Bounce buttonB;

void setup() {
  //initialize Serial connection
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(LedPin, OUTPUT);
 
  //Attach the buttonPins to the objects
  buttonA.attach(ButtonPinA, INPUT);
  buttonB.attach(ButtonPinB, INPUT);
}

void loop() {
  //Check for button A
  buttonA.update();
  //If pressed, save that
  if(buttonA.rose()){
    buttonApressed = true;
    Serial.println(F(“Button A pressed, ready for B”));
  }
 
  //Check for button B
  buttonB.update();
  //if button A was pressed befor and now button B became pressed
  if(buttonApressed && buttonB.rose()){
    //Toggle the LED
    digitalWrite(LedPin, !digitalRead(LedPin));
    //reset the state
    buttonApressed = false;
  }
}

I am using external pulldowns. And Thank you so much for this help I am going to try this code when i get home thank you :slight_smile:

You can use the AVR chip internal pullups instead of external resistors.

pinMode( buttonPin, INPUT_PULLUP ); // pin to button/switch to ground is safe, the pullup is 20K to 50K

Then when you read that pin, LOW means pressed and HIGH means not.

Button A needs to be pressed once time. Button A needs to be pressed once then Button 1 needs to be pressed to turn the led on

And that's it? The led just stays on? Something like this can do that, but is almost useless for more.

byte buttonA = 0;

// set pin numbers:
const byte ButtonPinA = 3;     // the number of the pushbutton pin
const byte ButtonPinB = 4;     // the number of the pushbutton pin
const byte LedPin =  13;        // the number of the LED pin


void setup() 
{
  Serial.begin(9600);

  pinMode(LedPin, OUTPUT); // default is LOW

  pinMode(ButtonPinA, INPUT_PULLUP); // wire pin to button, button to ground 
  pinMode(ButtonPinB, INPUT_PULLUP); // I just use a jumper to test, ground on the USB port.
}

void loop() 
{
  if ( buttonA == 0 )
  {
    if ( digital.read( ButtonPinA ) == 0 )
    {
      buttonA = 1;
      Serial.println(F("Button A pressed, ready for B"));
    }
  }

  if ( buttonA == 1 )
  {
    if ( digital.read( ButtonPinB ) == 0 )
    {
      digitalWrite( LedPin, HIGH );
      Serial.println(F("Button B pressed"));
      while ( 1 );
    }
  }
}

I think you want more and if you can work that out into a series of steps then make those into comments (the IDE Edit->comment/uncomment tool makes it easy), those comments can help you organize and write the code.

Yeay, you can use the internal pull ups next time. But it's fine to use a external pull up or pull down if you want to. More work but besides that it will work as fine.

Thanks you guys :) I did it, I used your code as a template and expanded it for my needs. (one A button, 1-4 buttons and 4 Leds) It works like a charm and i learned alot :)

septillion: Yeay, you can use the internal pull ups next time. But it's fine to use a external pull up or pull down if you want to. More work but besides that it will work as fine.

Absolutely true!

Using pullups lets me test button and led code without any breadboard or solder since my buttons are jumpers. That trick alone saves me minutes when trying simple things out.

Thanks you guys :) I did it, I used your code as a template and expanded it for my needs. (one A button, 1-4 buttons and 4 Leds) It works like a charm and i learned alot :)

Hey liljoe112, that's the spirit that will take you far! Karma to you for being a trooper!