Go Down

Topic: using a button to drive a led: if on->off if off->on (Read 3109 times) previous topic - next topic

sotomaior

Hi huys, im getting some troubles in using a simple code by means of the bounce library:

Code: [Select]
#include <Bounce.h>

//setup constants
const unsigned long antiBounce = 300;
//lights
const byte pinRelayLight0 = 13;
const byte pinSwitchLight0 = 10;
boolean lightStatus0 = false;
Bounce light0 = Bounce(pinSwitchLight0, antiBounce);

void setup()
{
  //LIGHT PINS
  pinMode(pinRelayLight0, OUTPUT);
  pinMode(pinSwitchLight0, INPUT);
}
void loop()
{
  //read all switches
  light0.update ();
  if (light0.read()) {if (lightStatus0) {lightStatus0 = false;} else {lightStatus0 = true;}}
  //actuate all lights
  if (lightStatus0) {digitalWrite(pinRelayLight0, HIGH);} else {digitalWrite(pinRelayLight0, LOW);}
}


this code is supposed to switch on the light when it is off and viceversa, but it seem not to be properly working:
if i press the buttom sometimes it works, sometimes it does not, any suggestion?

P.S. its for the purpose of a domotic system, so that i cant use any delay command to avoid bouncing, as i did in the previous versions  :~

AWOL

You could try to see what your code is actually doing by putting in some serial debug prints.

sotomaior

i did it:
the variable lightStatus0 is FALSE while i do nothing and goes from true to false and viceversa while i hold the button, how can i solve this?

i also tried:
Code: [Select]
if ((light0.duration() < antiBounce) && (light0.duration() > 0)) {if (lightStatus0) {lightStatus0 = false;} else {lightStatus0 = true;}}

but it also seems not to work... i miss my easy delay command... =(

ookid

Just curious, have you looked at the example code for the Bounce Library? There is some code in there that does exactly what you are looking for. Also, just as a rule of thumb, never use the delay command unless you know exactly what you are doing. Microcontrollers run so quickly that it will spend most of its time stuck in the delay loop, ignoring everything else, until it exits the loop. This tends to wreak havoc on all sorts of things. They are useful for the simple blink an led project but for pretty much everything else it is better to use the millis() function and just check to see if the delay time has passed since it was started.

UKHeliBob

Whilst you are amending the code can I suggest that you lay it out so that it is more readable ?
Code: [Select]
void loop()
{
  //read all switches
  light0.update ();
  if (light0.read())
  {
    if (lightStatus0)
    {
      lightStatus0 = false;
    }
    else
    {
      lightStatus0 = true;
    }
  }
  //actuate all lights
  if (lightStatus0)
  {
    digitalWrite(pinRelayLight0, HIGH);
  }
  else
  {
    digitalWrite(pinRelayLight0, LOW);
  }
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GoForSmoke

Code: [Select]

    if (lightStatus0)
    {
      lightStatus0 = false;
    }
    else
    {
      lightStatus0 = true;
    }


Might as well learn about XOR while you're at it.

XOR takes 2 inputs and returns true when they are different.
XOR in Arduino is ^. That is not exponent or power of, it is logical xor.
 
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

Code: [Select]

    lightStatus0 ^= 1;  // same as lightStatus0 = lightStatus0 ^ 1;


And XOR works for -all- the bits in the variables though I have trouble with 64-bit ints.
That means you can set many bits to match many switches and XOR the bunch at once.

Code: [Select]
// XOR

void setup( void )
{
  Serial.begin( 9600 );
  byte tst8 = 0x33;
  Serial.println(( tst8 ^ 0xFF), HEX );
  unsigned int tst16 = 0x5555;
  Serial.println(( tst16 ^ 0xFFFF), HEX );
  unsigned long tst32 = 0x0F0F0F0F;
  Serial.println(( tst32 ^ 0xFFFFFFFF), HEX );
}

void loop( void )
{
}


The other side of writing your own bugs is that your code is only as cool as you make it, too.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

sotomaior


Just curious, have you looked at the example code for the Bounce Library?

yep, i found a led example but it hold the led on only while the button is pressed, like the led is connected in series with the switch, maybe i didnt saw the one you mean


Might as well learn about XOR while you're at it.


i pretty know xor (degree in electronic eng   :smiley-eek-blue:) but i forgot that it was available in arduino-C  :smiley-roll-blue:

the solution i found is the following:
Code: [Select]
light0.update();
  if (light0.risingEdge()) {if (lightStatus0) {lightStatus0 = false;} else {lightStatus0 = true;}}
  if (lightStatus0) {digitalWrite(pinRelayLight0, HIGH);} else {digitalWrite(pinRelayLight0, LOW);}


i know i could erase my 1-bit-variable at this point but i need to keep it for other purposes of my small domotic system.

OFC as soon as i will finish it i will post the wole code, if anybody is intrested  :)

PaulS

Code: [Select]
if (lightStatus0) {lightStatus0 = false;} else {lightStatus0 = true;
That's a lot of code to do:
Code: [Select]
lightStateus0 = !lightStatus0;

Code: [Select]
  if (lightStatus0) {digitalWrite(pinRelayLight0, HIGH);} else {digitalWrite(pinRelayLight0, LOW);}
That's a lot of code to do:
Code: [Select]
digitalWrite(pinRelayLight0, lightStatus0);

lloyddean

Simplify -

Code: [Select]

digitalWrite(pinRelayLight0, !digitalRead(pinRelayLight0));


Tom Carpenter


Simplify -

Code: [Select]

digitalWrite(pinRelayLight0, !digitalRead(pinRelayLight0));




I think that should be:
Code: [Select]
digitalWrite(pinRelayLight0, !light0.read())

Yours would just toggle the LED every time with no relationship to the button.
~Tom~

lloyddean

Yes, I've got to catch my edit UNDO's better or stop posting.

zoomkat

Some simple button toggle test code.

Code: [Select]

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

GoForSmoke

Just a bit simpler, there's no need to test what you don't need to know.

Code: [Select]
  if (press == LOW)
  {
      digitalWrite(13, toggle);   
      toggle = !toggle;
  }
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

sotomaior

Code: [Select]
light0.update();
  if (light0.risingEdge()) {lightStatus0 = !lightStatus0;}
  digitalWrite(pinRelayLight0, lightStatus0);


the shorter i could....

the problem is that i am used to write fpga codes, and it is often useful to desctibe a logic component to use long codes, in order for the synthetizer to understend well what you mean, and i keep doing it here even if i slow down evrything  :smiley-roll-blue:

Go Up