There is quite a lot to it . The A and B buttons are also running in this program but not shown in the above video. The joystick also has a built in push button though that didn't work for me. Not sure if that was a problem with the code or the joystick. I'll test it with another one when I can and will update this page when I know more.
# Code below is a simplified version of the code found here
# https://www.waveshare.com/wiki/Joystick_for_micro:bit
# but still a lot to it.
# On the one I was trying it with the push button on pin 8 didn't work
# so I have commented it out below.
# The A and B buttons on the micro:bit are also used in this program
from microbit import *
JoyStick_P = pin8
JoyStick_X = pin1
JoyStick_Y = pin2
DIR = {
'NONE': 0,
'U': 1,
'D': 2,
'L': 3,
'R': 4,
'U_L': 5,
'U_R': 6,
'D_L': 7,
'D_R': 8
}
KEY = {
'NONE': 0,
'P': 1,
'A': 2,
'B': 3
}
class JOYSTICK():
def __init__(self):
self.Read_X = JoyStick_X.read_analog()
self.Read_Y = JoyStick_Y.read_analog()
def Listen_Dir(self, Dir):
Get_Rocker = DIR['NONE']
New_X = JoyStick_X.read_analog()
New_Y = JoyStick_Y.read_analog()
Dx = abs(self.Read_X - New_X)
Dy = abs(self.Read_Y - New_Y)
Right = New_X - self.Read_X
Left = self.Read_X - New_X
Up = New_Y - self.Read_Y
Down = self.Read_Y - New_Y
# max = 1023
Precision = 150
if Right > Precision and Dy < Precision:
Get_Rocker = DIR['R']
elif Left > Precision and Dy < Precision:
Get_Rocker = DIR['L']
elif Up > Precision and Dx < Precision:
Get_Rocker = DIR['U']
elif Down > Precision and Dx < Precision:
Get_Rocker = DIR['D']
elif Right > Precision and Up > Precision:
Get_Rocker = DIR['U_R']
elif Right > Precision and Down > Precision:
Get_Rocker = DIR['D_R']
elif Left > Precision and Up > Precision:
Get_Rocker = DIR['U_L']
elif Left > Precision and Down > Precision:
Get_Rocker = DIR['D_L']
else:
Get_Rocker = DIR['NONE']
if Dir == Get_Rocker:
return True
else:
return False
def Listen_Key(self, Key):
read_key = KEY['NONE']
if button_a.is_pressed():
read_key = KEY['A']
elif button_b.is_pressed():
read_key = KEY['B']
# elif JoyStick_P.read_digital() == 0:
# read_key = KEY['P']
else:
read_key = KEY['NONE']
if Key == read_key:
return True
else:
return False
def Test(self):
while self.Listen_Dir(DIR['U']):
display.show(Image.ARROW_N)
while self.Listen_Dir(DIR['D']):
display.show(Image.ARROW_S)
while self.Listen_Dir(DIR['L']):
display.show(Image.ARROW_W)
while self.Listen_Dir(DIR['R']):
display.show(Image.ARROW_E)
while self.Listen_Dir(DIR['U_L']):
display.show(Image.ARROW_NW)
while self.Listen_Dir(DIR['U_R']):
display.show(Image.ARROW_NE)
while self.Listen_Dir(DIR['D_L']):
display.show(Image.ARROW_SW)
while self.Listen_Dir(DIR['D_R']):
display.show(Image.ARROW_SE)
while self.Listen_Key(KEY['A']):
display.scroll("A")
while self.Listen_Key(KEY['B']):
display.scroll("B")
# while self.Listen_Key(KEY['P']):
# display.scroll("P")
display.clear()
JoyStick = JOYSTICK()
while True:
JoyStick.Test()