BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Python, how to retrieve a slider value in a variable

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

@Jumbo please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Perhaps rethink your coding to use the existing variable “value” in your servo command instead of trying to make/transfer the data to a new variable… or change “value” to whatever variable name you need instead.

Thanks for your answer, but if I try to use ‘value’ instead of ‘pos’,
I get the same message: name ‘value’ is not defined.
There should be another solution.

Your issue is not Blynk specific. The solution is learning Python… either declare the global variables used (in the correct location) or move your servo command within the valid function of the variable.

https://www.w3schools.com/python/python_variables.asp
https://www.w3schools.com/python/gloss_python_global_variables.asp

That’s it!
I had to declare the variable ‘pos = 150’ in the upper part.
within the function I had to write ’ global pos’

now it’s working as desired, setting the servo at min position 150 at startup.

Thank you very much. Problem solved!

1 Like