Project updated to V1.0 Release Candidate 1 (October 23th, 2022)
In this project we're going to learn how to make a simple dice with LEDs and we "roll the dice" by pressing a push button. 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 circuitWire your project like this:
Create a new Meadow Application project in Visual Studio 2022 for Windows or macOS and name it LedDice.
Step 3 - Write the code for LedDiceIn your MeadowApp class, copy the following code:
// public class MeadowApp : App<F7FeatherV1> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7FeatherV2>
{
Led[] leds;
PushButton button;
public override Task Initialize()
{
var onboardLed = new RgbPwmLed(
device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.SetColor(Color.Red);
leds = new Led[7];
leds[0] = new Led(Device, Device.Pins.D06); //
leds[1] = new Led(Device, Device.Pins.D07); // [6] [5]
leds[2] = new Led(Device, Device.Pins.D08); //
leds[3] = new Led(Device, Device.Pins.D09); // [4] [3] [2]
leds[4] = new Led(Device, Device.Pins.D10); //
leds[5] = new Led(Device, Device.Pins.D11); // [1] [0]
leds[6] = new Led(Device, Device.Pins.D12); //
button = new PushButton(Device, Device.Pins.D04);
button.Clicked += ButtonClicked;
onboardLed.SetColor(Color.Green);
return base.Initialize();
}
void ButtonClicked(object sender, EventArgs e)
{
Random random = new Random();
ShuffleAnimation();
ShowNumber(random.Next(1,7));
}
void ShuffleAnimation()
{
foreach (var led in leds)
{
led.StartBlink(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100));
}
Thread.Sleep(1000);
foreach (var led in leds)
{
led.Stop();
}
Thread.Sleep(100);
}
void ShowNumber(int number)
{
leds[0].IsOn = (number == 6 || number == 5 || number == 4);
leds[1].IsOn = (number == 6 || number == 5 || number == 4 || number == 3 || number == 2);
leds[2].IsOn = (number == 6);
leds[3].IsOn = (number == 4 || number == 5 || number == 3 || number == 1);
leds[4].IsOn = (number == 6);
leds[5].IsOn = (number == 6 || number == 5 || number == 4 || number == 3 || number == 2);
leds[6].IsOn = (number == 6 || number == 5 || number == 4);
}
}
This project uses an array of seven PwmLed
objects declared as leds
and one PushButton
object named button
and they're both initialized in the MeadowApp's constructor. Once they're initialized we call the ShuffleAnimation()
method (explained below) to give feedback that the project is fully loaded and running and lastly we do Thread.Sleep(Timeout.Infinite);
to keep the app running until the Meadow board is powered off.
The project consists of two methods and one event handler:
ShuffleAnimation();
method goes through each LED and starts a blink animation for one second and stops the animation on each LED right after.ShowNumber(int number);
method turns on the corresponding LEDs depending on the number passed in as a parameter.ButtonClicked(
object sender, EventArgs e
)
is the event handler method triggered wheneverbutton
is pressed, and what it does is instantiate a localRandom
object, calls theShuffleAnimation()
and once the animation finishes it calls theShowNumber(int number)
method passing a random value between one and six as a parameter.
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