Project 3 Programming Practice
Connecting servo motors:
Code to move servos:
from servo import Servo
import time
sg90_servo = Servo(pin=26) #To be changed according to the pin used
servo2 = Servo(pin=22)
while True:
sg90_servo.move(135) # turns the servo to 0°.
servo2.move(45)
time.sleep(1)
#sg90_servo.move(90) # turns the servo to 90°.
#servo2.move(90)
#time.sleep(1)
#sg90_servo.move(180) # turns the servo to 180°.
#time.sleep(1)
Code to make servos wiggle with proximity sensor:
from machine import Pin
from servo import Servo
import utime
import time
trigger = Pin(27, Pin.OUT)
echo = Pin(28, Pin.IN)
sg90_servo = Servo(pin=26)
def ultra():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
print("The distance from object is ",distance,"cm")
return distance
while True:
#ultra()
while ultra() <= 5:
sg90_servo.move(0) # turns the servo to 0°.
time.sleep(1)
sg90_servo.move(90) # turns the servo to 90°.
time.sleep(1)
utime.sleep(1)
Comments
Post a Comment