Hello…
I’m new to the community and asking for help:
I’m running Blynk app on my Android phone and want to control several servos
via a Raspberry Pi3 and a PCA 9685 board.
I’m using a Python script which works up to a certain point:
The slider values from 150 up to 600 are displayed in the terminal window of the Pi as desired.
Now I want to use these values as a variable to move the servo accordingly.
My script looks like this:
…
from __future__ import division
import time
import board
import busio
import blynklib
import Adafruit_PCA9685
# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()
BLYNK_AUTH = 'siBbOB2iBrQ_wiTidWC5dAt6lG0ahVY9'
# initialize Blynk
blynk = blynklib.Blynk(BLYNK_AUTH)
# Configure min and max servo pulse lengths
servo_min = 150 # Min pulse length out of 4096
servo_max = 600 # Max pulse length out of 4096
servo_middle = 375
# 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)
pwm.set_pwm(channel, 1, pulse)
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
# Register virtual pin handler
WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"
@blynk.handle_event('write V2')
def v2_write_handler(pin, value):
print(WRITE_EVENT_PRINT_MSG.format(pin, value))
pos = int(format(value[0]))
###########################################################
# infinite loop that waits for event
###########################################################
while True:
blynk.run()
pwm.set_pwm(0, 0, pos)
…
It does not take the variable “pos”. (Message: pos is nor defined)
If I write: pwm.set(0, 0, servo_min) the script is working.
I don’t know, how to get the correct syntax to make it running.
Thanks a lot for help