Two buttons - one output

Almost embarassed to post such a seemingly simple problem. How do you make an output HIGH - i.e turn on a LED and keep it on - by pushing one button, and turn it off with another? Both buttons are momentary.

if statement

for example

if(button1 == pressed) turn on led elseif(button2 == pressed) turn off led

Used the if statement in the following sketch. The led is not coming on. Not sure where the variable, pressed, should be declared.

[quote]

[color=#7E7E7E]/*[/color]
[color=#7E7E7E] [/color]
[color=#7E7E7E] Turns on and off a light emitting diode(LED) connected to digital  [/color]
[color=#7E7E7E] pin 13, when pressing pushbuttons attached to pins 2 and 3. [/color]
[color=#7E7E7E] [/color]
[color=#7E7E7E] The circuit:[/color]
[color=#7E7E7E] * LED attached from pin 13 to ground [/color]
[color=#7E7E7E] * pushbuttons attached to pins 2 and 3 from ground[/color]
[color=#7E7E7E] [/color]
[color=#7E7E7E] Written 1/27/13[/color]
[color=#7E7E7E] */[/color]

[color=#7E7E7E]// constants won't change. They're used here to [/color]
[color=#7E7E7E]// set pin numbers:[/color]
const [color=#CC6600]int[/color] button1 = 2;     [color=#7E7E7E]// the number of the 1st pushbutton pin[/color]
const [color=#CC6600]int[/color] button2 = 3;         [color=#7E7E7E]// the 2nd[/color]
const [color=#CC6600]int[/color] ledPin =  13;      [color=#7E7E7E]// the number of the LED pin[/color]

[color=#7E7E7E]// variables may change:[/color]
[color=#CC6600]int[/color] pressed = 0;

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{
  [color=#7E7E7E]// initialize the LED pin as an output:[/color]
  [color=#CC6600]pinMode[/color](ledPin, [color=#006699]OUTPUT[/color]);      
  [color=#7E7E7E]// initialize the 1st pushbutton pin as an input:[/color]
  [color=#CC6600]pinMode[/color](button1, [color=#006699]INPUT[/color]);   
  [color=#7E7E7E]//turn on internal pull-up resistor[/color]
  [color=#CC6600]digitalWrite[/color] (button1,[color=#006699]HIGH[/color]);
  [color=#7E7E7E]//initialize 2nd pushbutton[/color]
  [color=#CC6600]pinMode[/color](button2, [color=#006699]INPUT[/color]);
  [color=#7E7E7E]//turn on pull-up resistor[/color]
  [color=#CC6600]digitalWrite[/color](button2,[color=#006699]HIGH[/color]);
  
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
{ 
  [color=#CC6600]if[/color] (button1 == pressed)
    [color=#CC6600]digitalWrite[/color] (ledPin,[color=#006699]HIGH[/color]);
  [color=#CC6600]else[/color] [color=#CC6600]if[/color] (button2 == pressed)
    [color=#CC6600]digitalWrite[/color] (ledPin,[color=#006699]LOW[/color]);  
 }  

  
 

[/quote]

Does your code really look like that?

In order to post code on the Arduino forum you need to completely ignore the 'copy for forum' command and just copy the text using 'copy'. You're pasting it into the forum inside CODE tags which is the correct thing to do, you're just using the wrong method to copy the source out of the IDE.

(IMO the 'copy as HTML' and 'copy for forum' commands are abominations which should never have got into the IDE in the first place and should have been chopped out at the first opportunity.)

Thanks for the helpful hint re. copying code to Forum. Here is the code - cleaned up and still not working.

/*

 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing pushbuttons attached to pins 2 and 3. 

 The circuit:
 * LED attached from pin 13 to ground 
 * pushbuttons attached to pins 2 and 3 from ground

 Written 1/27/13
 */

// constants used to 
// set pin numbers:
const int button1 = 2;     // the number of the 1st pushbutton pin
const int button2 = 3;         // the 2nd
const int ledPin =  13;      // the number of the LED pin

// variables may change:
int pressed = 0;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the 1st pushbutton pin as an input:
  pinMode(button1, INPUT);   
  //turn on internal pull-up resistor
  digitalWrite (button1,HIGH);
  //initialize 2nd pushbutton
  pinMode(button2, INPUT);
  //turn on pull-up resistor
  digitalWrite(button2,HIGH);
  
}

void loop()
{ 
  if (button1 == pressed)
    digitalWrite (ledPin,HIGH);
  else if (button2 == pressed)
    digitalWrite (ledPin,LOW);  
 }
  if (button1 == pressed)

button1 contains the pin number that a switch is connected to. It is not the state of that switch. 2 is not 0. Neither is 3.

You need to use digitalRead() somewhere...

Can add the digitalRead as part of the if ( ) statement

  if (digitalRead(button1) == pressed)
    digitalWrite (ledPin,HIGH);
  else if (digitalRead(button2) == pressed)

Thanks guys. We are getting closer! Now button1 turns on LED, but the led does not stay on. The goal was to make the led stay on after one momentary button push. And use the second button to turn it off.

/*

 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing pushbuttons attached to pins 2 and 3. 

 The circuit:
 * LED attached from pin 13 to ground 
 * pushbuttons attached to pins 2 and 3 from ground

 Written 1/27/13
 */

// constants used to 
// set pin numbers:
const int button1 = 2;     // the number of the 1st pushbutton pin
const int button2 = 3;         // the 2nd
const int ledPin =  13;      // the number of the LED pin

// variables may change:
int pressed = 0;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the 1st pushbutton pin as an input:
  pinMode(button1, INPUT);   
  //turn on internal pull-up resistor
  digitalWrite (button1,HIGH);
  //initialize 2nd pushbutton
  pinMode(button2, INPUT);
  //turn on pull-up resistor
  digitalWrite(button2,HIGH);
  
}

void loop()
{ 
  if (digitalRead (button1) == pressed)
    digitalWrite (ledPin,HIGH);
  else if (digitalRead (button2) == pressed)
    digitalWrite (ledPin,LOW);  
 }

I can't see any issue with the code. It might be worth double checking the button's connections and that it's properly grounded.

Ah - one way is to use a flag to indicate the on/off state. When one button is pushed the flag is allowed to change. Something like:

if (button1 is pressed and flag == high){
flag = low;
digitalWrite(ledPin, flag);
}
if (button2 is pressed and flag == low){
flag = high;
digitalWrite(ledPin, flag);
}

Still not there. Checked circuit and grounding and put a flag into the code. Now, button 2 turns LED on momentarily and nothing with button 1.

/*

 Turns on a light emitting diode(LED) connected to digital  
 pin 13, when pressing one pushbutton attached to pin 2, 
 and off again when pressing a second button, attached to pin 3. 

 The circuit:
 * LED attached from pin 13 to ground 
 * pushbuttons attached to pins 2 and 3 from ground

 Written 1/27/13
 */

// constants used to 
// set pin numbers:
const int button1 = 2;     // the number of the 1st pushbutton pin
const int button2 = 3;         // the 2nd
const int ledPin =  13;      // the number of the LED pin

// variables may change:
int flag = HIGH;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the 1st pushbutton pin as an input:
  pinMode(button1, INPUT);   
  //turn on internal pull-up resistor
  digitalWrite (button1,HIGH);
  //initialize 2nd pushbutton
  pinMode(button2, INPUT);
  //turn on pull-up resistor
  digitalWrite(button2,HIGH);
  
}

void loop()
{ 
  if (digitalRead (button1) && flag == HIGH)
    {
      flag = LOW;
      digitalWrite (ledPin,flag);
    }
  if (digitalRead (button2) && flag == LOW)
    {
      flag = HIGH;
      digitalWrite (ledPin,flag);
    }   
 }

Serial.begin() in setup() and Serial.print()s in loop() would tell you (and us) a lot.

why do you need to have the argument like that?

why not just

if (Switch1) flag=HIGH;
if (switch2) flag=LOW;
digitalWrite(Led,flag);

this program however assume that you use an external pull down resistor to both switches and you have debounce both the switch to make it work reliably.

ash901226: why do you need to have the argument like that?

why not just

if (Switch1) flag=HIGH;
if (switch2) flag=LOW;
digitalWrite(Led,flag);

this program however assume that you use an external pull down resistor to both switches and you have debounce both the switch to make it work reliably.

Actually you don't need to debounce with that code and setup. Only pressing switch2 can reverse the action of switch1, and vice versa...

It looks like Loop is being called from elsewhere in the sketch, as I don't see an actual for/while/etc. loop in it. Since you declared Flag as a global variable, is it possible that the value of Flag on entering Loop is not what you are expecting?

One approach to fix this might be to eliminate Flag and use a digitalRead(Led) to retrieve the current state in the if statements - e.g.

if(!digitalRead(led) && digitalRead(button1)) { //Led is off, button 1 is pressed, turn on the led
    digitalWrite(led,HIGH);
} else if (digitalRead(led) && digitalRead(button2) { //Led is on, button 2 is pressed, turn off the led
    digitalWrite(led,LOW);
}

why do you need to have the argument like that?

Sometimes my programming gets convoluted, and gets better after I’ve thought about it some more.

CrossRoads sorry i dont mean to be negative on your code... but i do mean why do you need to know the current state of the led... sine if you press switch1 it turn on switch2 it turn off...

So I'm really tired and can't quite think straight (bad news night) but thought I'd take the chance of replying in case it's useful (keeps mind off of other things) so sorry if this doesn't provide anything useful ...

const uint8_t   pinLED          = 13;
const uint8_t   pinON_BUTTON    =  2;
const uint8_t   pinOFF_BUTTON   =  3;

void setup()
{
    pinMode(pinLED, OUTPUT);

    pinMode(pinON_BUTTON,  INPUT);
    pinMode(pinOFF_BUTTON, INPUT);

    digitalWrite(pinLED, LOW);

    // ... possibly incomplete ...
}

void loop()
{
    // ... an interesting thing is we can query an OUTPUT pin for its last written state ...
    // ... so no need for separate flags ...

    // ... second, LOW is considered 'false' and HIGH is not false and thus 'true' ...
    // ... so when querying items 'LOW' is '0' and thus 'false' meaning any non 0 value is 'true' ...
    // ... given that if a pressed button returns HIGH and a unpressed button is thus LOW ...
    
    if ( digitalRead(pinON_BUTTON) && !digitalRead(pinOFF_BUTTON) && !digitalRead(pinLED) )
    {
        digitalWrite(pinLED, HIGH);
    }
    else if ( !digitalRead(pinON_BUTTON) && digitalRead(pinOFF_BUTTON) && digitalRead(pinLED) )
    {
        digitalWrite(pinLED, LOW);
    }
}

@ash901226 , No problem. Here's what I think I think was thinking: If the state of the LED was tracked, one wouldn't need to spend pointless time going thru the steps of digital writes when the desired condition was already set.