'Define what microcontroller we are using
#chip mega328p,16

'Define connected pins
#DEFINE sensor1 PORTC.0  'Arduino analog pin 0
#DEFINE sensor2 PORTC.2  'Arduino analog pin 2
#DEFINE sensor3 PORTC.1  'Arduino analog pin 1

#DEFINE lmotor PORTB.2   'Arduino digital pin 10
#DEFINE rmotor PORTB.3   'Arduino digital pin 11

'dir - sets pin direction - input/output
dir sensor1 in           'Set Arduino analog pin 0 as input
dir sensor2 in
dir sensor3 in

dir lmotor out           'Set Arduino digital pin 10 as output
dir rmotor out

'Open main loop
start:

  'Add variables
  state1 = 0
  state2 = 0
  state3 = 0

  'Read sensor states
  If sensor1 = 1 Then state1 = 1
  If sensor2 = 1 Then state2 = 1
  If sensor3 = 1 Then state3 = 1

  'Decide where to go
  If state1 = 1 & state2 = 1 & state3 = 1 Then DriveForward
  If state1 = 1 & state2 = 1 & state3 = 0 Then DriveLeft
  If state2 = 1 & state3 = 1 & state1 = 0 Then DriveRight
  If state1 = 1 & state2 = 0 & state3 = 0 Then DriveLeft
  If state3 = 1 & state2 = 0 & state1 = 0 Then DriveRight
  If state2 = 1 & state1 = 0 & state3 = 0 Then DriveForward
  If state1 = 0 & state2 = 0 & state3 = 0 Then
    If last = 0 Then DriveLeft
    If last = 1 Then DriveRight
  End If

'Go to the beginning of loop
GoTo start

'Define moves
Sub DriveForward
  lmotor = 1
  rmotor = 1
End Sub

Sub DriveLeft
  rmotor = 1
  lmotor = 0
  last = 0
End Sub

Sub DriveRight
  lmotor = 1
  rmotor = 0
  last = 1
End Sub
