PDA

View Full Version : gosub and if-then working together?



bartman
- 23rd November 2005, 04:36
In the following bit of code there is a delay while waiting for a button to be pressed. If any of the buttons is pressed before the delay is up then a second routine is called to make a sound.

It works as I have written it, but I have noticed that the first loop that is looking for the button presses appears to "reset" if one is pressed and starts the counting again.

Is this normal operation when used this way? I'd just like to know. I know I could re-write this bit to remove that affect, but being still green on this I'm curious as to the why behind it.

Thanks.

Bart



errorcheck:

for loop = 1 to 4000 ' short delay once sound program is selected
getbutton = GPIO & %00110101 ' get port values and keep only ports 0,2,4,5
pause 1
if getbutton <> 0 then gosub errortone ' if a button is pressed play error tone
next
return ' return after delay and/or error routine

errortone: ' routine to produce a tone for any errors encountered

for loop = 1 to 3
pause 16
sound TRANSDUCER, [75, 5, 135, 5, 75, 2]
next loop
return

arniepj
- 23rd November 2005, 12:51
The for to next loops in both routines are using the same loop variable "loop".

bartman
- 23rd November 2005, 13:54
Darn. I was hoping to get back to this before someone else noticed!

I copied and pasted that second part from a previous version of my program and didn't change the loop name. I thought of this as I was going to sleep last night.

I'm surprised it even worked at all.

Thanks,

Bart