Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Analog Joystick / MCP3008 / Raspberry Pi
#!/usr/bin/python
import spidev
import time
import os
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000
def spiChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
while True:
button = spiChannel(0)
xas = spiChannel(1)
yas = spiChannel(2)
print("X-as: {} Y-as: {} Button: {}".format(xas,yas,button))
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.