Copyright © Parallax Inc.
Infrared Line Follower Kit (#28034)
v1.0 1/18/2011 Page 9 of 9
Stingray Robot Line Following Code
The line following code included below is ideal for longer courses with wide-radius turns, and is
compatible with the circuit shown in Figure 1.
This code was written to be used as a skeleton for higher-precision line following applications using PID
and/or other control algorithms.
'' InfraredLineFollower_Stingray.spin
'' Simple line following code compatible with the Parallax Stingray Robot, best used
'' on longer courses with wide-radius turns
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
MotorA = 25 ' Motor A connected to P25
MotorB = 26 ' Motor B connected to P26
VAR
byte DutyCycleA, DutyCycleB, state
OBJ
PWM : "PWM_32_V2.spin"
PUB Main
PWM.Start ' Start PWM object
dira[7..0]~ ' Set pins 7..0 as input
repeat
state := ina[7..0] ' Set input states to variable state
case state
%00000011, %00000001, %00000010: ' Pivot left
DutyCycleA := 65
DutyCycleB := 0
%00000111, %00000110, %00000100: ' Slight left
DutyCycleA := 60
DutyCycleB := 0
%00001110, %00001100: ' Adjust left
DutyCycleA := 55
DutyCycleB := 0
%00011100, %00011000, %00111000: ' Straight
DutyCycleA := DutyCycleB := 50
%01110000, %00110000: ' Adjust right
DutyCycleA := 0
DutyCycleB := 55
%11100000, %01100000, %0010000: ' Slight right
DutyCycleA := 0
DutyCycleB := 60
%11000000, %01000000, %10000000: ' Pivot right
DutyCycleA := 0
DutyCycleB := 65
other: ' Anything else, slowly move forward
DutyCycleA := DutyCycleB := 20
PWM.Duty(MotorA, DutyCycleA, 50) ' Adjust motor speeds
PWM.Duty(MotorB, DutyCycleB, 50)