In this Meadow project we're going to control an SG90 micro servo as we turn a rotary encoder.
Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll see how easy is to program these peripherals using Meadow.Foundation.
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 your project like this:
Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it RotaryServo.
Step 3 - Add the required NuGet packagesWindows
Right-click on your RotaryServo project and click Manage NuGet Packages. In the Browse tab, search for Meadow.Foundation.Servo and click Install to add it to your project.
macOS
Alt-click on your RotaryServo project in the Solution Explorer, and click Add => Add Nuget Package to open the NuGet Package window. Search for Meadow.Foundation.Servo and click Install to add it to your project.
Step 4 - Write the code for RotaryServoCopy the following code below:
public class MeadowApp : App<F7Micro, MeadowApp>
{
int angle = 0;
Servo servo;
RotaryEncoder rotaryEncoder;
public MeadowApp()
{
var led = new RgbLed(Device,
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue);
led.SetColor(RgbLed.Colors.Red);
servo = new Servo(
Device.CreatePwmPort(Device.Pins.D08), NamedServoConfigs.SG90);
servo.RotateTo(0);
rotaryEncoder = new RotaryEncoder(Device, Device.Pins.D02, Device.Pins.D03);
rotaryEncoder.Rotated += (s, e) =>
{
if (e.Direction ==
Meadow.Peripherals.Sensors.Rotary.RotationDirection.Clockwise)
{
angle++;
}
else
{
angle--;
}
if (angle > 180) angle = 180;
else if (angle < 0) angle = 0;
servo.RotateTo(angle);
};
led.SetColor(RgbLed.Colors.Green);
}
}
First we initialize an RgbLed
object and set the color Red
to indicate that the app has started initializing the peripherals. Next we instantiate a Servo
object class (notice we're passing the Servo Config SG90
, which is the kind of servo that's included in our hack kits) and we make it to rotate to 0 degrees as a starting position.
Then we create a rotary encoder object passing the pins for phase A and B interrupts. We register an event handler Rotated
, which is triggered when the encoder is rotated in either direction. In the lambda expression we implement the logic to control the servo: If the rotation is clockwise, we increase an angle property and we decrease if its counter-clockwise; we check the angle doesn't go out of bounds and we then make the servo rotate at the specified angle value.
After initializing the rotary encoder, we can finally set the color of the onboard RGB LED to green to give the user feedback that the project is done initializing and ready to rotate the servo when interacting with the encoder.
Step 5 - Run the projectClick 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