Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   loops inside of subs??? (http://www.chiefdelphi.com/forums/showthread.php?t=18237)

manodrum 19-02-2003 18:31

loops inside of subs???
 
hi, i have written a little code that will fire our pistons for approx. 3 seconds when one of the joystick buttons is pushed, so I wrote an if statement after the SERIN that

"if p2_sw_trig = 1 then GOSUB FIRE3"

that works and sends it to my sub code which looks like this:


FIRE3:
relay1_fwd = 1
MyCounter VAR byte
StepSize VAR byte

MyCounter = 1

FOR MyCounter = 1 TO 3001 STEP StepSize
StepSize = delta_t * 25
NEXT

relay1_fwd = 0
RETURN


but once i push the trigger, i get a basic run error, but, when i take away the FOR...NEXT statements, everything works, what's up with this? can i not do loops inside of subs? thanks a lot

Stephen

Joel J 19-02-2003 18:34

Re: loops inside of subs???
 
Quote:

Originally posted by manodrum
hi, i have written a little code that will fire our pistons for approx. 3 seconds when one of the joystick buttons is pushed, so I wrote an if statement after the SERIN that

"if p2_sw_trig = 1 then GOSUB FIRE3"

that works and sends it to my sub code which looks like this:


FIRE3:
relay1_fwd = 1
MyCounter VAR byte
StepSize VAR byte

MyCounter = 1

FOR MyCounter = 1 TO 3001 STEP StepSize
StepSize = delta_t * 25
NEXT

relay1_fwd = 0
RETURN


but once i push the trigger, i get a basic run error, but, when i take away the FOR...NEXT statements, everything works, what's up with this? can i not do loops inside of subs? thanks a lot

Stephen

MyCounter is declared as a byte. A byte can store a number no larger than 254. After that it loops around to a negative number (an illusion).

rbayer 19-02-2003 19:59

Re: Re: loops inside of subs???
 
Quote:

Originally posted by Joel J.
MyCounter is declared as a byte. A byte can store a number no larger than 254. After that it loops around to a negative number (an illusion).
While true, that is not the problem here. The problem is that the controllers need a SEROUT at least once every 125ms (that's once every five normal loops) in order to function properly. During your 3000-loop for structure, no data is sent or received from the rest of the RC causing it to die miserably. You need to find some way of putting a SEROUT in there somewhere.

ChrisA 19-02-2003 20:03

Re: Re: loops inside of subs???
 
Quote:

Originally posted by Joel J.
A byte can store a number no larger than 254. After that it loops around to a negative number (an illusion).
Unfortunately this is not the case. It can hold a number between 0 and 255 (256 values). If you have 255 and add one to it you will get 0. When you add 1 to 1 in binary you get 10. The 0 appears and the 1 is carried over to the next slot. So when you have 11111111 and you add 1 to it all 8 values turn to 0 and the 1 is carried over into the next available space. In this case there is no space left so it is simply dropped leaving you with 00000000.

[edit]As previously stated, MyCounter is a byte which can never reach 3001. As hard as the processor tries, it can never continuously add numbers to a byte and reach 3001. The byte will reach 255 and then drop back to 0 and then repeat which causes an infinite loop. there is no problem with running that loop where you have it. Try using a word (2 bytes stuck together) instead of a byte. If what rbayer says is true then you will never be able to do a loop for that long anyways, but I don't think there is any harm in trying it.[/edit]

redbeard0531 19-02-2003 20:05

<edit>Removed stuff covered by people who replied faster that me.</edit>

Here is what I suggest.

GoSub Fire3:

Fire3:

count Var word 'Number of 26ms periods
Fire3On Var Bit ' 1 if the piston should fire

If p2_sw_trig = 1 then Fire3On = 1

If Fire3On = 0 Then DontFire:

If count < 3000 then
relay1_fwd = 1
count = count + 1 + delta_t
Else
relay1_fwd = 0
count = 0
Fire3On = 0
EndIf

DontFire:

Return

That is how I would do it, but it may not be exactly what you need.


All times are GMT -5. The time now is 03:59.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi