Two Buttons one led on/off

const int  buttonPin1 = 23;    // the pin that the pushbutton is attached to
const int ledPin1 = 2;       // the pin that the LED is attached to
const int  buttonPin2 = 24;    // the pin that the pushbutton is attached to

void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin2,INPUT);
}


void loop() {
  // read the pushbutton input pin:
  
   if ((digitalRead(buttonPin1) == HIGH) && (digitalRead(buttonPin2) == LOW)) {
   digitalWrite(ledPin1,HIGH);
   }

   if ((digitalRead(buttonPin1) == LOW)&&(digitalRead(buttonPin2)) == HIGH) {
   digitalWrite(ledPin1,LOW);
   }
}

I am trying to on/off one led with 2 seperate buttons but when i turn it on i can not turn the led off with second button. I couldn't understand where i am wrong!!!

I am trying to on/off one led with 2 seperate buttons

Switches work much better. Explaining how they are wired is ESSENTIAL.

I think you may also have a logic problem with your IF statements.

Your IF statements each have two tests. Every test can have two options so two tests can have 4 possibilities.

You have only 2 IF statements - are you sure you have the correct 2 out of the 4?

...R

Momentary button switch code for servos, but might be modified to work for the LEDs.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}
if ((digitalRead(buttonPin1) == HIGH) && (digitalRead(buttonPin2) == LOW)) {
   digitalWrite(ledPin1,HIGH);
   }

   if ((digitalRead(buttonPin1) == LOW)&&(digitalRead(buttonPin2)) == HIGH) {
   digitalWrite(ledPin1,LOW);
   }

Check where your parentheses are in the second if statement.

SeMiHGeeK: ``` const int  buttonPin1 = 23;    // the pin that the pushbutton is attached to const int ledPin1 = 2;      // the pin that the LED is attached to const int  buttonPin2 = 24;    // the pin that the pushbutton is attached to

void setup() {   pinMode(buttonPin1, INPUT);   pinMode(ledPin1, OUTPUT);   pinMode(buttonPin2,INPUT); }

void loop() {   // read the pushbutton input pin:     if ((digitalRead(buttonPin1) == HIGH) && (digitalRead(buttonPin2) == LOW)) {   digitalWrite(ledPin1,HIGH);   }

  if ((digitalRead(buttonPin1) == LOW)&&(digitalRead(buttonPin2)) == HIGH) {   digitalWrite(ledPin1,LOW);   } }




I am trying to on/off one led with 2 seperate buttons but when i turn it on i can not turn the led off with second button. 
I couldn't understand where i am wrong!!!

When you deal with booleans you must always make sure of which gets evaluated. In any logical AND the first statement gets evaluated first. If it is TRUE then the second statement gets evaluated.

In your second if statement your parenthesis placement may be why you are not getting the results you are expecting. The AND ('&&') is being applied to

(digitalRead(buttonPin1) == LOW)&&(digitalRead(buttonPin2))

Now this statement translates to TRUE or FALSE (Boolean) being logically ANDed to HIGH or LOW (INT). That result will be compared to a HIGH (an implied INT) value. The result

I would try this:

void loop() {
  // read the pushbutton input pin:
  boolean pin1State, pin2State;

   pin1State = (digitalRead(buttonPin1) == HIGH);
   pin2State = (digitalRead(buttonPin2) == HIGH);
  
   if (pin1State && !pin2State) {
      digitalWrite(ledPin1,HIGH);
   }

   if (pin2State && !pin1State) {
      digitalWrite(ledPin1,LOW);
   }
}

You can add some button debounce logic if you like. This will read both buttons and check their states. If any changes to the buttons occur midway between checks it will be rechecked on the next iteration of the loop NOT on each if statement.

Hope this helps!

Don

I’d look for a state change on the pins, along with a check of the other pin, although that may not even matter…

const int  buttonPin1 = 23;    // the pin that the pushbutton is attached to
const int ledPin1 = 2;       // the pin that the LED is attached to
const int  buttonPin2 = 24;    // the pin that the pushbutton is attached to
int lastPin1State,lastPin2State;

void setup() 
{
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(buttonPin2,INPUT);
}


void loop() 
{
  // read the pushbutton input pin:
  int pin1State = digitalRead(buttonPin1);
  int pin2State = digitalRead(buttonPin2);
  if (pin1State == HIGH && lastPin1State == LOW && pin2State == LOW ) 
  {
    digitalWrite(ledPin1,HIGH);
  }

  if (pin2State == HIGH && lastPin2State == LOW && pin1State == LOW) 
  {
    digitalWrite(ledPin1,LOW);
  }
  lastPin1State = pin1State;
  lastPin2State = pin2State;
  delay(10);  //debounce
}

/you just considered 2 conditions but there are 4 of them/ const int buttonPin1 = 23; // the pin that the pushbutton is attached to const int ledPin1 = 2; // the pin that the LED is attached to const int buttonPin2 = 24; // the pin that the pushbutton is attached to

void setup() { pinMode(buttonPin1, INPUT); pinMode(ledPin1, OUTPUT); pinMode(buttonPin2,INPUT); }

void loop() { // read the pushbutton input pin:

if ((digitalRead(buttonPin1) == HIGH) && (digitalRead(buttonPin2) == LOW)) { digitalWrite(ledPin1,HIGH); }

if ((digitalRead(buttonPin1) == LOW)&&(digitalRead(buttonPin2)) == HIGH) { digitalWrite(ledPin1,HIGH); } if ((digitalRead(buttonPin1) == HIGH) && (digitalRead(buttonPin2) == HIGH)) { digitalWrite(ledPin1,LOW); }

if ((digitalRead(buttonPin1) == LOW)&&(digitalRead(buttonPin2)) == LOW) { digitalWrite(ledPin1,LOW); } }