I would like to ask colleagues with more experience for their opinion.
I am at the stage of reading the value of the analogue pads in micropython on the micro: bit platform.
Can this code be shortened? I want to determine and save (for later use) in this code the zero position of the joystick, and the max. And min. Positions. for each axis in order to correctly position the servo in the program on the basis of the established ranges.
How do I map the known ranges of deflection for the full range of the servo deflection? There is some function in micropythona. I note that I haven't been looking yet. So far I have struggled with this program.
Thanks to everyone for any suggestions and help. If I use bad programming practices somewhere, please write. Man learns from mistakes
"print" and "sleep" lines in ext. they are only for checking what I have in the variables.
Code: Select all
from microbit import *
max_x = 0
max_y = 0
min_y = 0
min_x = 0
zero_y = int(pin0.read_analog())
zero_x = int(pin1.read_analog())
while True:
click = pin2.read_digital()
while not(click):
value = pin0.read_analog()
while value > zero_y and value > max_y:
max_y = value
print(str(max_y))
sleep(100)
min_y = max_y/2
while value < zero_y and value < min_y:
min_y = value
print(str(min_y))
sleep(100)
value = pin1.read_analog()
while value > zero_x and value > max_x:
max_x = value
print(str(max_x))
sleep(100)
min_x = max_x/2
while value < zero_x and value < min_x:
min_x = value
print(str(min_x))
sleep(100)
click = pin2.read_digital()
break
print('min x: '+str(min_x)+' max x: '+str(max_x))
print('min y: '+str(min_y)+' max y: '+str(max_y))
print('zero x: '+str(zero_x)+' zero y: '+str(zero_y))
sleep(100)