Advertisement
sweenig

PiBorg (python3)

Jul 2nd, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. import time
  2. import RPi.GPIO as GPIO
  3.  
  4. GPIO.setmode(GPIO.BCM)
  5. GPIO.setwarnings(False)
  6.  
  7. GPIO.setup(17,GPIO.OUT)
  8. GPIO.setup(18,GPIO.OUT)
  9. GPIO.setup(22,GPIO.OUT)
  10. GPIO.setup(23,GPIO.OUT)
  11.  
  12. def forwards():
  13.         GPIO.output(17,0)
  14.         GPIO.output(18,1)
  15.         GPIO.output(22,0)
  16.         GPIO.output(23,1)
  17. def backwards():
  18.         GPIO.output(17,1)
  19.         GPIO.output(18,0)
  20.         GPIO.output(22,1)
  21.         GPIO.output(23,0)
  22. def left():
  23.         GPIO.output(17,0)
  24.         GPIO.output(18,1)
  25.         GPIO.output(22,1)
  26.         GPIO.output(23,0)
  27. def right():
  28.         GPIO.output(17,1)
  29.         GPIO.output(18,0)
  30.         GPIO.output(22,0)
  31.         GPIO.output(23,1)
  32. def stop():
  33.         GPIO.output(17,0)
  34.         GPIO.output(18,0)
  35.         GPIO.output(22,0)
  36.         GPIO.output(23,0)
  37.  
  38. def getch():
  39.   import sys, tty, termios
  40.   old_settings = termios.tcgetattr(0)
  41.   new_settings = old_settings[:]
  42.   new_settings[3] &= ~termios.ICANON
  43.   try:
  44.     termios.tcsetattr(0, termios.TCSANOW, new_settings)
  45.     ch = sys.stdin.read(1)
  46.   finally:
  47.     termios.tcsetattr(0, termios.TCSANOW, old_settings)
  48.   return ch
  49.  
  50. def done():
  51.     stop()
  52.     GPIO.cleanup()
  53.     print('\n')
  54.     exit(1)
  55.  
  56. print('Ready for commands (w for forwards, s for backwards,\na for rotate left, d for rotate right,\nq to quit:')
  57. while True:
  58.     direction = getch()
  59.     if direction == 'w':
  60.         forwards()
  61.         time.sleep(1)
  62.     elif direction == 's':
  63.         backwards()
  64.         time.sleep(1)
  65.     elif direction == 'a':
  66.         left()
  67.         time.sleep(0.25)
  68.     elif direction == 'd':
  69.         right()
  70.         time.sleep(0.25)
  71.     elif direction == 'q':
  72.         done()
  73.     else:
  74.         print('Command not found')
  75.     stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement