In this project we're going to learn how to use an analog Joystick with Meadow using Meadow.Foundation. We'll use four LEDs to light them up as you move the Joystick. Most the things you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro.
Meadow.Foundationa platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LEDproject to properly set up your development environment.
Step 1 - Assemble the circuitWire the joystick and LEDs to Meadow like the following Fritzing diagram:
Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it JoystickLeds.
Step 3 - Write the code for JoystickLedsIn Meadow, there are two approaches to monitor any changes that happen on Inputs. The first is the classic.NET Eventing pattern, in which an event
is raised on change, and the second is an IObservable
pattern, which is a more powerful and flexible reactive-style approach. For this project, we're doing the first approach, meaning that Meadow starts monitoring the Joystick for changes on its two analog inputs (one for X axis and one for Y axis), and it will fire an event when detecting a change.
Copy the following code below:
public class MeadowApp : App<F7Micro, MeadowApp> {
PwmLed Up, Down, Left, Right;
AnalogJoystick joystick;
public MeadowApp() {
Console.WriteLine("Initializing...");
Up = new PwmLed(Device.CreatePwmPort(Device.Pins.D07, 100, 0.0f),
TypicalForwardVoltage.Red);
Down = new PwmLed(Device.CreatePwmPort(Device.Pins.D04, 100, 0.0f),
TypicalForwardVoltage.Red);
Left = new PwmLed(Device.CreatePwmPort(Device.Pins.D02, 100, 0.0f),
TypicalForwardVoltage.Red);
Right = new PwmLed(Device.CreatePwmPort(Device.Pins.D03, 100, 0.0f),
TypicalForwardVoltage.Red);
joystick = new AnalogJoystick(
Device.CreateAnalogInputPort(Device.Pins.A01),
Device.CreateAnalogInputPort(Device.Pins.A00),
null, true);
joystick.SetCenterPosition();
joystick.Updated += JoystickUpdated;
joystick.StartUpdating();
}
void JoystickUpdated(object sender, JoystickPositionChangeResult e) {
if (e.New.HorizontalValue < 0.2f) {
Left.SetBrightness(0f);
Right.SetBrightness(0f);
}
if (e.New.VerticalValue < 0.2f) {
Up.SetBrightness(0f);
Down.SetBrightness(0f);
}
if (e.New.HorizontalValue > 0)
Left.SetBrightness(Math.Abs(e.New.HorizontalValue));
else
Right.SetBrightness(Math.Abs(e.New.HorizontalValue));
if (e.New.VerticalValue > 0)
Down.SetBrightness(Math.Abs(e.New.VerticalValue));
else
Up.SetBrightness(Math.Abs(e.New.VerticalValue));
Console.WriteLine($"({e.New.HorizontalValue}, {e.New.VerticalValue})");
}
}
In the MeadowApp's Constructor, we initialize all four LEDs as PwmLeds
(up
, down
, left
and right
), and the 2 axis AnalogJoystick
(joystick
). Notice in the last parameter when creating the joystick object, IsInverted,
we pass the value true
. This is to indicate that the joystick is upside down, so the driver will correct the orientation by inverting the position values.
After instantiating AnalogJoystick
, we call SetCenterPosition()
to set the current values read in the analog ports and establish them as an initial position. Then, we register joystick
to the Updated
event that fires whenever we move it, and finally we call StartSampling()
so the joystick activates.
Finally, the JoystickUpdated
event handler will check the values for the X and Y axis, and depending on its position, it will adjust the brightness on the corresponding LEDs. Notice there's also an initial threshold we check to see if the Joystick is in the center in either of its axis, so we turn off those LEDs.
Click the Run button in Visual Studio. It should look like to the following GIF:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments