Page 1 of 1

How do you toggle the value of a binary variable?

Posted: Wed Dec 30, 2009 10:34 pm
by F22-Raptor
I would like to read a binary variable (ExtendedMode)and take an action based on it's value (0 or 1).
What I'm struggling with this variable will be set by a push button.
So for example, the program starts running my my variable ExtendedMode is initialized with a value of 0, now a momentary push button is pressed and I want my ExtendedMode to be 1 until next time when the button is pressed again when I want it to toggle back to 0.

Can you use something like ExtendedMode = !ExtendedMode or is there some trick I'm supposed to use?

Thanks

D.

Re: How do you toggle the value of a binary variable?

Posted: Wed Dec 30, 2009 10:45 pm
by F22-Raptor
OK, so I just tried the var = !var and it works (in a calculation Macro), now this might me stupid but how do I use my new value to turn no an LED?

If I use the component macro it tells me to select a macro (LED(0)), if I use an output macro it sets my variable to 1?

D.

Re: How do you toggle the value of a binary variable?

Posted: Thu Dec 31, 2009 9:39 am
by Steve
Use the output icon and set the output value to your variable, and select "single bit" mode.

Re: How do you toggle the value of a binary variable?

Posted: Thu Dec 31, 2009 12:19 pm
by medelec35
I have created a basic toggle version for you. This has no switch de-bounce, which can be easily added. I have not use switch macro just in case you want the routine to run and do other things.
All you have to do is set all you own config parameters up i.e type of chip, osc speed, watchdog off etc.

Note: Using var = var XOR 0b00000001 will also toggle

Re: How do you toggle the value of a binary variable?

Posted: Fri Jan 08, 2010 7:05 pm
by F22-Raptor
Thanks guys. This is very helpful.

D.