import time
from Motor import *
from ADC import *
class Light:
def run(self):
try:
self.adc=Adc()
self.PWM=Motor()
self.PWM.setMotorModel(0,0,0,0)
while True:
L = self.adc.recvADC(0)
R = self.adc.recvADC(1)
if L < 2.99 and R < 2.99 :
self.PWM.setMotorModel(600,600,600,600)
elif abs(L-R)<0.15:
self.PWM.setMotorModel(0,0,0,0)
elif L > 3 or R > 3:
if L > R :
self.PWM.setMotorModel(-1200,-1200,1400,1400)
elif R > L :
self.PWM.setMotorModel(1400,1400,-1200,-1200)
except KeyboardInterrupt:
led_Car.PWM.setMotorModel(0,0,0,0)
if __name__=='__main__':
print ('Program is starting ... ')
led_Car=Light()
led_Car.run()
Result analysis
When the voltages of left and right photoresistor are less than 2.99V, the car move forward straightly. And
when one of the voltages is greater than 3V:
If the left voltage is greater than the right, the car turns left.
If the right voltage is greater than the left, the car turns right.
You can change the judgment of the program to achieve the result you want, according to the light intensity
of the environment.