Shop OBEX P1 Docs P2 Docs Learn Events
Help with counting inputs — Parallax Forums

Help with counting inputs

Kenny DKenny D Posts: 8
edited 2006-06-07 04:25 in BASIC Stamp
Hi everyone,

I have just started playing around with Boe Bot. It's great fun.

Can someone help me with this?·

I am trying to get Boe Bot to follow a course using a microswitch tied to input 7.· The microswitch feels for these
poles I have set up.·I want to be able to set a constant and have the Bot move forward until input 7· counts up to the constant
and then stop.·
·I would then like it to reset the counter and move backwards until it counts to another constant.

·I have tried many different things·that resemble··

IF (IN7 = 0) THEN
··· counter = counter + 1
····· IF (counter =7) THEN
·GOSUB forward

······ I know I am far off..so please help

Thanks in advance.
·confused.gif
Kenny

Comments

  • FranklinFranklin Posts: 4,747
    edited 2006-06-02 15:50
    IF (IN7 = 0) THEN
    counter = counter + 1
    IF (counter < 7) THEN
    GOSUB forward

    this should work. (notice the less than)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-06-02 17:52
    The problem with that routine is that in7 probably stays low for a relatively long time, long enough for the counter loop to execute many times. Whereas what you want is one count for each time it hits a switch. Debounce the switch with an RC circuit.

    newin7 VAR bit
    oldein7 VAR bit
    oldin7 = in7
    
    DO
    newin7 = in7
    counter = counter + (newin7 ^ oldin7 & newin7)   ' increment on 0-->1 transition
    oldin7=newin7
    DEBUG ? counter
    LOOP
    



    Note that ^ is the Stampese XOR and & is Stampese AND, bitwise, so the thing in () is 1 only when in7 makes the transition from 0 to 1, and that gets added directly to the counter.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Kenny DKenny D Posts: 8
    edited 2006-06-04 06:37
    That works great Tracy thanks.
    I am trying to get a generic working program so I can modify it as needed later.
    Right now I am just messing with one wheel until I have it down. I am having a problem with the second movement.
    The wheel will not pulse like the first time.
    Forward1 works fine. When it gets to back1 the wheel moves slow and jerky ant not at the speed I told it to.
    I have tried several speeds with no change.
    Any suggestions??

    thanks




    [noparse][[/noparse]Variables]

    newin7 VAR Bit
    oldin7 VAR Bit
    oldin7 = IN7
    counter VAR Byte

    '
    [noparse][[/noparse] Main Routine ]

    DO

    newin7 = IN7
    counter = counter + (newin7 ^ oldin7 & newin7) ' increment on 0-->1 transition
    oldin7=newin7
    DEBUG ? counter

    IF (counter > 7) THEN
    GOSUB forward1
    PAUSE 10

    IF (counter > 9) THEN
    GOSUB back1
    PAUSE 10
    ENDIF
    ENDIF

    LOOP
    forward1:
    PULSOUT 13, 850
    RETURN

    back1:
    PULSOUT 13, 650
    RETURN
  • SSteveSSteve Posts: 808
    edited 2006-06-04 08:49
    Your IF/THEN logic isn't right:

    IF (counter > 7) THEN
    GOSUB forward1
    PAUSE 10


    IF (counter > 9) THEN
    GOSUB back1
    PAUSE 10
    ENDIF

    ENDIF

    Once counter is greater than 9, you're calling both the forward1 and back1 subroutines. I'm not sure exactly what you're trying to do, but this code:
    IF (counter > 9) THEN
      GOSUB back1
      PAUSE 10
    ELSEIF (counter > 7) THEN
      GOSUB forward1
      PAUSE 10
    ENDIF
    
    


    will do nothing until counter is greater than 7. It will call forward1 when counter is 8 & 9. Once counter is greater than 9, it will call back1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-06-04 18:20
    Kenny, you probably also need a "mode" variable in the mix. That is a bit that is 1 when the bot is supposed to go forward and 0 when backward.

    Then,
    IF mode=1 AND counter >7 THEN mode=0 : counter=0
    IF mode=0 AND counter >9 THEN mode=1 : counter=0
    BRANCH mode, [noparse][[/noparse]backward, forward]
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Kenny DKenny D Posts: 8
    edited 2006-06-07 00:25
    Thanks guys for your help.
    The following code does what I need it to do.··I although I couldn't· get the MODE variable to work.
    ·My programming knowledge is still a beginner.

    I would like to get an output to work when the pogram hits each goal.· However,
    ··the program has the·output·as part of the loop, so the output doesn't work·correctly.
    Any suggestions as how to make the output work outside the loop?

    Thank you sooo much for your help...

    Kenny




     Speaker PIN 4
    '------[noparse][[/noparse]Variables]---------------------------------------------------------
    newin7 VAR Bit
    oldin7 VAR Bit
    oldin7 = IN7
    counter VAR Byte
    temp VAR Byte
    ' -----[noparse][[/noparse] Main Routine ]----------------------------------------------------
    start:
       newin7 = IN7
       counter = counter + (newin7 ^ oldin7 & newin7)   ' increment on 0-->1 transition
       oldin7=newin7
       DEBUG ? counter
     IF (counter <= 7) THEN
       GOSUB forward
    ELSEIF (counter >= 8) AND (counter<=10) THEN
         GOSUB back
    ELSEIF (counter >= 11) AND (counter <=15) THEN
         GOSUB forward
    ELSEIF (counter >= 16 )AND (counter <=33) THEN
         GOSUB back
    ELSEIF (counter >= 16 )AND (counter <=33) THEN
         GOSUB forward
          ENDIF
      GOTO testoutput
     
    testoutput:
            IF (counter = 8)THEN
          FREQOUT Speaker, 10 ,5050
          HIGH 10
          PAUSE 10
          LOW 10
          PAUSE 10
          HIGH 10
          PAUSE 10
          HIGH 10
             ENDIF
        GOTO start
    '-----------------------------Routines
            forward:
               DEBUG "forward "
               PULSOUT 13, 850
               PAUSE 20
               RETURN
            back:
                DEBUG "back "
                PULSOUT 13, 650
                PAUSE 20
                RETURN
    
    
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-06-07 04:25
    Kenny -

    Please go back and re-read the message you posted, as it doesn't make a whole lot of sense, in a number of places. Here's what I mean:

    First you say "... I couldn't get the MODE variable to work." yet there is NO "MODE" variable in the program. What's the story with that?

    You also say "The following code does what I need it to do." but later on say "... the output doesn't work correctly." Does it work, or doesn't it work, and if it doesn't work, what is the program doing differently from what you want it to do?

    Then, in the program, the following code seems rather odd:

    ELSEIF (counter >= 16 ) AND (counter <=33) THEN
    GOSUB back
    ELSEIF (counter >= 16 ) AND (counter <=33) THEN
    GOSUB forward

    Last, just as a suggestion, you may find that using SELECT ... CASE is a bit easier to use and understand than all of those IF ... THEN statements. SELECT ... CASE works very well for range values.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
Sign In or Register to comment.