-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_Light_Searching.py
43 lines (38 loc) · 1.17 KB
/
03_Light_Searching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'''
Simple Light Searching Robot using only ONE Light Dependent Resistor (LDR)
'''
import time
import board
import analogio
import digitalio
import pwmio
from adafruit_motor import motor
# Left Motor
PWM_M1A = board.GP8
PWM_M1B = board.GP9
# Right Motor
PWM_M2A = board.GP10
PWM_M2B = board.GP11
# DC motor setup
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
# cases. A capacitor can be used to help prevent this.
pwm_1a = pwmio.PWMOut(PWM_M1A, frequency=10000)
pwm_1b = pwmio.PWMOut(PWM_M1B, frequency=10000)
motorL = motor.DCMotor(pwm_1a, pwm_1b)
pwm_2a = pwmio.PWMOut(PWM_M2A, frequency=10000)
pwm_2b = pwmio.PWMOut(PWM_M2B, frequency=10000)
motorR = motor.DCMotor(pwm_2a, pwm_2b)
ldr = analogio.AnalogIn(board.GP27)
def Robot_Movement(sL, sR):
motorL.throttle = sL
motorR.throttle = sR
while True:
raw = ldr.value
print("raw = {:5d}".format(raw))
time.sleep(0.1)
if (raw < 15000): # << Please changed HERE
Robot_Movement(0.5, 0.53) # Forward
print("Move Forward")
else:
Robot_Movement(0.1, 0.33) # Turn Left
print("Turn Left")