In this project we're going to learn how to build a simple stopwatch (seconds and minutes) with push buttons and a four digit seven segment display, all using Meadow.Foundation driver stack.
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 StopwatchDisplay.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for StopwatchDisplayCopy the following code below:
public class MeadowApp : App<F7Micro, MeadowApp>
{
bool isRunning;
Stopwatch stopwatch;
PushButton reset;
PushButton startStop;
FourDigitSevenSegment display;
public MeadowApp()
{
var led = new RgbLed(
Device,
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue);
led.SetColor(RgbLed.Colors.Red);
stopwatch = new Stopwatch();
startStop = new PushButton(
device: Device,
inputPin: Device.Pins.D12,
resistor: Meadow.Hardware.ResistorMode.PullUp);
startStop.Clicked += StartStopClicked;
reset = new PushButton(
device: Device,
inputPin: Device.Pins.D13,
resistor: Meadow.Hardware.ResistorMode.PullUp);
reset.Clicked += ResetClicked;
display = new FourDigitSevenSegment
(
portDigit1: Device.CreateDigitalOutputPort(Device.Pins.D00),
portDigit2: Device.CreateDigitalOutputPort(Device.Pins.D03),
portDigit3: Device.CreateDigitalOutputPort(Device.Pins.D04),
portDigit4: Device.CreateDigitalOutputPort(Device.Pins.D06),
portA: Device.CreateDigitalOutputPort(Device.Pins.D01),
portB: Device.CreateDigitalOutputPort(Device.Pins.D05),
portC: Device.CreateDigitalOutputPort(Device.Pins.D08),
portD: Device.CreateDigitalOutputPort(Device.Pins.D10),
portE: Device.CreateDigitalOutputPort(Device.Pins.D11),
portF: Device.CreateDigitalOutputPort(Device.Pins.D02),
portG: Device.CreateDigitalOutputPort(Device.Pins.D07),
portDecimal: Device.CreateDigitalOutputPort(Device.Pins.D09),
isCommonCathode: true
);
display.SetDisplay("0000".ToCharArray());
led.SetColor(RgbLed.Colors.Green);
while (true)
{
string time = stopwatch.Elapsed.Minutes.ToString("D2") +
stopwatch.Elapsed.Seconds.ToString("D2");
display.SetDisplay(time.ToCharArray());
Thread.Sleep(1000);
}
}
void StartStopClicked(object sender, EventArgs e)
{
if (isRunning)
{
stopwatch.Stop();
}
else
{
stopwatch.Start();
}
isRunning = !isRunning;
}
void ResetClicked(object sender, EventArgs e)
{
display.SetDisplay("0000".ToCharArray());
stopwatch.Reset();
}
}
In the Meadow App's constructor, we first initialize an RgbLed
object which we immediately set the LED to Red to indicate the app has started. Next we initialize a StopWatch object which we'll use to show and control the stop watch via our hardware.
We then initialize two push buttons: one to stop/resume and one to reset the time elapsed. We register the click events StartStopClicked
and ResetClicked,
respectively. Notice that since we're not using external resistors, we can use Meadow's internal ones when doing Meadow.Hardware.ResistorMode.PullUp.
Once
the buttons are set, we initialize the display passing in all the pins its connected, specifying that this one is common cathode and setting all the digits to zero by calling the SetDisplay
method.
We then set the onboard LED green to indicate that the initializing process is complete, and the app now enters an infinite while loop which will constantly draw the value of the stopwatch object on the display. Whenever either the two push buttons are pushed, you'll see the display starting, stopping and resetting the stopwatch object.
Step 5 - Run the projectClick the Run button in Visual Studio. It should look similar to this:
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