Andrés Briano
Member
To be honest, I came here looking for ways to improve my rudimentary line following code. I went over the 80 posts here and there is not one example for this category. Considering that there is always someone out there who is a couple of steps behind you, I thought I´d post my code as a first approach to this common task. I welcome criticism and improvements.
Cheers,
Andrés
PS: You will find that the texts displayed on the LCD are in my native Spanish. However you can find a translation on the comments.
Cheers,
Andrés
PS: You will find that the texts displayed on the LCD are in my native Spanish. However you can find a translation on the comments.
Code:
#picaxe 18x
init:
setfreq m4
high 3
serout 3,t2400_4,(254,1) ' blank the screen
pause 30 ' short delay to enable blank to complete
symbol CEN_LDR = b5
symbol LEF_LDR = b6
symbol RIG_LDR = b7
symbol LEFT_MOTOR_FORWARD = pin7
symbol RIGHT_MOTOR_FORWARD = pin5
serout 3,t2400_4,(254,128,"Saludos,") ' top line message "Greetings,"
serout 3,t2400_4,(254,192,"terricolas!") ' bottom line message "earthlings!"
pause 4000
serout 3,t2400_4,(254,1) ' blank the screen
pause 30 ' short delay to enable blank to complete
calib:
serout 3,t2400_4,(254,192,"...calibrando...") ' bottom line message "calibrating"
readadc 0,b0
readadc 1,b1
readadc 2,b2
if b0<220 or b1<220 or b2<220 then calib ; repeat calib routine if robot is not placed over white surface
let b10=255-b0 ' b10, b11 and b12 hold a value that compensates the differences between the 3 particular LDRs
let b11=255-b1
let b12=255-b2
pause 250
serout 3,t2400_4,(254,1) ' blank the screen
pause 30 ' short delay to enable blank to complete
main:
readadc 0,b0
readadc 1,b1
readadc 2,b2
let b5=b0+b10
let b6=b1+b11
let b7=b2+b12
serout 3,T2400_4,(254,128,"Acercar a linea") ' displays "place over line" on the first line
serout 3,T2400_4,(254,192,"I=",#LEF_LDR,"C=",#CEN_LDR,"D=",#RIG_LDR) ' displays the corrected values of the 3 LDRs on the second line
if b5<=b6 and b5<=b7 then cen ' compares values and calls on the correct subroutine
if b6<=b5 and b6<=b7 then izq
if b7<=b6 and b7<=b5 then der
goto main
izq:
serout 3,T2400_4,(254,128,"Virando a IZQ") ' displays "correcting left" on the first line
low LEFT_MOTOR_FORWARD
high RIGHT_MOTOR_FORWARD
goto main
cen:
serout 3,T2400_4,(254,128,"Recto ") ' displays "straight ahead" on the first line
high LEFT_MOTOR_FORWARD
high RIGHT_MOTOR_FORWARD
goto main
der:
serout 3,T2400_4,(254,128,"Virando a DER") ' displays "correcting right" on the first line
high LEFT_MOTOR_FORWARD
low RIGHT_MOTOR_FORWARD
goto main
Last edited: