Advertisement
Guest User

Untitled

a guest
May 24th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. from gpiozero import DistanceSensor
  2. from gpiozero import Servo
  3. from time import sleep
  4. import random
  5.  
  6. sensor = DistanceSensor(echo=5, trigger=19)
  7.  
  8. def scanLeft():
  9.     myGPIO = 6
  10.     scanLeft = Servo(myGPIO)
  11.     for x in range(8):
  12.         randomLeft = random.uniform(-0.3, 0.3)
  13.         sleep(0.005)
  14.         scanLeft.value = randomLeft
  15.         print(scanLeft.value)
  16.  
  17. def scanRight():
  18.     myGPIO = 13
  19.     scanRight = Servo(myGPIO)
  20.     for x in range(8):
  21.         randomRight = random.uniform(-0.3, 0.3)
  22.         sleep(0.005)
  23.         scanRight.value = randomRight
  24.         print(scanRight.value)
  25.  
  26.  
  27. def leftForward():
  28.     myGPIO=6
  29.     myCar=Servo(myGPIO)
  30.     for x in range(10):
  31.         sleep(0.005)
  32.         myCar.value = 1
  33.  
  34. def rightForward():
  35.     myGPIO=13
  36.     myCar=Servo(myGPIO)
  37.     for x in range(10):
  38.         sleep(0.005)
  39.         myCar.value = 1
  40.  
  41. def backward():
  42.     myGPIO=6
  43.     myGPIO1=13
  44.  
  45.     myCar=Servo(myGPIO)
  46.     myCar1=Servo(myGPIO1)
  47.  
  48.     for x in range(4):
  49.         sleep(0.5)
  50.         myCar.value=-0.4
  51.         myCar1.value=-0.4
  52. while True:
  53.     sleep(0.01)
  54.     distance = sensor.distance * 100
  55.     if distance <= 20 and distance > 6:
  56.         leftForward()
  57.         distance = sensor.distance * 100
  58.         rightForward()
  59.     elif distance <= 5:
  60.         backward()
  61.  
  62.     elif distance > 20:
  63.         scanLeft()
  64.         distance = sensor.distance * 100
  65.         scanRight()
  66.     distance = sensor.distance * 100
  67.  
  68.     print(distance)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement