Pages: [1] 2   Go Down
Author Topic: Two buttons - one output  (Read 1380 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

SE USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if statement

for example

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


Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46017
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does your code really look like that?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11148
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
/*
 
 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); 
 } 

 
 
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46017
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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...
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23652
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 
 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); 
 } 

 
 
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23652
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (button1 is pressed and flag == high){
flag = low;
digitalWrite(ledPin, flag);
}
if (button2 is pressed and flag == low){
flag = high;
digitalWrite(ledPin, flag);
}
Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 
 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);
    }   
 } 

 
 
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46017
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why do you need to have the argument like that?

why not just
Code:
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.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why do you need to have the argument like that?

why not just
Code:
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...
Logged

Pages: [1] 2   Go Up
Jump to: