This code would be better written:
Code:
'----------- Another Feeble Attempt at Autonomy---------
'Declare Variables
auto_mode VAR bit
desired_on VAR byte
motor_fwd VAR byte
motor_stop1 VAR byte
motor_left VAR byte
motor_stop2 VAR byte
motor_rev VAR byte
motor_right VAR byte
'--init variables--
motor_fwd = 0
motor_stop1 = 0
motor_left = 0
auto_mode=1
loop:
'Check autonomous mode
if auto_mode = 0 then skip_auto:
'Program 1:
if motor_fwd>200 then next_step1:
p1_y=200
p2_y=200
motor_fwd=motor_fwd+1
goto output
next_step1:
'Program 2:
if motor_stop1>200 then next_step2:
p1_y=127
p2_y=127
motor_stop1=motor_stop1+1
goto output
next_step2:
'Program 3:
if motor_left>200 then next_step3:
p1_y=127
p2_y=200
motor_left=motor_left+1
goto output
next_step3:
skip_auto:
'regular control code goes here
output:
Serout USERCPU, OUTBAUD, [255,255,p1_y,relayA,p2_y]
goto loop
First, in the previous code, in each cycle it would set the motor forward, serout, set it stop, serout, set it left, serout... there was no breaks in the code.
Second, only one serout is better programming practice.
Third, changed the 300s to 200s. This means that they are actually achieveable numbers in PBASIC. If you need to go higher, I would suggenst somthing like the following:
Code:
CountNumber VAR bit
motor_fwd VAR byte
CountNumber = 0
motor_fwd = 0
'Program 1:
if motor_fwd > 254 and CountNumber = 1 then next_step1:
p1_y=200
p2_y=200
motor_fwd=motor_fwd+1
if motor_fwd > 254 and CountNumber = 0 then CountNumber = 1
goto output
next_step1: