Go Down

Topic: Two buttons - one output (Read 2118 times) previous topic - next topic

Obereon

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

   

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

ash901226

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

lloyddean

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: [Select]

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);
   }
}


CrossRoads

@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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

telefondreng

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

telefondreng

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. =(
Going back to test all the code previously submitted.

johncc


Going back to test all the code previously submitted.


My vote is for reply #1 and #7 :)

telefondreng

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

Code: [Select]

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


 


Go Up