Shop OBEX P1 Docs P2 Docs Learn Events
counter question — Parallax Forums

counter question

can someone explain to me why my code will not work without this counter line? im not understanding this for some reason. heres the code,
DO
counter=counter +1
IF counter>10 THEN
GOSUB pingout
ENDIF
IF (distance >30) THEN
GOSUB fwd
ELSE
GOSUB slowdown
ENDIF
LOOP

i didnt put in the subroutines but this is the main program. it will not work without the counter and im trying to figure out why. Thanks

Comments

  • You really need to post the whole program (between [ code] and [ /code] (without the spaces)). Even more important, you will have to explain just *how* it is not working. What are the symptoms?
  • ercoerco Posts: 20,254
    Your counter=counter+1 increments variable "counter" each loop, which subsequently calls GOSUB pingout, which subsequently measures & updates variable "distance", which conditionally calls GOSUB fwd.

    Without counter=counter+1 , your program will always fall through (via ELSE) to GOSUB slowdown.
  • Is it your intention to repeatedly call pingout with each count step after 10 or do you reset the counter in the hidden part of your program? You might be firing the Ping faster than it is capable.
  • i dont think i wrote that correct. ive tried to replace the counter=counter and the FOR NEXT statement with just the ping routine but all it does is twitch so thats why i was wondering why i needed the counter .
  • As Tom stated above, we really need to see the whole program because we cannot see what the subroutines are doing.
    In order to function properly, the Ping should have 20+ milliseconds between firings else you run the risk of incorrect readings.
  • fwd:
    pulsout esc, 775
    pause 20
    return

    slowdown:
    pulsout esc,550
    pause 20
    return


    all it does right now is crawl fwd until it detects something then stops. im adding more and more steps one at a time to see how much it will do before slowing down. i wanted to know y i need the counter and cant just let it ping without it
  • I think the purpose of the counter might be to give the device time to move a small distance between pings. However, it looks like once the counter has exceeded 10 I do not see where the counter is reset to 0 to start a new interval. You didn't post the pingout subroutine so I don't know if the counter is set back to 0 there or just not at all. If not at all then try placing a counter = 0 statement between if counter... and gosub pingout and see if that helps.
    Tom, Erco, and I are trying to help, and have asked several times for the complete code and yet we still do not have a complete picture.
  • sorry forgot the pingout code. its just a small little code so i can start building blocks onto it. im new to this ,its my first time on here or trying the bs2 so sorry if ive left things out but here it is:

    pingout:
    counter =0
    low ping
    pulsout ping,5
    pulsin ping,1,distance
    distance=distance**2257
    return


    now for now that is all of it. all i wanted to know was the purpose of the counter being here and what it does. thanks
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    As Hal said, the counter variable is there to allow the code to run for 10 loops between each PING))) pulse. Without it the sensor would be triggered every pass through the loop resulting in some very choppy forward motion as the PING))) can take over 18 ms to complete a reading.
  • Initialise all variables before entering the do-loop, avoid to change global variables in a subroutine.

    So, initialise 'counter = 0' before DO, take 'counter = 0' out of the pingout subroutine and place it before or after 'GOSUB pingout'.
Sign In or Register to comment.