Go Down

Topic: Two buttons - one output (Read 2 times) previous topic - next topic

telefondreng

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.

Osgeld

if statement

for example

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

telefondreng

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

Code: [Select]
[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]

PaulS

Does your code really look like that?

PeterH

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.)
I only provide help via the forum - please do not contact me for private consultancy.

telefondreng

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

Code: [Select]

/*

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); 


 


PaulS

Code: [Select]
  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...

CrossRoads

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

Code: [Select]

  if (digitalRead(button1) == pressed)
    digitalWrite (ledPin,HIGH);
  else if (digitalRead(button2) == pressed)
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

telefondreng

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.

Code: [Select]

/*

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); 


 


Lubby

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. 

CrossRoads

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:
Code: [Select]

if (button1 is pressed and flag == high){
flag = low;
digitalWrite(ledPin, flag);
}
if (button2 is pressed and flag == low){
flag = high;
digitalWrite(ledPin, flag);
}
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

telefondreng

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.

Code: [Select]

/*

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);
    }   


 


PaulS

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

ash901226

why do you need to have the argument like that?

why not just
Code: [Select]

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.

johncc


why do you need to have the argument like that?

why not just
Code: [Select]

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...

Go Up