Project updated to beta 6.2 (released on March 5th, 2022)
In this project we're going to control a servo when pressing a push button using Meadow. Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll create a Meadow Application project and use Meadow.Foundation to easily write the logic.
Meadow.Foundation a 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 LED project to properly set up your development environment.
Step 1 - Assemble the circuitConnect your button and servo like the following Fritzing diagram:
Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it ServoButton.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for ServoButtonCopy the following code below:
using Meadow;
using Meadow.Devices;
using Meadow.Foundation;
using Meadow.Foundation.Leds;
using Meadow.Foundation.Sensors.Buttons;
using Meadow.Foundation.Servos;
using Meadow.Units;
using System;
using System.Threading;
using AU = Meadow.Units.Angle.UnitType;
namespace ServoButton
{
// public class MeadowApp : App<F7Micro, MeadowApp> <- If you have a Meadow F7 v1.*
public class MeadowApp : App<F7MicroV2, MeadowApp>
{
Servo servo;
PushButton button;
public MeadowApp()
{
Initialize();
}
void Initialize()
{
var onboardLed = new RgbPwmLed(
device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.SetColor(Color.Red);
servo = new Servo(Device.CreatePwmPort(Device.Pins.D10),
NamedServoConfigs.SG90);
servo.RotateTo(NamedServoConfigs.SG90.MaximumAngle);
button = new PushButton(Device, Device.Pins.D04);
button.Clicked += ButtonClicked;
onboardLed.SetColor(Color.Green);
}
void ButtonClicked(object sender, EventArgs e)
{
servo.RotateTo(new Angle(75, AU.Degrees));
Thread.Sleep(1000);
servo.RotateTo(new Angle(0, AU.Degrees));
}
}
}
In the MeadowApp's constructor the first thing the app does is initialize a Servo
object, passing a ServoConfig
for the SG90 model so it can rotate at its full range. Once Initialized, servo rotates to angle 0 using RotateTo(int angle);
method. Next the app initializes a PushButton
by passing an IIODevice
which is Meadow in this case, and the IPin
which the button is connected (D04
). Now that our button is initialized, we can register the Clicked event handler, and will call ButtonClicked
, which rotated the servo to 75 degrees for one second, and then goes back to its initial position.
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