Coding question: Two switches, four states

Jeff Haas

Senior Member
I've been trying to figure out what approach to take to select one of four states with two switches. In other words:

Switch 1 off, Switch 2 off = State 0
Switch 1 on, Switch 2 off = State 1
Switch 1 on, Switch 2 on = State 2
Switch 1 off, Switch 2 on = State 3

You would set the switches and the code would branch to one of four loops. What's the best way to test for this? Nested If..Then...Else statements? Another way?

Any help is appreciated.

Jeff
 

boriz

Senior Member
You can use 4 IFs, or a SELECT CASE structure, or my personal fave, LOOKDOWN followed by ON GOSUB.
 

Jeff Haas

Senior Member
Thanks, that's helpful. I was also considering BRANCH.

Now I just need a tip on one more thing...I need to set a variable to a specific state, based on how the two switches are set, as in my first post. So far I can see doing it this way:

- Read switch 1 into b1, and switch 2 into b2. b1 and b2 will be either 0 or 1.

Then:

IF b1=0 AND b2=0 THEN b3=0
IF b1=0 AND b2=1 THEN b3=1
IF b1=1 AND b2=0 THEN b3=2
IF b1=1 AND b2=1 THEN b3=3

Then b3 would be used for LOOKDOWN or BRANCH.

Is there a better way to do it?
 

erco

Senior Member
b2=pinsb and 3 ' one step, reads switches on b.0 and b.1 b2 value is 0-3 based on switch inputs
 

inglewoodpete

Senior Member
b2=pinsb and 3 ' one step, reads switches on b.0 and b.1 b2 value is 0-3 based on switch inputs
I like that one. To complete the picture and make it a little clearer and more flexible:
Code:
b2 = PinsB And %00000011  'Select the bit pattern to match the input pins
Select Case b2
Case 0 'Or a bit pattern to match one of the states
   'Insert code for the first condition
Case 1 'Or a bit pattern to match one of the states
   'Insert code for the second condition
Case 2 'Or a bit pattern to match one of the states
   'Insert code for the third condition
Else
   'Insert code for the forth condition
EndSelect
 

westaust55

Moderator
If using the ON...GOSUB construct:

Code:
Main:
	b2 = PinsB And %00000011  'Select the bit pattern to match the input pins

	ON b2 GOSUB Case0, Case1, Case2, Case3
	GOTO Main

Case0:
   'Insert code for the first condition
	RETURN
Case1:
   'Insert code for the second condition
	RETURN
Case2:
   'Insert code for the third condition
	RETURN
Case3:
   'Insert code for the fourth condition
	RETURN
 

Jeff Haas

Senior Member
Wow, in only a few hours I get a lot of helpful answers. Thanks, it's why I like this forum. I've just moved from an 08M to both 08M2 and 14M2, so the port/pin terminology is new.

Here's another question. I tried both code examples, and in the simulator, the B.0 button is permanently on - I can't toggle it off. Changing "b2 = PinsB And %00000011" to PinsC fixes that, but it seems that B.0 can't be used as an input on a 14M2 (Manual 1, page 10).

So my first thought was to move over a leg, using two that can be configured as inputs:

b2 = PinsB And %00000110

But then that throws off the value in PinsB - you get 0, 2, 4 or 6 depending on which switches are closed, and the case selection doesn't work. You can add the three additional cases but they take up space and don't do anything...which is annoying but certainly not fatal. Of course if you simulate an 18M2 instead of a 14M2 this goes away as well.

Thoughts?
 

westaust55

Moderator
I tried both code examples, and in the simulator, the B.0 button is permanently on - I can't toggle it off. .....it seems that B.0 can't be used as an input on a 14M2 (Manual 1, page 10).
Ah yes, :mad: an oversight there - had not checked which chip you were specifically using.
AS you say, the solution is in the pin-out diagrams in Manual 1 starting at page 9.
For any M/M2 PICAXE the SERIALOUT pin can be used as an output under program control - sometimes digital, sometimes ADC, but NEVER as an input.

If you were to use B.1 and B.2 then change the first line to:
b2 = PinsB / 2 And %00000011
 

hippy

Technical Support
Staff member
For a more 'port/pin neutral' solution you could try this ...

Code:
Symbol SWITCH_0 = pinC.1 ' Switch for 0 bit
Symbol SWITCH_1 = pinC.3 ' Switch for 1 bit

Do
  b0 = SWITCH_0 : bit1 = SWITCH_1
  SerTxd( "Switch Selection = ", #b0, CR, LF )
Loop
 

Buzby

Senior Member
Code:
Symbol SWITCH_0 = pinC.1 ' Switch for 0 bit
Symbol SWITCH_1 = pinC.3 ' Switch for 1 bit

Do
  b0 = SWITCH_0 : bit1 = SWITCH_1
  SerTxd( "Switch Selection = ", #b0, CR, LF )
Loop
If bit twiddling was an Olympic sport, hippy would have won us another gold !
 

Jeff Haas

Senior Member
Sorry erco, didn't mean to over look you! It was late last night.

How about pinsc and 3 using c.0 and c .1 ?
Your solution works too. Checking in the simulator and toggling C.0 and C.1 confirms it.

Code:
#picaxe 14M2

Main:
	b2=PinsC and 3
	ON b2 GOSUB Case0, Case1, Case2, Case3

	GOTO Main
	
Case0:
	RETURN

Case1:
	RETURN

Case2:
	RETURN

Case3:
	RETURN
 

Jeff Haas

Senior Member
Hippy...very nice solution! Also "works a treat" in the simulator.

Code:
#picaxe 14M2

Symbol SWITCH_0 = pinC.1 ' Switch for 0 bit
Symbol SWITCH_1 = pinC.2 ' Switch for 1 bit

	Do
	  b0 = SWITCH_0 : bit1 = SWITCH_1
	  SerTxd( "Switch Selection = ", #b0, CR, LF )
	
	ON b0 GOSUB Case0, Case1, Case2, Case3
	
	Loop
	
Case0:
	RETURN

Case1:
	RETURN

Case2:
	RETURN

Case3:
	RETURN
 

Jeff Haas

Senior Member
OK, I've done some more reading and realize my post to erco showed that I didn't understand the "bitwise and" operation he proposed, and that Pete and WestAust55 had both adapted (and converted to binary). I copied their code into the program editor and ran it, but now I see what that part of the code is actually doing.

Thanks again to the members of the forum, I appreciate the help!
 
Top