Last active
December 6, 2016 10:15
-
-
Save MrDMurray/1b5cfa88f6d9dc441af49f584e4fb053 to your computer and use it in GitHub Desktop.
STJLOL-DMU: Controls an RC car with a Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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