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
*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).
*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.
*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.