PDA

View Full Version : toggle or count?



earltyso
- 26th May 2008, 01:36
Simple question...
I have a TX module I am sending to an RX board.
I simply want to push a button once to latch a Relay "ON", then push the same button again to turn the relay "OFF".
Yes my modules are working fine... I just need help with this simple brain teaser.
I was thinking of adding + 1 to a variable with a initial condition of zero... each time the button is pushed then divide it down to an even or odd number. If odd the relay stays high, if even the relay if off.

Basically I am making a simple state machine. I should know this but have not messed with state logic in a while.

Could I use toggle to do this more simply?

skimask
- 26th May 2008, 03:11
Just increment a variable and only pay attention to the LSB of that variable...
i.e.

x = x + 1

if x.0 = 0 then -the variable is an even number

if x.0 = 1 then -the variable is an odd number

earltyso
- 26th May 2008, 03:29
That makes perfect sence.
Perhaps in 10 more years I will be ready to apply for work with Lock Head Martin and propel the next Mars vehicle to welll... Mars. Until then, I will press on and keep learning the basics. Thanks again!

skimask
- 26th May 2008, 04:23
Perhaps in 10 more years I will be ready to apply for work with Lock Head Martin and propel the next Mars vehicle to welll... Mars.
Looks like the boys (and girls) in Pasadena have pretty much got Mars handled for now...as of tonight anyways...
Maybe you should shoot for something a wee bit farther out :D

Acetronics2
- 26th May 2008, 08:54
Simple question...

I was thinking of adding + 1 to a variable with a initial condition of zero... each time the button is pushed then divide it down to an even or odd number. If odd the relay stays high, if even the relay if off.


Could I use toggle to do this more simply?



Hi, Martian

You also have, using a single BIT :

IF Condition_true then relay_state = relay_state ^ 1

...

Alain