To use this demo, simply move the joystick around. The mouse will move slowly if you move the joystick a little off
center, and more quickly if you move it as far as it goes. Press down on the joystick to click the mouse. Awesome! Now
let's take a look at the code.
Create the Objects and Variables
First we create the mouse object.
Next, we set
x_axis
and
y_axis
to pins
A0
and
A1
. Then we set
select
to
A2
, set it as input and give it a pullup.
The x and y axis on the joystick act like 2 potentiometers. We'll be using them just like we did in
CircuitPython Analog
In
(https://adafru.it/Bep)
. We set
pot_min
and
pot_max
to be the minimum and maximum voltage read from the
potentiometers. We assign
step = (pot_max - pot_min) / 20.0
to use in a helper function.
CircuitPython HID Mouse Helpers
First we have the
get_voltage()
helper so we can get the correct readings from the potentiometers. Look familiar? We
learned about it in Analog In
(https://adafru.it/Bep)
.
Second, we have
steps(axis)
. To use it, you provide it with the axis you're reading. This is where we're going to use
the
step
variable we assigned earlier. The potentiometer range is 0-3.29. This is a small range. It's even smaller with
the joystick because the joystick sits at the center of this range, 1.66, and the + and - of each axis is above and below
this number. Since we need to have thresholds in our code, we're going to map that range of 0-3.29 to while numbers
between 0-20.0 using this helper function. That way we can simplify our code and use larger ranges for our thresholds
instead of trying to figure out tiny decimal number changes.
Main Loop
First we assign
x
and
y
to read the voltages from
x_axis
and
y_axis
.
© Adafruit Industries
https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51
Page 164 of 183
Содержание Feather M4 Express
Страница 1: ...Adafruit Feather M4 Express Created by lady ada Last updated on 2019 03 04 10 41 14 PM UTC...
Страница 5: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 10 of 183...
Страница 58: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 63 of 183...
Страница 164: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 169 of 183...
Страница 168: ...Adafruit Industries https learn adafruit com adafruit feather m4 express atsamd51 Page 173 of 183...