Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: January 27, 2013, 01:32:33 am » |
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
Faraday Member
Karma: 33
Posts: 3626
@ssh0le
|
 |
« Reply #1 on: January 27, 2013, 01:40:21 am » |
if statement
for example
if(button1 == pressed) turn on led elseif(button2 == pressed) turn off led
|
|
|
|
|
Logged
|
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #2 on: January 28, 2013, 01:05:50 pm » |
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]
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35581
Seattle, WA USA
|
 |
« Reply #3 on: January 28, 2013, 01:10:28 pm » |
Does your code really look like that?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6400
-
|
 |
« Reply #4 on: January 28, 2013, 02:47:38 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #5 on: January 28, 2013, 03:06:37 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35581
Seattle, WA USA
|
 |
« Reply #6 on: January 28, 2013, 03:17:18 pm » |
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
Brattain Member
Karma: 249
Posts: 16572
Available for Design & Build services
|
 |
« Reply #7 on: January 28, 2013, 03:26:23 pm » |
Can add the digitalRead as part of the if ( ) statement if (digitalRead(button1) == pressed) digitalWrite (ledPin,HIGH); else if (digitalRead(button2) == pressed)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #8 on: January 28, 2013, 11:26:02 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
Arduino rocks
|
 |
« Reply #9 on: January 28, 2013, 11:32:46 pm » |
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
Brattain Member
Karma: 249
Posts: 16572
Available for Design & Build services
|
 |
« Reply #10 on: January 28, 2013, 11:50:55 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #11 on: January 29, 2013, 04:09:52 pm » |
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); } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35581
Seattle, WA USA
|
 |
« Reply #12 on: January 29, 2013, 04:16:54 pm » |
Serial.begin() in setup() and Serial.print()s in loop() would tell you (and us) a lot.
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #13 on: January 30, 2013, 06:35:55 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #14 on: January 30, 2013, 07:34:21 am » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
|