Go Down

Topic: [RESOLVED] 2 buttons control for LED (Read 42 times) previous topic - next topic

SJMaybury

Jan 26, 2016, 03:19 pm Last Edit: Jan 26, 2016, 04:42 pm by SJMaybury
Hi,
So I'm connecting two buttons to an LED so that one turns it ON and the other turns it OFF. It felt simple at the time... but:

Code: [Select]

// pin variables
int btnPower = 0;
int btnONread = 1;
int btnOFFread = 2;
int led = 13;

// control variables
int ONval = LOW;
int pONval = HIGH;
int OFFval = LOW;
int pOFFval = HIGH;
int state = LOW;
int Pstate = HIGH;   // Previous state of LED

void setup(){
  // declare flow through pins
  pinMode(btnPower, OUTPUT);
  pinMode(btnONread, INPUT);
  pinMode(btnOFFread, INPUT);
  pinMode(led, OUTPUT);

  digitalWrite(btnPower, HIGH); // Provides power to the buttons
}

void loop(){
  ONval = digitalRead(btnONread);
  OFFval = digitalRead(btnOFFread);  // Stores current state of button

// On scenario
  if (ONval == HIGH && pONval == LOW && OFFval == LOW){
    if (state == LOW){state = HIGH;}  //Turns OFF state ON
  }
// Off scenario 
  if (ONval == LOW && OFFval == HIGH && pOFFval == LOW){
    if (state == HIGH){state = LOW;} //Turns ON state OFF
  }
// neither
  else{}

// update led status
digitalWrite(led, state);

}






I'd appreciate some tips on what I'm meant to do and how to improve/ shorten this code. Also I'd be grateful if someone could point me towards a simple and comprehensive button latch tutorial I'd be very grateful.

Thanks for the help!!

Greg37

#1
Jan 26, 2016, 03:54 pm Last Edit: Jan 26, 2016, 04:00 pm by Greg37
Try this:

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


And as for wiring the buttons, look at this:


SJMaybury

Hi, that code doesn't seem to be working, I notice you haven't got any latch? Both the ON and OFF latch the state, however the code leaves the LED on constantly.

6v6gt

Here is another variant:
The buttons should be wired as in the sketch above so these are LOW when pressed:


Code: [Select]
// pin variables
int btnONread = 1; 
int btnOFFread = 2; 
int led = 13;

// control variables
int state = LOW;

void setup() {
  // declare flow through pins
  // pinMode(btnPower, OUTPUT);
  pinMode(btnONread, INPUT_PULLUP);
  pinMode(btnOFFread, INPUT_PULLUP);
  pinMode(led, OUTPUT);
}

void loop() {

  if ( ! digitalRead(btnONread) || ! digitalRead(btnOFFread)   ) {
    // button pressed
    if ( ! digitalRead(btnONread) && ! state ) state = HIGH ;
    else if ( ! digitalRead(btnOFFread) && state ) state = LOW ;
    else {}
    digitalWrite(led, state);
  }
}

septillion

For the wiring, just drop the switch resistors (not the led resistor ;) ) and use pinMode(pin, INPUT_PULLUP).

And for the code:
Code: [Select]

buttonPin[x] == 2

Is a very very weird thing to do... Now you can't go and change the pins on the top... If you want the for thing, check it against x.

Also, buttonState as global is a bit overkill ;)

Try:
Code: [Select]

const byte ButtonPins[] = {2,3};     // the number of the pushbutton pins
const byte LedPin =  13;      // the number of the LED pin

void setup() {
  // initialize the LED pin as an output:
  pinMode(LedPin, OUTPUT);  
  
  // initialize the pushbutton pin as an input:
  for(int x=0; x < sizeof(ButtonPins); x++)
  {
    pinMode(ButtonPins[x], INPUT_PULLUP);
  }  
}

void loop(){
  if(!digitalRead(ButtonPins[0])){
    digitalWrite(LedPin, LOW);
  }
  else if(!digitalRead(ButtonPins[1])){
    digitalWrite(LedPin, HIGH);
  }
}



Use fricking code tags!!!!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy