Dear Sparkers,
I have a coding issue, regarding a Blynk joystick widget.
After days of trying all kinds of examples and reading tutorials, I concluded this novice needs help! Or a suggestion that can point me in the right direction.
I bought a Pi Servo pHat(V2), because I want to rotate 2 servo’s within a Pan/Tilt Bracket. After reading the article description the Pi Servo seemed most suitable. I am a novice and hooking the Pi Servo to my Raspberry Pi Zero was easy. So far, I have managed to do several actions solely by myself. I already had the succes to move the servos via Python 3.7.3.
Recently, I saw a video that showed a Blynk widget that acts like a Joystick. This widget controls the 2 servos more smoothly then 2 slider widgets. Please keep in mind that I am a novice, my statements can be wrong.
After I copied the code, provided by the author, my problems began. My conclusions so far:
-
if I am correct this code is Python and not C++
-
if I am correct this code is written using certain commands that are not actual anymore
-
the current Blynk commands differ from the code I downloaded, not all of them but quite a few
-
it is confusing to see commands for the Void Loop and Virtual Write (some symbols used in Void are unneccesary in Virtual Write)
Right now I reached a point where I do not know if this code is usable or needs tweaking for further use. If I can tweak it for use in 2021 I definitely need some pointers or advice. Can someone please help me out???
Code:
BLYNK_AUTH = ‘xxxxxxxxxxxxxxxxxxx’
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()
Thanks in advance for time and effort!
Waiting in great expectation,
Raoul.