Advertisement
qianzhe

TestCode for Puppet Show

Aug 9th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # External module imports
  2. from __future__ import division
  3. import RPi.GPIO as GPIO
  4. import time
  5.  
  6. # Import the PCA9685 module.
  7. import Adafruit_PCA9685
  8.  
  9. # GPIO Pin Definitons:
  10. butPin = 17 # Broadcom pin 17 (P1 pin 11)
  11.  
  12. # PCA9685 Pin Definitions
  13. belle_Body = 0
  14. led_white = 15
  15. led_red = 12
  16. led_green = 13
  17. led_blue = 14
  18.  
  19.  
  20. # Configure min and max servo pulse lengths
  21. servo_min = 320  # Min pulse length out of 4096
  22. servo_max = 600  # Max pulse length out of 4096
  23. led_low = 0
  24. led_high = 3072
  25.  
  26. # Pin Setup:
  27. GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
  28. GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
  29.  
  30. # Initialise the PCA9685 using the default address (0x40).
  31. pwm = Adafruit_PCA9685.PCA9685()
  32.  
  33. # NOTE: Servos work with these values:
  34. # Pulses are at a 50/60Hz interval
  35. # Pulse high vary between 1 to 2 miliseconds, i.e. centre should be 1.5ms
  36.  
  37. # Helper function to make setting a servo pulse width simpler.
  38. def set_servo_pulse(channel, pulse):
  39.     pulse_length = 1000000    # 1,000,000 us per second
  40.     pulse_length //= 60       # 60 Hz
  41.     print('{0}us per period'.format(pulse_length))
  42.     pulse_length //= 4096     # 12 bits of resolution
  43.     print('{0}us per bit'.format(pulse_length))
  44.     pulse *= 1000
  45.     pulse //= pulse_length
  46.     pwm.set_pwm(channel, 0, pulse)
  47.  
  48. # Set frequency to 60hz, good for servos.
  49. pwm.set_pwm_freq(60)
  50.  
  51.  
  52.  
  53.  
  54.  
  55. # Alternatively specify a different address and/or bus:
  56. # pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)
  57.  
  58.  
  59. print("Here we go! Press CTRL+C to exit")
  60. try:
  61.     while 1:
  62.         if GPIO.input(butPin): # button is released
  63.             pwm.set_pwm(belle_Body, 0, servo_min)
  64.             pwm.set_pwm(led_white, 0, led_low)
  65.             pwm.set_pwm(led_red, 0, led_low)
  66.             print("Go to servo min, light min")
  67.             time.sleep(3)
  68.         else: # button is pressed:
  69.             print("mowing on to second step")
  70.             pwm.set_pwm(belle_Body, 0, servo_max)
  71.             pwm.set_pwm(led_white, 0, led_high)
  72.             pwm.set_pwm(led_red, 0, led_high)
  73.             print("Go to servo max, light max")
  74.             time.sleep(3)
  75. except KeyboardInterrupt: #If CTRL+C is pressed, exit cleanly:
  76.     pwm.set_pwm(belle_Body, 0, servo_min)
  77.     pwm.set_pwm(led_white, 0, 0)
  78.     pwm.set_pwm(led_red, 0, 0)
  79.     GPIO.cleanup() # cleanup all GPIO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement