Roll An Led Dice With A Push Button Using Meadow

Photo of jorgedevs

Made by jorgedevs

About the project

Learn how to make an LED dice and you can roll it pressing a push button using a Meadow F7 Micro and Meadow. Foundation driver library.

Project info

Difficulty: Easy

Platforms: MicrosoftSparkFunMeadowWilderness Labs

Estimated time: 1 hour

License: Apache License 2.0 (Apache-2.0)

Items used in this project

Hardware components

Tiny Breadboard Tiny Breadboard x 1
Resistor Network - 10K Ohm (6-pin bussed) Resistor Network - 10K Ohm (6-pin bussed) x 1
Momentary Pushbutton Switch - 12mm Square Momentary Pushbutton Switch - 12mm Square x 1
Tiny Breadboard Tiny Breadboard x 7
Tiny Breadboard Tiny Breadboard x 1
Jumper Wire Kit - 140pcs Jumper Wire Kit - 140pcs x 1
Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM x 1

Software apps and online services

Microsoft Visual Studio 2019 Microsoft Visual Studio 2019

Story

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.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 circuit

Wire your project like this:

LedDice circuit diagram

LedDice circuit diagram

LedDice circuit diagram

Step 2 - Create a Meadow Application project

Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it LedDice.

Step 3 - Write the code for LedDice

In your MeadowApp class, copy the following code:

using Meadow;using Meadow.Devices;using Meadow.Foundation.Leds;using Meadow.Foundation.Sensors.Buttons;using System;using System.Threading;namespace LedDice{    public class MeadowApp : App<F7Micro, MeadowApp>    {        PwmLed[] leds;        PushButton button;        public MeadowApp()        {            leds = new PwmLed[7];            leds[0] = new PwmLed(                Device.CreatePwmPort(Device.Pins.D06),                TypicalForwardVoltage.Red);            leds[1] = new PwmLed(                Device.CreatePwmPort(Device.Pins.D07),                TypicalForwardVoltage.Red);            leds[2] = new PwmLed(                Device.CreatePwmPort(Device.Pins.D08),                TypicalForwardVoltage.Red);            leds[3] = new PwmLed(                Device.CreatePwmPort(Device.Pins.D09),                TypicalForwardVoltage.Red);            leds[4] = new PwmLed(                Device.CreatePwmPort(Device.Pins.D10),                TypicalForwardVoltage.Red);            leds[5] = new PwmLed(                Device.CreatePwmPort(Device.Pins.D11),                TypicalForwardVoltage.Red);            leds[6] = new PwmLed(                Device.CreatePwmPort(Device.Pins.D12),                TypicalForwardVoltage.Red);            button = new PushButton(Device, Device.Pins.D05);            button.Clicked += ButtonClicked;            ShuffleAnimation();            // Keeps the app running            Thread.Sleep(Timeout.Infinite);        }        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();            }            Thread.Sleep(1000);            foreach (var led in leds)            {                led.Stop();            }            Thread.Sleep(100);        }        void ShowNumber(int number)        {            switch (number)            {                case 1:                    leds[3].IsOn = true;                    break;                case 2:                    leds[1].IsOn = true;                    leds[5].IsOn = true;                    break;                case 3:                    leds[1].IsOn = true;                    leds[3].IsOn = true;                    leds[5].IsOn = true;                    break;                case 4:                    leds[0].IsOn = true;                    leds[1].IsOn = true;                    leds[5].IsOn = true;                    leds[6].IsOn = true;                    break;                case 5:                    leds[0].IsOn = true;                    leds[1].IsOn = true;                    leds[3].IsOn = true;                    leds[5].IsOn = true;                    leds[6].IsOn = true;                    break;                case 6:                    leds[0].IsOn = true;                    leds[1].IsOn = true;                    leds[2].IsOn = true;                    leds[4].IsOn = true;                    leds[5].IsOn = true;                    leds[6].IsOn = true;                    break;            }        }    }}

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 whenever button is pressed, and what it does is instantiate a local Random object, calls the ShuffleAnimation() and once the animation finishes it calls the ShowNumber(int number) method passing a random value between one and six as a parameter.

Step 4 - Run the project

Click the Run button in Visual Studio. It should look like to the following GIF:

LedDice project running

LedDice project running

LedDice project running

Check out Meadow.Foundation!

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.

References

Schematics, diagrams and documents

LedDice circuit diagram

Code

LedDice complete project

You can check the LedDice project in Meadow.Samples/Source/MeadowSamples/Projects/LedDice

Credits

Leave your feedback...