Shop OBEX P1 Docs P2 Docs Learn Events
Switch Logic Design — Parallax Forums

Switch Logic Design

MSteinMStein Posts: 9
edited 2011-02-06 18:52 in General Discussion
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?
' {$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

  • doggiedocdoggiedoc Posts: 2,246
    edited 2011-02-05 06:18
    I would think that SELECT...CASE routine using %00, %01, %10, and %11 would suit this nicely.

    I'll post an example shortly....

    Paul
  • doggiedocdoggiedoc Posts: 2,246
    edited 2011-02-05 07:00
    I can't find the original article at the moment. But Jon Williams (JohnnyMac) wrote a Nuts & Volts article a while back about debounceing button inputs that would translate nicely for your application of switches.

    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
      RETURN
    
    
    


    Here's a link to a post with my full original code.

    Perhaps smarter people will chime in soon!

    Paul
  • MSteinMStein Posts: 9
    edited 2011-02-06 15:23
    Thank you...
  • bsnutbsnut Posts: 521
    edited 2011-02-06 18:05
    Are you trying to look at a Encoder that is attached to a shaft? If, this is correct an encoder program will work perfect and it will do what you are looking for. This program can be found it in the Stamp Works PDF, which is on the Parallax website.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-02-06 18:52
    Hi , when your program makes the first comparison ( IF instruction ) if it is true then the following comparisons will all be false. Seeing the second comparison as false the program flow will exit to the endif 's and repeat the loop. When the switches change state and the first comparison becomes false the program flow will exit to the endif 's and repeat the loop. In other words the second and third comparisons can never be seen as true.

    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.
Sign In or Register to comment.