loading

Arduino coding to allow separate push buttons to turn led on and off?

I am new to the Arduino and have yet to use one. I understand a little about how to program the Arduino and would like to know more specifically about push buttons. I know that I will most likely have to use "edge detection" to determine when the push button is pressed. I don't want to have a push button turn an led on if pressed and if pressed again turn it off. I want to separate the on and off into two different buttons. When i push the first button it will always turn the LED circuit on even if i press it twice. When i push the second button it will always turn the LED circuit off no matter how many times it gets pressed. Is this possible, and if so how do i code it?


sort by: active | newest | oldest
Here is a slightly modified version of the Button example to do what your wanting to do.
/*
  Button

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


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V

* pushbutton attached to pin 3 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
modified 10 Apr 2012
by mpilchfamily

*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin[] = {2,3};     // the number of the pushbutton pins
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  for(int x=0; x<2; x++)
  {
    pinMode(buttonPin[x], INPUT); 
  }  
}

void loop(){
  // read the state of the pushbutton value:
  for(int x=0; x<2; x++)
  {
    buttonState = digitalRead(buttonPin[x]);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH && buttonPin[x] == 2) {    
      // turn LED on:   
      digitalWrite(ledPin, HIGH); 
    }
    if (buttonState == HIGH && buttonPin[x] == 3) {
      // turn LED off:
      digitalWrite(ledPin, LOW);
    }
  }
}
 
+1

Great code -- keep in mind it works reliably because the code is so simply, its nearly impossible to hit the button and NOT get detected within the timespan of one loop, in fact, pressing the button would probably be detected hundreds of times if not thousands.

If timing or absolute detection is required, then 'edge detection' would come in handy to trigger an interrupt - telling the processor to stop whatever its doing, run the isr (interrupt subroutine) - a procedure that does stuff like 'turn the led on/off'.
It's much more complex, and does the 'same' job in a different way. If your main loop was SUPER processor intensive and you needed to make sure you don't miss a buttonpress (say, during a delay(); ) that's what you would do.
Bülentİ1 year ago

I would like to create something similar;

if button 1 pressed -> loop 1

if button 2 pressed -> loop 2

if none -> wait 10 sec and loop3

is it possible?

tyishistory4 years ago
I am relatively new to Arduino's and haven't had much time in programming, but I have to design a circuit similiar to this one but I have to have one button turn off the LED by simply pressing it once and the other push button turn the LED on by pressing it once. Can anyone help me out?
buteman5 years ago
It would be advisable to have a 10K resistor attached to pin 3 from ground as well.
iceng5 years ago
You don't need edge detection.  See the flow chart below;
  1. Set in pin 7, in pin 8, out pin 3  // two push buttons one LED
  2. While LED pin3 off = Low  //  test led status
  3. IF in button pin 7 low  //  is ON button depressed
  4. Then set LED pin 3 High //  is ON button depressed
  5. Else repeat step 3.
  6. While LED pin 3 ON= High  //  test led status
  7. IF in button pin 8 low  //  is Off button depressed
  8. Then set LED pin 3 Low  //  is Off button depressed
  9. Else repeat step 7.
  10. Repeat step 2.
A
mcguyverzboss (author)  iceng5 years ago
I love all the help from the answers. I like your answer in that it makes logical sense but i'm not familiar with how i would code it. I selected mpilichfamily's answer as the best as i copied and pasted the code and it works perfect for what I needed to do. I press the first button and it turns led on. I can keep pressing it and it still is on. I press the second button and it turns the led off. thank you for your response.
Easy.
Code something like

If button1=1 then lamp=1;
if button2=1 then lamp=0
while (button1=1)!!(button2=1) do;