Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import RPi.GPIO as GPIO
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(17,GPIO.OUT)
- GPIO.setup(18,GPIO.OUT)
- GPIO.setup(22,GPIO.OUT)
- GPIO.setup(23,GPIO.OUT)
- def forwards():
- GPIO.output(17,0)
- GPIO.output(18,1)
- GPIO.output(22,0)
- GPIO.output(23,1)
- def backwards():
- GPIO.output(17,1)
- GPIO.output(18,0)
- GPIO.output(22,1)
- GPIO.output(23,0)
- def left():
- GPIO.output(17,0)
- GPIO.output(18,1)
- GPIO.output(22,1)
- GPIO.output(23,0)
- def right():
- GPIO.output(17,1)
- GPIO.output(18,0)
- GPIO.output(22,0)
- GPIO.output(23,1)
- def stop():
- GPIO.output(17,0)
- GPIO.output(18,0)
- GPIO.output(22,0)
- GPIO.output(23,0)
- def getch():
- import sys, tty, termios
- old_settings = termios.tcgetattr(0)
- new_settings = old_settings[:]
- new_settings[3] &= ~termios.ICANON
- try:
- termios.tcsetattr(0, termios.TCSANOW, new_settings)
- ch = sys.stdin.read(1)
- finally:
- termios.tcsetattr(0, termios.TCSANOW, old_settings)
- return ch
- def done():
- stop()
- GPIO.cleanup()
- print('\n')
- exit(1)
- print('Ready for commands (w for forwards, s for backwards,\na for rotate left, d for rotate right,\nq to quit:')
- while True:
- direction = getch()
- if direction == 'w':
- forwards()
- time.sleep(1)
- elif direction == 's':
- backwards()
- time.sleep(1)
- elif direction == 'a':
- left()
- time.sleep(0.25)
- elif direction == 'd':
- right()
- time.sleep(0.25)
- elif direction == 'q':
- done()
- else:
- print('Command not found')
- stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement