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

It looks like Loop is being called from elsewhere in the sketch, as I don't see an actual for/while/etc. loop in it. Since you declared Flag as a global variable, is it possible that the value of Flag on entering Loop is not what you are expecting?

One approach to fix this might be to eliminate Flag and use a digitalRead(Led) to retrieve the current state in the if statements - e.g.
Code:
if(!digitalRead(led) && digitalRead(button1)) { //Led is off, button 1 is pressed, turn on the led
    digitalWrite(led,HIGH);
} else if (digitalRead(led) && digitalRead(button2) { //Led is on, button 2 is pressed, turn off the led
    digitalWrite(led,LOW);
}
   
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26956
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

Quote
why do you need to have the argument like that?
Sometimes my programming gets convoluted, and gets better after I've thought about it some more.
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.

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

CrossRoads sorry i dont mean to be negative on your code... but i do mean why do you need to know the current state of the led... sine if you press switch1 it turn on switch2 it turn off...
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I'm really tired and can't quite think straight (bad news night) but thought I'd take the chance of replying in case it's useful (keeps mind off of other things) so sorry if this doesn't provide anything useful ...

Code:
const uint8_t   pinLED          = 13;
const uint8_t   pinON_BUTTON    =  2;
const uint8_t   pinOFF_BUTTON   =  3;

void setup()
{
    pinMode(pinLED, OUTPUT);

    pinMode(pinON_BUTTON,  INPUT);
    pinMode(pinOFF_BUTTON, INPUT);

    digitalWrite(pinLED, LOW);

    // ... possibly incomplete ...
}

void loop()
{
    // ... an interesting thing is we can query an OUTPUT pin for its last written state ...
    // ... so no need for separate flags ...

    // ... second, LOW is considered 'false' and HIGH is not false and thus 'true' ...
    // ... so when querying items 'LOW' is '0' and thus 'false' meaning any non 0 value is 'true' ...
    // ... given that if a pressed button returns HIGH and a unpressed button is thus LOW ...
    
    if ( digitalRead(pinON_BUTTON) && !digitalRead(pinOFF_BUTTON) && !digitalRead(pinLED) )
    {
        digitalWrite(pinLED, HIGH);
    }
    else if ( !digitalRead(pinON_BUTTON) && digitalRead(pinOFF_BUTTON) && digitalRead(pinLED) )
    {
        digitalWrite(pinLED, LOW);
    }
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26956
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

@ash901226 ,
No problem. Here's what I think I think was thinking:
If the state of the LED was tracked, one wouldn't need to spend pointless time going thru the steps of digital writes when the desired condition was already set.
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

Sorry about the long absence from this project  (dental implant && medications == brain, OFF) but thanks for all the suggestions and debate.

The circuit and components  -  simple as it is  -  has been checked forwards and backwards.  The two if statements work by themselves, turning the Led on or off.  I.e. the innards of the Arduino are not at fault.  Yet, can not get them to work together.
Went back to external pull-down resistors.  Agree that debouncing should not be needed here (the two "push buttons" are in fact reed switches, reliably activated by a train to set or clear a red signal).

The post by lloyddean pretty much summarizes previous ideas, including reading the output pin as an argument in the on/off function. (is const uint8_t the same as const int; the former won't work in my version).
But OFF_BUTTON (= button2) does not work in this sketch.  Not sure what serial.print could tell us here that can not be seen with the Led on pin 13.

Still open to ideas
Logged

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

Apologies are in order!  One of my test buttons from the junkbox was a normally-on type (didn't even know they exist), keeping pin 3 high instead of low. smiley-cry
Going back to test all the code previously submitted.
Logged

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

Going back to test all the code previously submitted.

My vote is for reply #1 and #7 smiley
Logged

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

Problem solved!   Using hardware that actually works  -  plus code recommended by the Forum.  Thanks again for helping out.

Code:
/*
  TWO BUTTONS  -  ONE OUTPUT
 
 Turns on a light emitting diode(LED) when pressing one pushbutton;
 and off again when pressing a second button. LED stays on in between. 
 
 The circuit:
 * LED attached from pin 13 to ground
 * pushbuttons attached to pins 2 and 3 with 10k pull-down resistors to ground.
 
 Written 2/1/13 by
 Telefondreng.
 */

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



void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the 1st pushbutton pin as an input:
  pinMode(button1, INPUT);   
  //initialize 2nd pushbutton
  pinMode(button2, INPUT);
 
  digitalWrite (ledPin,LOW);
 
}

void loop()
{
  if ( digitalRead(button1))
    {
      digitalWrite (ledPin,HIGH);
    } 
  if (digitalRead(button2))
    {
      digitalWrite(ledPin,LOW);
    } 
 } 

 
 
Logged

Pages: 1 [2]   Go Up
Jump to: