Shop OBEX P1 Docs P2 Docs Learn Events
having issues exiting loops etc. — Parallax Forums

having issues exiting loops etc.

thefitz85thefitz85 Posts: 16
edited 2009-04-24 18:37 in BASIC Stamp
i have been trying to use the loop until function to exit out of the obstacle_avoidance subroutine. pretty sure i am having an issue with the serin for my ir detection. the serin pin is a seperate electronics circuit designed to interact with household remote control. it seems that when i try to push the stop button on remote (button_code = 101), that the device is not responding. do i have to reference it again in the subroutine, or can i use the loop until/while function to make it exit. have been working at it for a while, any help would be appreciated
sorry for the attachment, using a mac. . . .doesn't like my file types i guess








' {$STAMP BS2}
' {$PBASIC 2.5}

DEBUG "program running"

'
[noparse][[/noparse]variables]

irdetectleft VAR BIT
irdetectright VAR BIT
irdetectcenter VAR BIT
BUTTON_CODE VAR BYTE 'Infrated Button Code
DEVICE_CODE VAR BYTE 'Infrared Device Code
'
[noparse][[/noparse]main routine]
'***************************
'* MAIN PROGRAM *
'***************************

MAIN:
GOSUB COMMAND_030 'Send the Remote control Command to Infrated Control Freak

GOTO MAIN

COMMAND_030:
SEROUT 14, 16468, [noparse][[/noparse]30]
poop:
SERIN 15, 16468,1000,main, [noparse][[/noparse]device_code, button_code]
DEBUG "button code =", DEC Button_Code," Device Code =",DEC Device_code," ", CR
DEBUG " ",CR
DEBUG "____________________________",CR
DEBUG HOME
IF Button_code = 21 AND Device_code = 1 THEN obstacle_avoidance
IF BUTTON_code = 101 AND device_code = 1 THEN STOP_bot

'
[noparse][[/noparse]subroutines]

GET_BUTTON:
SERIN 15, 16468, 500, main, [noparse][[/noparse]device_code, BUTTON_code]
IF BUTTON_code = 21 AND device_code = 1 THEN
GOSUB obstacle_avoidance
ELSEIF BUTTON_code = 101 AND device_code THEN
GOSUB STOP_bot
ENDIF
RETURN

obstacle_avoidance:
DO
FREQOUT 8, 1, 38500
irdetectleft = IN9

FREQOUT 2,1, 38500
irdetectright = IN0

FREQOUT 3,1, 38500
irdetectcenter = IN4

IF (irdetectcenter = 0) THEN
GOSUB back_up
ELSEIF (irdetectleft = 0) THEN
GOSUB turn_right
ELSEIF (irdetectright = 0) THEN
GOSUB turn_left
ELSEIF(irdetectcenter = 0) AND (irdetectleft = 0) AND (irdetectright = 0) THEN
GOSUB STOP_bot
ELSE
GOSUB forward_pulse
ENDIF
LOOP UNTIL (BUTTON_code = 101) AND (device_code = 1)
GOTO main

forward_pulse:
PAUSE 100
SEROUT 16, 8276, [noparse][[/noparse]"!A1E", CR]
SEROUT 16, 8276, [noparse][[/noparse]"!b1E", CR]
RETURN

turn_left:
PAUSE 20
SEROUT 16, 8276, [noparse][[/noparse]"!A14", CR]
SEROUT 16, 8276, [noparse][[/noparse]"!b14", CR]
RETURN

turn_right:
PAUSE 20
SEROUT 16, 8276, [noparse][[/noparse]"!A1E", CR]
SEROUT 16, 8276, [noparse][[/noparse]"!b1E", CR]
RETURN

back_up:
PAUSE 20
SEROUT 16, 8276, [noparse][[/noparse]"!a1E", CR]
SEROUT 16, 8276, [noparse][[/noparse]"!B1E", CR]
RETURN

STOP_bot:
PAUSE 20
SEROUT 16, 8276, [noparse][[/noparse]"!A00", CR]
SEROUT 16, 8276, [noparse][[/noparse]"!B00", CR]
RETURN

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-04-14 20:59
    What works and what does not? When you run this program what happens? If you are having trouble with a command write a small program that just does the process you are having trouble with and get that working before you throw it into a large program.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • JDJD Posts: 570
    edited 2009-04-14 21:04
    Gentleman,

    I don’t see where the GET_BUTTON: is referenced in the routine obstacle_avoidance. You would want to use the GET_BUTTON to check to see if the button was pressed, and then keep running the obstacle_avoidance, or go to the stop routine.



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


    Joshua Donelson
    www.parallax.com
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-15 12:35
    i am aware that the code we posted does not portray what we talked about on the phone, but as we experimented with the program we noticed that we were back to where we initially had started. every second the stamp wanted to scan for a command before it went through any routine. we could either get a 1 sec pulse forward or 1 sec pulse stop command. this is not what we desired from the program. we need it to continually loop the obstacle avoidance until the designated stop button is pushed. no what we had tried was putting the do command above the get_button sub. noticed what had happened and noticed it wasn't what we needed. now i am assuming that i should leave the do command where it is now but instead just of running the obs._avoidance w the gosubs we have we need to:

    obstacle_avoidance:
    DO
    FREQOUT 8, 1, 38500
    irdetectleft = IN9

    FREQOUT 2,1, 38500
    irdetectright = IN0

    FREQOUT 3,1, 38500
    irdetectcenter = IN4

    IF (irdetectcenter = 0) THEN
    GOSUB get_button (insert here)
    GOSUB back_up
    ELSEIF (irdetectleft = 0) THEN
    GOSUB get_button (here)
    GOSUB turn_right
    ELSEIF (irdetectright = 0) THEN
    GOSUB get_button (here)
    GOSUB turn_left
    ELSEIF(irdetectcenter = 0) AND (irdetectleft = 0) AND (irdetectright = 0) THEN
    GOSUB get_button (here)
    GOSUB STOP_bot
    ELSE
    GOSUB get_button (here)
    GOSUB forward_pulse
    ENDIF
    LOOP UNTIL (BUTTON_code = 101) AND (device_code = 1)
    GOTO main

    now im going to try this and see if this works, but as i was saying......we did try what you had said.....my partner just failed to post what we had tried vs. what we were currently working on.

    stephen,
    as of right now if we run the program as is we run into the problem of getting stuck in a loop that continually runs the obstacle_avoidance, but wont stop when we press the designated stop button (button_code = 101, device_code = 1). we can either initiate the go sub obstacle avoidance sub or simply the stop sub. like i said before....we assumed that we could execute what we need to do by placing a loop until w button codes in () but this does nothing for us. we have tried a number of different things that have all lead to a dead end. we have run through this program quite a bit and know what is doing what in this program thus far but from what we know and have experimented with we are running out of ideas. i will try running a similar smaller program to try and execute the loop until command but i think i am having trouble addressing what goes in the () can i just place the button_code and device_code as we have in the program already?
  • FranklinFranklin Posts: 4,747
    edited 2009-04-15 23:13
    It really would be helpful if you would use the attachment manager to ATTACH your actual code to your next post.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-16 12:26
    stephen i realize this but i am working wiht a mac and when i try to attach it i cannot upload it. so i apologize for inconvenience of having to read it on the thread but that's the only way i could get the code on my thread. but if you have any suggestions i would love to hear them
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-17 14:08
    josh this is jason..........this is the thread you need to check
    (effuzionz@yahoo.com)
  • JDJD Posts: 570
    edited 2009-04-17 15:40
    Jason & Fitz,

    I was waiting to hear to what happened when you added the get_button in the routines. When you added the following lines get_button (here) the editor wouldn't compile; if you take out the (here) then it should compile. You should also have a IF...THEN statement as a part of the get_button. Perhaps using two get_button routines would work better; one that checks only for the STOP command so that it can return to the program without starting over, and the other one that checked at the beginning for both states.

    **CHECK FOR BOTH STATES**

    GET_BUTTON:
    SERIN 15, 16468, 500, main, [noparse][[/noparse]device_code, BUTTON_code]
    IF BUTTON_code = 21 AND device_code = 1 THEN
    GOSUB obstacle_avoidance
    ELSEIF BUTTON_code = 101 AND device_code = 1 THEN
    GOSUB STOP_bot
    ENDIF
    RETURN

    **CHECK FOR STOP STATE**

    GET_BUTTON2:
    SERIN 15, 16468,500,main, [noparse][[/noparse]device_code, BUTTON_code]
    IF get_button = 101 and device_mode = 1 THEN
    GOSUB STOP_bot
    ENDIF
    RETURN

    Does this make since? And do you see where you can add these to your program? Also, is device_mode always 1?

    For an explanation of what that does and why. The BASIC Stamp can only do one thing at a time; known as a single thread operator. So when you are checking sensors, it can not receive the command to stop. Ideally, what you would check for the command to stop (GET_BUTTON2) as much as possible; so when you press the button, the BASIC Stamp would be more likely to see it and make those changes appropriately.

    I hope this helps to explain,

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


    Joshua Donelson
    www.parallax.com
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-20 12:48
    we added the get_button routine as i had said before w/out the (here) in it i was just putting that in so you knew what i had put into the program, and nothing good came of it. we also placed the do.....loop around the get button routines and nothing changed. we got back to either being able to do the obs_avoidance or the stop command. this program w the 2 get button routines seems to be doing the same thing. as we think more and more about it we don't think that what we would like to do isn't possible with one processor as we would like to send and receive data to and from the control freak which is what we are using for the ir remote control detection. it seems that no matter what we try, and i know we arent as familiar with the basic stamp as most of the people that respond on here but we feel like we have tried everything that makes sense. we need to exit a loop based on the input on the control freak but withe the one processor it will either check for buttons instead of obs_avoidance or do the obs_avoidance w/out checking for buttons...... if we can just simply exit a loop after a button is pushed like we keep hearing then why are we having such a difficult time doing it?
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-20 13:00
    also if we just use a serin then our control freak wont bother scanning for a button because it has already received and sent a signal. once the program starts running it locks into the program until we find a way to exit the loop it starts.......
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-20 13:00
    also if we just use a serin then our control freak wont bother scanning for a button because it has already received and sent a signal. once the program starts running it locks into the program until we find a way to exit the loop it starts.......
  • JDJD Posts: 570
    edited 2009-04-20 15:36
    Gentleman,

    Can you use the attachment manager to upload your current program so I can see exactly what changes you have made? I remember that you stated you were having issues attaching a file before; if that is still the case, can you then cut and paste your program for review?

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


    Joshua Donelson
    www.parallax.com
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-21 14:06
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    '
    [noparse][[/noparse]variables]

    irdetectleft VAR BIT 'left sensor
    irdetectright VAR BIT 'right sensor
    irdetectcenter VAR BIT 'center sensor
    BUTTON_CODE VAR BYTE 'Infrated Button Code
    DEVICE_CODE VAR BYTE 'Infrared Device Code
    button_code2 VAR BYTE 'ir button code 2
    device_code2 VAR BYTE 'ir device code 2
    '
    [noparse][[/noparse]main routine]
    '***************************
    '* MAIN PROGRAM *
    '***************************

    MAIN:

    GOSUB COMMAND_030 'Send the Remote control Command to Infrated Control Freak

    GOTO MAIN

    '
    [noparse][[/noparse]subroutines]
    COMMAND_030:

    SEROUT 14, 16468, [noparse][[/noparse]30] 'ir control freak reading remote i/o

    remote:
    SERIN 15, 16468,1000, main, [noparse][[/noparse]device_code, button_code] 'ir control freak reading remote i/o
    DEBUG "button code =", DEC Button_Code," Device Code =",DEC Device_code," ", CR
    DEBUG " ",CR
    DEBUG "____________________________",CR
    DEBUG HOME
    IF Button_code = 21 AND Device_code = 1 THEN obstacle_avoidance ' Button code 21 is power button
    IF button_code = 101 AND device_code = 1 THEN stop_bot ' Button code 101 is ok button
    IF BUTTON_code = 116 AND device_code = 1 THEN forward
    IF button_code = 117 AND device_code = 1 THEN backup
    IF button_code = 52 AND device_code = 1 THEN left
    IF button_code = 51 AND device_code = 1 THEN right
    IF button_code = 14 AND device_code = 1 THEN slight_left
    IF button_code = 96 AND device_code = 1 THEN slight_right
    IF button_code = 95 AND device_code = 1 THEN roast_em
    GOTO remote

    obstacle_avoidance:
    DO
    DEBUG HOME, "irdetectright = ", BIN1 irdetectright
    DEBUG " irdetectleft = ", BIN1 irdetectleft
    DEBUG " irdetectcenter = ", BIN1 irdetectcenter

    FREQOUT 8, 1, 38500 'ir sensor
    irdetectleft = IN9

    FREQOUT 2,1, 38500 'ir sensor
    irdetectright = IN0

    FREQOUT 3,1, 38500 'ir sensor
    irdetectcenter = IN4

    IF (irdetectcenter = 0) THEN
    GOSUB back_up
    ELSEIF (irdetectleft = 0) THEN
    GOSUB turn_right
    ELSEIF (irdetectright = 0) THEN
    GOSUB turn_left
    ELSE
    GOSUB forward_pulse
    ENDIF
    LOOP

    forward_pulse:
    PAUSE 100
    SEROUT 16, 8276, [noparse][[/noparse]"!A1E", CR] '!A and !b indicate channel on motor controller (external power amp),
    SEROUT 16, 8276, [noparse][[/noparse]"!b1E", CR] '1E is hx value (0-127) that controls speed of motors
    RETURN

    turn_left:
    SEROUT 16, 8276, [noparse][[/noparse]"!b64", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!a64", CR]
    PAUSE 750
    GOTO stop_bot

    turn_right:
    SEROUT 16, 8276, [noparse][[/noparse]"!A64", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!B64", CR]
    PAUSE 750
    GOTO stop_bot

    back_up:
    SEROUT 16, 8276, [noparse][[/noparse]"!a1E", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!B1E", CR]
    PAUSE 750
    GOTO stop_bot

    Forward:
    SEROUT 16, 8276, [noparse][[/noparse]"!A1E", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!b1E", CR]
    GOTO remote

    backup:
    SEROUT 16, 8276, [noparse][[/noparse]"!a1E", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!B1E", CR]
    GOTO remote

    left:
    SEROUT 16, 8276, [noparse][[/noparse]"!b64", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!a64", CR]
    PAUSE 750
    GOTO stop_bot

    right:
    SEROUT 16, 8276, [noparse][[/noparse]"!A64", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!B64", CR]
    PAUSE 750
    GOTO stop_bot

    slight_left:
    SEROUT 16, 8276, [noparse][[/noparse]"!b64", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!a64", CR]
    PAUSE 375
    GOTO stop_bot

    slight_right:
    SEROUT 16, 8276, [noparse][[/noparse]"!A64", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!B64", CR]
    PAUSE 375
    GOTO stop_bot

    roast_em:
    SEROUT 16, 8276, [noparse][[/noparse]"!A7F", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!B7F", CR]
    GOTO remote

    Stop_bot:
    SEROUT 16, 8276, [noparse][[/noparse]"!A00", CR]
    SEROUT 16, 8276, [noparse][[/noparse]"!B00", CR]
    GOTO remote

    this is our program as it is now....... we placed the get_button subs right before the forward pulse sub and placed the get_button at the begining of the do......loop and the placed the get_button 2: after each gosub in the do.....loop section which is what we had discussed before but it still enabled us to either go through the obs_avoidance or just stop the bot much like before so we added some commands that we could use with the remote to drive the bot. these were more or less safety features as the bot is way to high powered for what we need. so we are just stopping the bot after is mkes the maneuver to avoid the obstacle. take a look and shoot me any ideas but if all else fails we can just present our bot as is but we would really like to get this bot moving on its own and beable to stop it w the remote (which is what we have been chatting about).
  • JDJD Posts: 570
    edited 2009-04-21 16:37
    Gentleman,

    Looking at the program suggested; looks like I had a few syntax errors in it. Here is an updated program that you can try. The first one was still calling the MAIN program instead of continuing to run the program for the timeout which no doubt was giving you errors.

    GET_BUTTON2:
    SERIN 15, 16468,500,CONTINUE, [noparse][[/noparse]device_code, BUTTON_code]
    IF BUTTON_code = 101 AND device_code = 1 THEN
    GOSUB STOP_bot
    ENDIF
    CONTINUE:
    RETURN

    My apologies, without having the exact modules that you two are working with, it is a little hard to debug the program. However, you can try to see if this snippet would work where GET_BUTTON2 was placed·last time.

    Let me know if this works, or what you encounter if it does not.

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


    Joshua Donelson
    www.parallax.com
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-23 13:48
    it's not doing anything different then before but i was kind of curious to why the continue is in there. continue isn't referenced anywhere in the program so im kinda confused as to why thats in there?
  • JDJD Posts: 570
    edited 2009-04-23 18:29
    Jason & Fitz,

    The program calls the CONTINEUE: sub routine after a ½ second timeout; which is within the GET_BUTTON2 routine right before RETURN. The reason for this is to remedy the bug of starting back from the MAIN routine; which will allow the program to continue through the Avoid routine. The other routine suggested should have exhibited a restarting bug. I’m a little puzzled as to why both routines are acting in the same manor, but without the modules that you are working with it is hard to troubleshoot and debug the program.

    I hope this helps to clarify, please let me know if we can help with anything else.


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


    Joshua Donelson
    www.parallax.com
  • thefitz85thefitz85 Posts: 16
    edited 2009-04-24 14:40
    well i can tell you that we are using a platform from battlekits.com, the heavey duty base with the ampflow motor controller, and the bs2 stamp. along with that we are using the control freak light from robotmaker.co.uk. those are all the components that we are using the pinouts are in the program. thank you for your help and if you have anymore suggestions i would like to know but we have about 2 days till our project is due.
    one other thing. i placed the get_button2: routine after the loop section of the obstacle avoidance. should i place the new sub in the do loop? and still reference get button 2 in every if statement?
  • JDJD Posts: 570
    edited 2009-04-24 18:37
    The GET_BUTTON2: routine can be called within the Avoid routine without starting over; that is what the continue portion was added for in the button routine.· I tried it with a similar command style·to verify that the action of the GET_BUTTON: routine wouldn’t interrupt or reset the Avoid routine if there was no input made. I'm still not positive why it's acting in the same manor as the previous routine.

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


    Joshua Donelson
    www.parallax.com
Sign In or Register to comment.