Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # External module imports
- from __future__ import division
- import RPi.GPIO as GPIO
- import time
- # Import the PCA9685 module.
- import Adafruit_PCA9685
- # GPIO Pin Definitons:
- butPin = 17 # Broadcom pin 17 (P1 pin 11)
- # PCA9685 Pin Definitions
- belle_Body = 0
- led_white = 15
- led_red = 12
- led_green = 13
- led_blue = 14
- # Configure min and max servo pulse lengths
- servo_min = 320 # Min pulse length out of 4096
- servo_max = 600 # Max pulse length out of 4096
- led_low = 0
- led_high = 3072
- # Pin Setup:
- GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
- GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up
- # Initialise the PCA9685 using the default address (0x40).
- pwm = Adafruit_PCA9685.PCA9685()
- # NOTE: Servos work with these values:
- # Pulses are at a 50/60Hz interval
- # Pulse high vary between 1 to 2 miliseconds, i.e. centre should be 1.5ms
- # Helper function to make setting a servo pulse width simpler.
- def set_servo_pulse(channel, pulse):
- pulse_length = 1000000 # 1,000,000 us per second
- pulse_length //= 60 # 60 Hz
- print('{0}us per period'.format(pulse_length))
- pulse_length //= 4096 # 12 bits of resolution
- print('{0}us per bit'.format(pulse_length))
- pulse *= 1000
- pulse //= pulse_length
- pwm.set_pwm(channel, 0, pulse)
- # Set frequency to 60hz, good for servos.
- pwm.set_pwm_freq(60)
- # Alternatively specify a different address and/or bus:
- # pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)
- print("Here we go! Press CTRL+C to exit")
- try:
- while 1:
- if GPIO.input(butPin): # button is released
- pwm.set_pwm(belle_Body, 0, servo_min)
- pwm.set_pwm(led_white, 0, led_low)
- pwm.set_pwm(led_red, 0, led_low)
- print("Go to servo min, light min")
- time.sleep(3)
- else: # button is pressed:
- print("mowing on to second step")
- pwm.set_pwm(belle_Body, 0, servo_max)
- pwm.set_pwm(led_white, 0, led_high)
- pwm.set_pwm(led_red, 0, led_high)
- print("Go to servo max, light max")
- time.sleep(3)
- except KeyboardInterrupt: #If CTRL+C is pressed, exit cleanly:
- pwm.set_pwm(belle_Body, 0, servo_min)
- pwm.set_pwm(led_white, 0, 0)
- pwm.set_pwm(led_red, 0, 0)
- GPIO.cleanup() # cleanup all GPIO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement