Skip to content

Instantly share code, notes, and snippets.

@Bill-Park
Created January 10, 2020 02:46
Show Gist options
  • Save Bill-Park/76dbb3ca8aa4ae7f448101a5e5bb7782 to your computer and use it in GitHub Desktop.
Save Bill-Park/76dbb3ca8aa4ae7f448101a5e5bb7782 to your computer and use it in GitHub Desktop.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO_DIR1 = 23
GPIO_PWM1 = 24
GPIO_DIR2 = 27
GPIO_PWM2 = 22
GPIO.setup(GPIO_DIR1, GPIO.OUT)
GPIO.setup(GPIO_PWM1, GPIO.OUT)
GPIO.setup(GPIO_DIR2, GPIO.OUT)
GPIO.setup(GPIO_PWM2, GPIO.OUT)
def setSpeed(speed1, speed2):
p1.ChangeDutyCycle(speed1)
p2.ChangeDutyCycle(speed2)
try:
p1 = GPIO.PWM(GPIO_PWM1, 100) # 100hz
p1.start(0) # start the PWM on 0% duty cycle
p2 = GPIO.PWM(GPIO_PWM2, 100) # 100hz
p2.start(0) # start the PWM on 0% duty cycle
while True:
GPIO.output(GPIO_DIR1, GPIO.HIGH)
GPIO.output(GPIO_DIR2, GPIO.HIGH)
setSpeed(13, 13)
print("front")
time.sleep(5)
setSpeed(0, 0)
print("stop")
time.sleep(5)
GPIO.output(GPIO_DIR1, GPIO.LOW)
GPIO.output(GPIO_DIR2, GPIO.LOW)
setSpeed(13, 13)
print("back")
time.sleep(5)
setSpeed(0, 0)
print("stop")
time.sleep(5)
GPIO.output(GPIO_DIR1, GPIO.HIGH)
GPIO.output(GPIO_DIR2, GPIO.LOW)
setSpeed(7, 7)
print("right")
time.sleep(5)
setSpeed(0, 0)
print("stop")
time.sleep(5)
GPIO.output(GPIO_DIR1, GPIO.LOW)
GPIO.output(GPIO_DIR2, GPIO.HIGH)
setSpeed(7, 7)
print("left")
time.sleep(5)
setSpeed(0, 0)
print("stop")
time.sleep(5)
finally:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment