Shop OBEX P1 Docs P2 Docs Learn Events
Help writing a code — Parallax Forums

Help writing a code

ur5pointos2slour5pointos2slo Posts: 19
edited 2008-11-08 20:40 in BASIC Stamp
Ok so in my last post I was trying to hook up infrared headlights and use QTI line follower sensors. I finally have them all hooked up but now I need to write a code to make it work properly and need a little help. I need the robot to follow a given line(the line travels all the way to the wall). So I need to get the·bot to stop short of the wall using the headlights. Then after it does that I need it to pause for about 10-20 seconds Make a 180 degree turn and follow the line back down to the other end and repeat the process. If anyone could help me write a code to make it do this that would be greatttly appreciated.

Comments

  • SRLMSRLM Posts: 5,045
    edited 2008-11-08 17:37
    Start out by writing out one piece of code, then type in another, test, debug, write, and so on. The way to tackle this is to break into little chunks.
  • ur5pointos2slour5pointos2slo Posts: 19
    edited 2008-11-08 19:57
    SRLM said...
    Start out by writing out one piece of code, then type in another, test, debug, write, and so on. The way to tackle this is to break into little chunks.
    I should have included a copy of the code that I have attempted. I can get it to do one thing but not the other..one time it will follow the line but then the headlights wont pick up or the other way around. Could anyone please look at my code and see if they could give me suggestions on how to get this thing to work right. Thanks
  • SRLMSRLM Posts: 5,045
    edited 2008-11-08 20:26
    You'll want a main sort of like this:

    Main:

    Loop until done
    gosub folow line
    if wall is ahead done = true
    End loop

    pause number of seconds

    Turn 180 degrees

    GOTO main



    Now, I'm assuming here that the line is between two walls and your bot is bouncing (sort of) between the two. As for your code, some suggestions. Always use proper indentation. Notice that on line 18 you have a DO, and the next line is not indented. Wrong. Second, make sure that you close out anything that you open. With that same DO, you should have a loop close by (not like you have it; on line 84). Keep things in small, logical blocks. You should only have one GOTO command in your code: to loop back to main. Everything else, you should have a GOSUB that runs until a RETURN. Make sure that you have all the returns you need in your code.

    Next, replace all pin numbers with a pin variable declared at the begining. This will help you do two things: easily change what I/O pin that particular component uses, and make your code more self documenting. Next, notice that you have two blocks of code that deal with the servos (lines 31 and 89). Delete one, then if neccessary, you can call the appropriate subroutine to do the work.
  • ur5pointos2slour5pointos2slo Posts: 19
    edited 2008-11-08 20:40
    SRLM said...
    You'll want a main sort of like this:

    Main:

    Loop until done
    gosub folow line
    if wall is ahead done = true
    End loop

    pause number of seconds

    Turn 180 degrees

    GOTO main



    Now, I'm assuming here that the line is between two walls and your bot is bouncing (sort of) between the two. As for your code, some suggestions. Always use proper indentation. Notice that on line 18 you have a DO, and the next line is not indented. Wrong. Second, make sure that you close out anything that you open. With that same DO, you should have a loop close by (not like you have it; on line 84). Keep things in small, logical blocks. You should only have one GOTO command in your code: to loop back to main. Everything else, you should have a GOSUB that runs until a RETURN. Make sure that you have all the returns you need in your code.

    Next, replace all pin numbers with a pin variable declared at the begining. This will help you do two things: easily change what I/O pin that particular component uses, and make your code more self documenting. Next, notice that you have two blocks of code that deal with the servos (lines 31 and 89). Delete one, then if neccessary, you can call the appropriate subroutine to do the work.
    Yes you are correct it is basically going back and forth between 2 walls. Thanks for writing back so quick I will take these suggestions and see what I can do.
Sign In or Register to comment.