jodicalhon
New Member
My little two-wheeled robot is now controlled by a TV remote controller, using infrain2. Depending on the button pushed, the program chooses a subroutine for turn left, turn right, go forward, or reverse. So far so good!
I thought it would be fun to have it perform a sequence from one button push, which leads me to my question.
Upon receipt of IR the program chooses a subroutine by if...then statements. Perhaps some code would help:
<code><pre><font size=2 face='Courier'>
loop:
infrain2
if infra = 0 then left
if infra = 1 then fwd
if infra = 2 then right
if infra = 4 then rverse
if infra = 8 then sequence
'else allstop
allstop:
high 0 'disables motors
goto loop
left:
low 2 'right motor forward
high 4 'left motor reverse
low 0 'enable motors
goto loop
</font></pre></code>
And so on for the other subroutines.
The sequence subroutine would go something like:
sequence:
gosub left
pause 1000
gosub fwd
pause 1500
etc, etc
But the 'gosubs' need a 'return' in the subroutine they point to. Whereas the if/then statements do not. D'you see what I mean?
My solution has been to have the if/then statement point to a subroutine which contains a gosub to the wanted subroutine. The wanted subroutine can then contain a 'return', and so can be used easily by my 'sequence' subroutine. Like so:
infrain2
if infra = 0 then zero
. . . . . . . .
zero:
gosub left
goto loop
left:
whatever my motors need to do!
return
Is there a better way to handle this problem?
Thanks for any help.
I thought it would be fun to have it perform a sequence from one button push, which leads me to my question.
Upon receipt of IR the program chooses a subroutine by if...then statements. Perhaps some code would help:
<code><pre><font size=2 face='Courier'>
loop:
infrain2
if infra = 0 then left
if infra = 1 then fwd
if infra = 2 then right
if infra = 4 then rverse
if infra = 8 then sequence
'else allstop
allstop:
high 0 'disables motors
goto loop
left:
low 2 'right motor forward
high 4 'left motor reverse
low 0 'enable motors
goto loop
</font></pre></code>
And so on for the other subroutines.
The sequence subroutine would go something like:
sequence:
gosub left
pause 1000
gosub fwd
pause 1500
etc, etc
But the 'gosubs' need a 'return' in the subroutine they point to. Whereas the if/then statements do not. D'you see what I mean?
My solution has been to have the if/then statement point to a subroutine which contains a gosub to the wanted subroutine. The wanted subroutine can then contain a 'return', and so can be used easily by my 'sequence' subroutine. Like so:
infrain2
if infra = 0 then zero
. . . . . . . .
zero:
gosub left
goto loop
left:
whatever my motors need to do!
return
Is there a better way to handle this problem?
Thanks for any help.