Switch Logic Design
Greetings
I'm trying to get the basic stamp to recognize shaft rotation direction by using the sequence of two switches, 01-11-10 being one direction, and the other being 10-11-01. This program tokenizes but I have not yet tried it. I'm wondering if this is the best way to do this?
I'm trying to get the basic stamp to recognize shaft rotation direction by using the sequence of two switches, 01-11-10 being one direction, and the other being 10-11-01. This program tokenizes but I have not yet tried it. I'm wondering if this is the best way to do this?
' {$STAMP BS2}
' {$PBASIC 2.5}
DO
IF ( IN0 = 1 ) AND ( IN1 = 0 ) THEN 'first switches on, second off
IF ( IN0 = 1 ) AND ( IN1 = 1 ) THEN ' both on
IF ( IN0 = 0 ) AND ( IN1 = 1 ) THEN 'first off, second on
HIGH 14
PAUSE 1000
LOW 14
ENDIF
ENDIF
IF ( IN1 = 1 ) AND ( IN0 = 0 ) THEN 'reverse action
IF ( IN1 = 1 ) AND ( IN0 = 1 ) THEN
IF ( IN1 = 0 ) AND ( IN0 = 1 ) THEN
HIGH 15
PAUSE 1000
LOW 15
ENDIF
ENDIF
LOOP
END

Comments
I'll post an example shortly....
Paul
Below is a portion of some code I wrote with his help. I've generalized it for you so it may not run as typed and it reads 4 inputs instead of just 2.
' [----------------------------------------------------------------------------] ' [-- I/O Definitions --] ' [----------------------------------------------------------------------------] SwtchInputs VAR IND 'pins 12,13,14,15 to read switch inputs ' [----------------------------------------------------------------------------] ' [-- Variables --] ' [----------------------------------------------------------------------------] theSwitches VAR Nib swtchCounter VAR Nib ' [----------------------------------------------------------------------------] ' [-- Program Code --] ' [----------------------------------------------------------------------------] Main: DO GOSUB Get_Switches SELECT theSwitches CASE %1000 GOSUB Do_Something_A CASE %0100 GOSUB Do_Something_B CASE %0010 GOSUB Do_Something_C CASE %0001 GOSUB Do_Something_D ENDSELECT LOOP END ' [----------------------------------------------------------------------------] ' [-- Subroutines --] ' [----------------------------------------------------------------------------] Get_Switches: theSwitches = %0000 FOR swtchCounter = 1 TO 5 'loop to debounce the inputs theSwitches = ~theSwitches & SwtchInputs NEXT RETURN Do_Something_A: ' your code here RETURN Do_Something_A: ' your code here RETURN Do_Something_B: ' your code here RETURN Do_Something_C: ' your code here RETURN Do_Something_D: ' your code here RETURNHere's a link to a post with my full original code.
Perhaps smarter people will chime in soon!
Paul
You will probably need to debounce the switch inputs ( see them in a certain state for a set length of time ) . At first glance and not knowing exactly how the switches operate my inclination would be to just do two checks , the first would be both switches at logic level 1 , seeing this state I would move on and wait for a change in either bit 0 or bit 1 , whichever went low would indicate direction. Once direction is determined go back to looking for both switches at logic level 1.
Pauls example can certainly be adapted to do the above , I particularly like the way he has used IND , in case you have not checked it out yet take a look at INS , OUTS and DIRS under Memory and Variables in the PBasic help file these instructions allow you to read/write all or some of the16 I/O simultaneously
Jeff T.