Skip to content

Instantly share code, notes, and snippets.

@dobrienSTJ
Forked from MrDMurray/2car.py
Created December 6, 2016 10:15
Show Gist options
  • Save dobrienSTJ/17832f49d57ac644c7d35bf2b50b45af to your computer and use it in GitHub Desktop.
Save dobrienSTJ/17832f49d57ac644c7d35bf2b50b45af to your computer and use it in GitHub Desktop.
STJLOL-DMU: Controls an RC car with a Raspberry Pi
import RPi.GPIO as GPIO
from time import sleep
import readchar
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT) #reverse
GPIO.setup(5, GPIO.OUT) #forward
GPIO.setup(8, GPIO.OUT) #left
GPIO.setup(10, GPIO.OUT) #right
print(" Welcome to Motor Control v1.0")
print(" ")
print(" w ")
print(" ^ ")
print(" | r (forward) ")
print(" a <- @---@ -> d ^ ")
print(" [ ] / | \ ")
print(" [ ] | ")
print(" [ ] \ / ")
print(" @---@ f (reverse) ")
print(" ")
print(" STEERING MOVEMENT ")
print("")
print("Type 'z' to exit")
sleep(1)
print("Awaiting instructions...")
while True:
key=readchar.readkey() #reads the key you type in, no need to press enter.
if key == "r": #forward
GPIO.output(5, GPIO.HIGH)
sleep(1)
GPIO.output(5, GPIO.LOW)
elif key == "f": #reverse
GPIO.output(3, GPIO.HIGH)
sleep(1)
GPIO.output(3, GPIO.LOW)
elif key == "a": #steers left
GPIO.output(10, GPIO.LOW)
GPIO.output(8, GPIO.HIGH)
elif key == "d": #steers right
GPIO.output(8, GPIO.LOW)
GPIO.output(10, GPIO.HIGH)
elif key == "w": #steers straight
GPIO.output(8, GPIO.LOW)
GPIO.output(10, GPIO.LOW)
elif key == "z": #quits the program
GPIO.cleanup()
print("Motors on safe, exiting.")
sleep(1)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment