Code: Select all
class Joystick:
def __init__(self, x_axis_pin, y_axis_pin, button_pin):
self.x_axis = pyb.ADC(x_axis_pin)
self.y_axis = pyb.ADC(y_axis_pin)
self.button = pyb.Pin(button_pin, pyb.Pin.IN)
def get_state(self):
return {
'x': self.x_axis.read(),
'y': self.y_axis.read(),
'button': self.button.value()
}
- pressing button doesn't set the pin to high
- ADC changes only for one axis on the joystick and it changes values for x and y:
Code: Select all
# start
{'x': 2855, 'button': 0, 'y': 2851}
>>> j.get_state()
# x axis on one side
{'x': 4095, 'button': 0, 'y': 3140}
>>> j.get_state()
# x axis on another side
{'x': 5, 'button': 0, 'y': 5}
Code: Select all
from time import sleep
import pymcu
BUTTON_DIGITAL_PIN = 1
X_AXIS_ANALOG_PIN = 1
Y_AXIS_ANALOG_PIN = 2
mb = pymcu.mcuModule()
mb.digitalState(BUTTON_DIGITAL_PIN, 'input')
mb.pinHigh(BUTTON_DIGITAL_PIN)
while True:
print mb.analogRead(X_AXIS_ANALOG_PIN), mb.analogRead(Y_AXIS_ANALOG_PIN)
if not mb.digitalRead(BUTTON_DIGITAL_PIN):
print 'button pressed'
sleep(0.1)