Created
February 4, 2019 16:23
-
-
Save raspberrytipsnl/9536226c3bf84a7f10af0c263b87a5d7 to your computer and use it in GitHub Desktop.
Analog Joystick / MCP3008 / Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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