trying to get values from a JoyStick Module with merged output.
I’m very new to python, and I had similar code working on Arduino (Wemos D1 Mini actually)
The following code works, but only displays one value, and I need to get both into the JoyX and JoyY variables, but I’m drawing a blank on how to get the split values.
I don’t think there is any merge option in the current Python library, at least nothing I can interpret in BlynkLib.py
Simple solution is just to NOT use merge
@blynk.VIRTUAL_WRITE(3) # Get Joystick X axis on V3
def my_write_handler(value):
blynk.virtual_write(4,value) # Show X Axis on Display on V4
@blynk.VIRTUAL_WRITE(5) # Get Joystick Y axis on V5
def my_write_handler(value):
blynk.virtual_write(6,value) # Show Y Axis on Display on V6
And to further use the X & Y values in a global variable capacity, this seems to work…
JoyX = 0 # global variable
JoyY = 0 # global variable
@blynk.VIRTUAL_WRITE(3) # Joystick on V3
def my_write_handler(value):
global JoyX # use global variable
JoyX = value # Transfer value to Global variable JoyX
blynk.virtual_write(4,value) # Print int value of JoyX to display on V4
@blynk.VIRTUAL_WRITE(5) # Joystick on V5
def my_write_handler(value):
global JoyY # use global variable
JoyY = value # Transfer value to Global variable JoyY
blynk.virtual_write(6,value) # Print int value of JoyY to display on V6
def joy_display(): # Display both Global Joystick values as String on V7
blynk.virtual_write(7, "Joystick X = " + str(JoyX) + " and Joystick Y = " + str(JoyY))
blynk.set_user_task(joy_display, 200) # Run function joy_display every 200ms
Thank you for the suggestions. I’m working on trying them out at the moment, but my SD card crapped on me, so I’m installing a faster one now. I’ll try to report back my findings and hopefully working code.
Thank you again, here’s my working code. pretty much my first time really making something in python. I think I like it.
#!/usr/bin/python
# Gotta have time or the boss gets mad
from time import sleep
# Might be a better option for this?
import pigpio
pi=pigpio.pi()
# This is the root of the evil going on here
import BlynkLib
BLYNK_AUTH = '185d56b6ff2f48768ac34b607d7b9593'
# Set and initialize pins
pins = [17, 18, 22, 23]
for p in range(0,4):
pi.set_PWM_range(pins[p], 200)
pi.write(pins[p], 0)
# Global Variables (Thanks Gunner for the help)
JoyX = 0
JoyY = 0
# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH, "192.168.136.247", 8888)
# Made a little clamp() function
# I don't want to type min/max over and over
def clamp(n, smallest, largest):
return max(smallest, min(n, largest))
# Joystick X axis with deadzone to limit drifting
@blynk.VIRTUAL_WRITE(0)
def v0_write_handler(value):
global JoyX
if abs(int(value)) < 30:
JoyX = 0
else:
JoyX = int(value)
mDrive()
# Joystick Y axis
@blynk.VIRTUAL_WRITE(1)
def v1_write_handler(value):
global JoyY
JoyY = int(value)
mDrive()
# LED headlights
@blynk.VIRTUAL_WRITE(2)
def v2_write_handler(value):
pi.write(3, int(value))
# My (messy?) drive function
def mDrive():
Motor = [JoyX - JoyY, JoyX + JoyY]
for p in range(0,2):
pi.set_PWM_dutycycle(pins[p*2], clamp(Motor[p], 0, 200))
pi.set_PWM_dutycycle(pins[p*2+1], abs(clamp(Motor[p], -200, 0)))
# Motors make little beeps for notifications
def twitch(delay):
pi.set_PWM_dutycycle(pins[0], 50)
pi.set_PWM_dutycycle(pins[3], 50)
sleep(delay)
pi.write(pins[0], 0)
pi.write(pins[3], 0)
sleep(delay/2)
# Trigger beeps when ready, and fire up the blynk loop
twitch(0.1)
twitch(0.1)
twitch(0.1)
blynk.run()