Connecting Pi servo HAT(2) to Blynk

Fellow Blynkers,

I am struggling with a Python code.

After learning about Raspberry (hardware, install etc.) I managed to make a headless connection and attach servos to my Sparkfun Pi Servo Phat(2). Then I spend a lott of time learning Python so that I can move the servos on command. I bought a pan/tilt bracket that uses 2 servos.

Recently, I came acros Blynk. It is a very cool tool! I managed to make simple commands between the app and my configuration.

But, now that I saw a video I am stuck. https://stemship.com/raspberry-pi-camera-control/

So far I could learn the basics / tutorials to enhance my knowledge. The video shows how somebody uses an joystick button to move the bracket (included 2 servos) around. The joystick code is totally different from what I worked with so far. Right know I am even struggling with the standard writings of the code. It has stressed me out because it seems that this script was written by someone with extensive knowledge of Python.

Code:


from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)

angleX = 0
angleY = 0
kit.servo[3].angle = 90
kit.servo[2].angle = 90

import BlynkLib
from BlynkTimer import BlynkTimer

blynk = BlynkLib.Blynk(BLYNK_AUTH)
timer = BlynkTimer()

def limit(value, lower, upper):
    if value < lower:
        value = lower
    elif value > upper:
        value = upper
    return value

@blynk.on("V1")     # Joystick
def v1_write_handler(value):
    global joystick_mode, angleX, angleY
    joystickX = int(value[0])
    joystickY = int(value[1])

    # PanTilt
    if   joystickX >  30: angleX =  4
    elif joystickX < -30: angleX = -4
    else:                 angleX =  0
    if   joystickY >  30: angleY =  4
    elif joystickY < -30: angleY = -4
    else:                 angleY =  0
    print("angle {} {} ".format(angleX,angleY))

def panTiltMove():
    global angleX, angleY
    kit.servo[3].angle = limit(kit.servo[3].angle - angleX, 45, 135)
    kit.servo[2].angle = limit(kit.servo[2].angle - angleY, 45, 135)
 
# Define Timers
timer.set_interval(0.1, panTiltMove)    # 100ms
 
while True:
    blynk.run()
    timer.run()```

Can someone please have a look and help / guide me in the right direction? I keep on getting a error after: @blynk.on("V1")     # Joystick. Error> @Blynk.on("V1) , TypeError: on() missing 1 required positional argument: (func). So far, I have not encountered this error before. I do not know where to start...

Extra> I also found this code, but this looks even more complicated. However, this code seems to add a active control for activating the virtual pins.

```BLYNK_AUTH = 'YourAuthToken'
 
from board import SCL, SDA
import busio
from adafruit_pca9685 import PCA9685
i2c_bus = busio.I2C(SCL, SDA)
pca = PCA9685(i2c_bus)
pca.frequency = 60
 
from adafruit_servokit import ServoKit
kit = ServoKit(channels=8)
 
joystick_mode = 0   # 0:Motor 1:PanTilt
angleX = 0
angleY = 0
 
import BlynkLib
from BlynkTimer import BlynkTimer
import os
import math
 
blynk = BlynkLib.Blynk(BLYNK_AUTH)
timer = BlynkTimer()
     
def limit(value, lower, upper):
    if value < lower:
        value = lower
    elif value > upper:
        value = upper
    return value
 
@blynk.on("connected")
def blynk_connected():
    blynk.sync_virtual(2, 4)
    blynk.virtual_write(3, 1)
 
@blynk.on("V1")     # Joystick
def v1_write_handler(value):
    global joystick_mode, angleX, angleY
    joystickX = int(value[0])
    joystickY = int(value[1])
    # PanTilt
    if joystick_mode:
        if   joystickX >  30: angleX =  4
        elif joystickX < -30: angleX = -4
        else:                 angleX =  0
        if   joystickY >  30: angleY =  4
        elif joystickY < -30: angleY = -4
        else:                 angleY =  0
    # Motor
    else:
        joystickY = joystickY * 0.7
        if joystickY >= 0:
            if joystickX >= 0:
                throttleL = joystickY + joystickX * 0.3
                throttleR = -joystickY + joystickX
            else:
                throttleL = joystickY + joystickX
                throttleR = -joystickY + joystickX * 0.3
        elif joystickX >= 0:
            throttleL = joystickY - joystickX * 0.3
            throttleR = -joystickY - joystickX
        else:
            throttleL = joystickY - joystickX
            throttleR = -joystickY - joystickX * 0.3
        kit.continuous_servo[0].throttle = throttleL / 100
        kit.continuous_servo[1].throttle = throttleR / 100
 
@blynk.on("V2")     # Motor and Pan/Tilt switch button
def v2_write_handler(value):
    global joystick_mode
    joystick_mode = int(value[0])
    if joystick_mode == 0:  # Motor
        kit.servo[6].angle = 90
        kit.servo[7].angle = 110
 
@blynk.on("V3")     # Power button
def v3_write_handler(value):
    if int(value[0]) == 0:  # Off
        os.system('sudo shutdown -h now')
 
@blynk.on("V4")     # LED ON/OFF button
def v4_write_handler(value):
    if int(value[0]):       # ON
        pca.channels[14].duty_cycle = 0xffff
        pca.channels[15].duty_cycle = 0xffff
    else:                   # OFF
        pca.channels[14].duty_cycle = 0x0000
        pca.channels[15].duty_cycle = 0x0000
 
def panTiltMove():
    global angleX, angleY
    kit.servo[6].angle = limit(kit.servo[6].angle - angleX, 0, 180)
    kit.servo[7].angle = limit(kit.servo[7].angle - angleY, 0, 180)
 
# Define Timers
timer.set_interval(0.1, panTiltMove)    # 100ms
 
while True:
    blynk.run()
    timer.run()```

This code gives me the same error after: @blynk.on("connected")

I think it has somethin to do with @Blynk.on. in both scripts.

In great expectation!!!

@Pimove 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.

@Pimove you have edited your post, but despite me giving you triple backticks that you could copy/paste, you’ve decided to use different characters, which don’t work.

Please edit your post and use the correct characters, otherwise your unformatted code will be deleted.

Pete.