Shop OBEX P1 Docs P2 Docs Learn Events
State machine with BS2 — Parallax Forums

State machine with BS2

vaclav_salvaclav_sal Posts: 451
edited 2009-02-11 03:11 in General Discussion
I am looking for a sample code written in Parallax basic implementing state machine.
I am·coding·four state system and just need something to get me started on my project.
Thanks for reading
·
·

Comments

  • ZootZoot Posts: 2,227
    edited 2009-02-10 20:50
    
    
    state VAR Nib
    dur   VAR Byte
    
    buttons VAR INA  ' active low buttons on pins 0-3
    leds VAR OUTD ' 4 leds on pins 12-15
    
    DIRA = %0000
    DIRD = %1111
    
    Start:
    
     state = $F
     dur = 255
    
    
    Main:
    
    Parse_Timer:
       IF dur > 0 THEN
          dur = dur - 1
       ENDIF
       PAUSE 100ms ' 100ms "frame" for state machine
    
    Parse_States:
    
       ON state GOTO state_00, state_01, state_02, state_03 ' etc
    
    state_04: ' and higher since they are not defined above -- waiting for "release"
      
       IF buttons = 0 THEN
          state = $0
       ENDIF
       GOTO States_Done
    
    state_03:  ' wait for timeout, then see if no buttons pressed
        IF dur = 0 THEN
           state = $4
        ENDIF
       GOTO States_Done
    
    
    state_02:  ' wait for button 3 to be pressed, if times out, restart state machine
        IF buttons.BIT3 = 1 THEN
          state = $3
          dur = 50 ' 5 seconds
        ELSEIF dur = 0 THEN
           state = $0 
        ENDIF
       GOTO States_Done
    
    state_01:  ' wait for button 1 to be pressed, if times out, restart state machine
        IF buttons.BIT1 = 1 THEN
          state = $3
          dur = 50 ' 5 seconds
        ELSEIF dur = 0 THEN
           state = $0 
        ENDIF
       GOTO States_Done
    
    state_00:  ' wait for button, if it's 0 or 3, then wait for  1 or 4 to be pressed within several seconds
        IF buttons = %0001 THEN ' won't work with simulataneous button presses
          state = $1
          dur = 20 ' user must press other button within 2 seconds!
        ELSEIF buttons = %0100 THEN
           state = $2 
           dur = 10 ' user must press other button within 1 seconds! fast!
        ENDIF
      '  GOTO States_Done
    
    
    States_Done:
       leds = state ' show 4 bit state on leds
       DEBUG HOME, "state:", IHEX1 state, "    dur:",DEC3 dur, CLREOL
       ' more code, say outputting results of state machine actions
    
         GOTO Main
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-02-10 21:32
    Here's some.· It will take two replies to get it all in, but this machine uses multiple cogs -- no, not cogs, slots·-- and has many states.· It works. See the comments at the front for explanation of the states.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net

    Post Edited (Carl Hayes) : 2/10/2009 9:40:05 PM GMT
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-02-10 21:33
    Here's the rest of it.· I have them a little out of order, because I left Slot 2 out of the first reply accidentally.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net

    Post Edited (Carl Hayes) : 2/10/2009 9:38:19 PM GMT
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-02-10 21:45
    Thanks guys.
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2009-02-11 03:11
    Hi vaclav_sal,

    I have examples of PBASIC state machines posted at,
    www.emesys.com/BS2fsm.htm
    buttons, keypads, encoders, game. Good luck stamping!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.