Pi Python merged values

Blynk local server
Raspberry Pi Zero W

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.

import pigpio
import RPi.GPIO as GPIO
import BlynkLib
import time
from operator import add
BLYNK_AUTH = '185d56b6ff2f48768ac34b607d7b9593'
pi.set_PWM_range(17, 320)
pi.set_PWM_range(18, 320)
pi.write(17, 0)
pi.write(18, 0)

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH, "", 8888)

# Register virtual pin handler
def v0_write_handler(value):
    print('Value 1: ', value)
    #JoyX = int(value[0])
    #JoyY = int(value[1])
    #mDrive(JoyX, JoyY)

1 Like

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 :stuck_out_tongue:

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


# Gotta have time or the boss gets mad
from time import sleep

# Might be a better option for this?
import pigpio

# 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, "", 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
def v0_write_handler(value):
  global JoyX
  if abs(int(value)) < 30:
    JoyX = 0
    JoyX = int(value)

# Joystick Y axis
def v1_write_handler(value):
  global JoyY
  JoyY = int(value)

# LED headlights
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)
  pi.write(pins[0], 0)
  pi.write(pins[3], 0)

# Trigger beeps when ready, and fire up the blynk loop

1 Like