having issues exiting loops etc.
thefitz85
Posts: 16
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
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?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
(effuzionz@yahoo.com)
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
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
' {$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).
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
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
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?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Respectfully,
Joshua Donelson
www.parallax.com