Project updated to beta 6.2 (released on March 5th, 2022)
In this project we're going to learn how to use Meadow's real time clock (RTC) by making a simple clock using a 20x4 characters LCD display and a couple of push buttons. 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.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 2019 for Windows or macOS and name it MeadowClock.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for MeadowClockCopy the following code below:
// public class MeadowApp : App<F7Micro, MeadowApp> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7MicroV2, MeadowApp>
{
PushButton minute;
PushButton hour;
CharacterDisplay display;
public MeadowApp()
{
Initialize();
CharacterDisplayClock();
}
void Initialize()
{
var led = new RgbLed(
Device,
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue);
led.SetColor(RgbLed.Colors.Red);
display = new CharacterDisplay
(
device: Device,
pinV0: Device.Pins.D11,
pinRS: Device.Pins.D10,
pinE: Device.Pins.D09,
pinD4: Device.Pins.D08,
pinD5: Device.Pins.D07,
pinD6: Device.Pins.D06,
pinD7: Device.Pins.D05
);
hour = new PushButton(Device, Device.Pins.D14);
hour.Clicked += HourClicked;
minute = new PushButton(Device, Device.Pins.D13);
minute.Clicked += MinuteClicked;
Device.PlatformOS.SetClock(new DateTime(2022, 03, 05, 19, 45, 00));
led.SetColor(RgbLed.Colors.Green);
}
void HourClicked(object sender, EventArgs e)
{
Device.SetClock(DateTime.Now.AddHours(1));
}
void MinuteClicked(object sender, EventArgs e)
{
Device.SetClock(DateTime.Now.AddMinutes(1));
}
void CharacterDisplayClock()
{
display.ClearLines();
display.WriteLine($"Meadow F7 Micro ", 0);
display.WriteLine($"Onboard RTC ", 1);
while (true)
{
DateTime clock = DateTime.Now;
display.WriteLine($"{clock:MM}/{clock:dd}/{clock:yyyy}", 2);
display.WriteLine($"{clock:hh}:{clock:mm}:{clock:ss} {clock:tt}", 3);
Thread.Sleep(1000);
}
}
}
Meadow Constructor
The app initializes both the push buttons, which one increases the clock by an hour and the other increases the clock by a minute on each press. The interface to adjust the time could be more sophisticated, but I opted to keep it simple for the purpose of this project.
Then initializes the LCD display (CharacterDisplay
), and we finally call Device.SetClock(DateTime
dateTime);
method to initialize the date and time on Meadow.
CharacterDisplayClock
This method clears the screen, writes a couple of strings on the first and second line of the display, and finally enters an infinite while loop, where every second refreshes the Date and Time on the third and fourth line. Note that to write text on the display, you simply call the WriteLine(string text, int line);
API method.
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