Shop OBEX P1 Docs P2 Docs Learn Events
GOSUB issue — Parallax Forums

GOSUB issue

ShackShack Posts: 11
edited 2009-08-18 00:46 in BASIC Stamp
I'm having a little issue with the attached code.· The last "GOSUB right"·doesn't seem to be functioning.· I changed the pin information thinking it was a bad output.· It did not change the issue.· It's probably somehting simple that I'm missing.· Any help would be nice?

Comments

  • JDJD Posts: 570
    edited 2009-08-17 20:09
    Shack,

    Can you please explain what the program is doing or not doing as expected? That way we can see how it is supposed to operate and offer some suggestions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Respectfully,


    Joshua Donelson
    www.parallax.com
  • ShackShack Posts: 11
    edited 2009-08-17 20:19
    Sorry.· It's just a simple program I'm going to use for·large object·detection.· The servo moves to a point then PING))) for a distance.· The "left", "straight", and "right" programs lights a LED according to the distances found in the PING))) process, time0, time1, and time2.· At present, the "right" sub doesn't light the LED.· The LED and output are good.
  • ZootZoot Posts: 2,227
    edited 2009-08-17 20:47
    You are overwriting the first value in your implied array:

    PULSOUT 8, 5
    PULSIN 8, 1, time
    DEBUG CR, "time = ", DEC5 time
    time(ping_num) = time ** 2251
    DEBUG CR, "Distance = ", DEC4 time(ping_num), " cm"
    DEBUG CR, "ping_num = ", DEC1 ping_num
    RETURN
    
    



    In the above, "time" is the same as time(0). Try this:

    PULSOUT 8, 5
    PULSIN 8, 1, time(ping_num)
    DEBUG CR, "time = ", DEC5 time(ping_num)
    time(ping_num) = time(ping_num) ** 2251
    DEBUG CR, "Distance = ", DEC3 time(ping_num), " cm"
    DEBUG CR, "ping_num = ", DEC1 ping_num
    RETURN
    
    

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

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • JDJD Posts: 570
    edited 2009-08-17 20:59
    Shack,

    Looking at the code I see you are calling the ping: routine once in the search but then use the time values as though they were called each time at 9, noon, and 3 o'clock servo position.

    You would want to call each of the positions the servo needs to go to, and then use the ping: routine to get that measurement, and then have a statement like time = time0, 1, or 2 depending on what position the servo is in. Once you have that, you will have 3 different time measurements all from the different positions you are searching for.

    So the flow would look something like the following:

    Step1: Move servo to position
    Step2: Take measurement
    Step3: Assign time measurement to new time(0-3) variable
    Step4: Repeat steps 1-3 until all measurements have been taken and assigned to variables
    Step5: Run routine to turn on LEDs

    I hope this helps,




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Respectfully,


    Joshua Donelson
    www.parallax.com
  • ShackShack Posts: 11
    edited 2009-08-17 21:18
    Zoot - I'm using the time variable as a scratch pad.· Overwriting doesn't matter, but I apprecaite the effort.



    Jos - "ping" is being called each time the FOR...NEXT for the servo is being executed.· By using the "ping_num = ping_num +1" and the time(ping_num) indirect addressing, I'm loading the information into time0, time1, and time2 each time the servo moves.· I'll verify the information in time0, time1, and time2 tonight to make sure it is working.· The other two subs, "left" and "straight" are functioning correctly.· Thanks.
  • ShackShack Posts: 11
    edited 2009-08-17 22:40
    I changed the code for troubleshooting and the plot thickens...Here is the debug information.·

    Distance = 0021 cm

    ping_num = 00

    Distance = 0028 cm

    ping_num = 01

    Distance = 0048 cm

    Ping_num = 02

    Time0 = 0028 cm

    Time1 = 0048 cm

    Time 2 = 0000 cm

    The initial distances information is correct along with the ping_num value, but the information is· being placed in the wrong variables i.e. time1 into time0, time2 into time1.·This leaves "0000" for time2. I must be missing something in the sequence of the code, but I don't know what it is.
  • ZootZoot Posts: 2,227
    edited 2009-08-17 22:40
    It has to matter, because time(0) is the same as time0, which means that the value in time0 (aka time(0)) will not be correct when time0 is checked (to see if LED should be lit). The way your code reads right now, the value in time0 will be clobbered before gosub left is run.

    In your implied array:

    time0 = time(0) = time
    time1 = time(1)
    time2 = time(2)

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

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • ShackShack Posts: 11
    edited 2009-08-18 00:46
    Zoot - It doesn't matter.·"Time" is just a scratch pad.· Think about it like this:

    When the·first·GOSUB ping is ran, let say·"time"·is calculated at·10.· That means time(ping_num) = 10 ** 2251 = time(0).· That saves the initial value of "time".·When the·next GOSUB·ping is ran, let say·"time"·is calculated at·12.· That means time(ping_num) = 12 ** 2251 = time(1).· That saves the second value in time(1).

    I hope that helps to clear it up.

    Jos and Zoot - I've got it working now.· I needed to move the indirect address pointer statement, ping_num = ping_num +1, up before the GOSUB ping.· It changes the time variables to time1, time2, and time3 and solves the problem.· Just something in the sequence, I guess.· I didn't change anything else.· Thanks for your help.


    Zoot said...
    It has to matter, because time(0) is the same as time0, which means that the value in time0 (aka time(0)) will not be correct when time0 is checked (to see if LED should be lit). The way your code reads right now, the value in time0 will be clobbered before gosub left is run.

    In your implied array:

    time0 = time(0) = time
    time1 = time(1)
    time2 = time(2)

Sign In or Register to comment.