Light Follower Robot
by tomas_17121
Mikrobasic Pro Code
program robot 0.1
dim current_duty, current_duty1, old_duty, old_duty1,oldstate as byte
dim current_duty2 as byte
dim adc1,adc2 as word
dim a as integer
sub procedure goright
portb.7=0
portb.6=1
current_duty = 250 ' initial value for current_duty
current_duty2 = 250
PWM1_Start()
PWM2_Start() ' start PWM1
PWM1_Set_Duty(current_duty) ' Set current duty for PWM1
PWM2_Set_Duty(current_duty2)
PORTB.1=0
PORTB.2=1
PORTB.3=1
PORTB.4=0
end sub
sub procedure goleft
portb.6=1
portb.7=0
current_duty = 250 ' initial value for current_duty
current_duty2 = 250
PWM1_Start()
PWM2_Start() ' start PWM1
PWM1_Set_Duty(current_duty) ' Set current duty for PWM1
PWM2_Set_Duty(current_duty2)
PORTB.1=1
PORTB.2=0
PORTB.3=0
PORTB.4=1
end sub
sub procedure gostr
portb.7=1
portb.6=0
current_duty = 190 ' initial value for current_duty
current_duty2 = 190
PWM1_Start()
PWM2_Start() ' start PWM1
PWM1_Set_Duty(current_duty) ' Set current duty for PWM1
PWM2_Set_Duty(current_duty2)
PORTB.1=0
PORTB.2=1
PORTB.3=0
PORTB.4=1
end sub
sub procedure InitMain()
CCP1CON =0 ''''''''''''''''''''
adcon0=0
ADRESH=0
ADRESL=0
'PORTB = 255
' TRISB = 255 ' configure PORTA pins as input
TRISA=0xFF
TRISB=0
PORTB=0 ' designate PORTB pins as output
PORTC = 0 ' set PORTC to 0
TRISC = 0 '
ADCON1 = 0xC2'''''''''''''''''''''
PWM1_Init(5000)
PWM2_Init(5000)
end sub
main:
InitMain()
current_duty = 50 ' initial value for current_duty
current_duty2 = 50
PWM1_Start() ' start PWM1
PWM1_Set_Duty(current_duty) ' Set current duty for PWM1
PWM2_Start() ' start PWM2
PWM2_Set_Duty(current_duty2) ' Set current duty for PWM2
delay_ms(2000)
while true
adc1= ADC_Read(0)
adc2=ADC_Read(1)
a = adc1-adc2
if (a< 40) and (-40<a) then gostr
else if adc1>adc2 then goright
else if adc1<adc2 then goleft
end if
end if
end if
wend
end.