Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rasp Pi Analog Joystick
#1
I am currently trying to connect my raspberry pi 3 to an analog joystick controller using an ADC but when I run the code and move the joystick, it always returns a position of 0. Any help would be greatly appreciated.

#!usr/bin/python
import spidev
import os
import time

#Define Axis Channels
swt_channel = 0
vrx_channel = 1
vry_channel = 2

#Time Delay
delay = 0.5

#Spi oeffnen
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000

#Function for reading the MCP3008 channel between 0 and 7
def readChannel(channel):
    val = spi.xfer2([1,(8+channel)<<4,0])
    data = ((val[1]&3) <<8) + val[2]
    return data

#endless loop
while True:
    #Determine position
    vrx_pos = readChannel(vrx_channel)
    vry_pos = readChannel(vry_channel)

    #SW determine
    swt_val = readChannel(swt_channel)

    #output
    print("VRx : {} VRy : {} SW : {}".format(vrx_pos,vry_pos,swt_val))

    # wait
    time.sleep(delay)
Reply


Messages In This Thread
Rasp Pi Analog Joystick - by mmagner2022 - Feb-07-2022, 02:05 AM
RE: Rasp Pi Analog Joystick - by Larz60+ - Feb-07-2022, 10:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Converting PyGame 2 axis joystick float to 360 angle archieab 1 3,399 Sep-26-2018, 05:40 PM
Last Post: archieab
  [PyGame] Using joystick module from PyGame archieab 6 16,366 Sep-25-2018, 06:13 PM
Last Post: archieab
  [PyGame] Joystick Input microphone_head 2 9,490 Sep-16-2018, 06:02 AM
Last Post: microphone_head
  [PyGame] Limiting a Joystick value -1 to +1, to a value between 0 an 127 with 64 being center? japreja 2 4,538 Dec-10-2017, 06:20 AM
Last Post: japreja

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020