Project Blinky Ready to Build!
First all we need
- Raspberry Pi2
- Led (any color)
- 220 OHM resistor
- Breadboard and couple wire
Using this GPIO we use GPIO number 5 to create blinky our led.
Build our blinky like this picture bellow.
And let’s coding.
Using Visual Studio 2015 we use this code, do not forget add reference Windows IoT Extension
using System; using Windows.Devices.Gpio; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Media; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace Blinky { /// /// An empty page that can be used on its own or navigated to within a Frame. /// public sealed partial class MainPage : Page { private int LEDStatus = 0; private const int LED_PIN = 5; private GpioPin pin; private DispatcherTimer timer; private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red); private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray); public MainPage() { InitializeComponent(); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(500); timer.Tick += Timer_Tick; timer.Start(); Unloaded += MainPage_Unloaded; InitGPIO(); } private void InitGPIO() { var gpio = GpioController.GetDefault(); // Show an error if there is no GPIO controller if (gpio == null) { pin = null; GpioStatus.Text = "There is no GPIO controller on this device."; return; } pin = gpio.OpenPin(LED_PIN); pin.Write(GpioPinValue.High); pin.SetDriveMode(GpioPinDriveMode.Output); GpioStatus.Text = "GPIO pin initialized correctly."; } private void MainPage_Unloaded(object sender, object args) { // Cleanup pin.Dispose(); } private void FlipLED() { if (LEDStatus == 0) { LEDStatus = 1; if (pin != null) { // to turn on the LED, we need to push the pin 'low' pin.Write(GpioPinValue.Low); } LED.Fill = redBrush; } else { LEDStatus = 0; if (pin != null) { pin.Write(GpioPinValue.High); } LED.Fill = grayBrush; } } private void TurnOffLED() { if (LEDStatus == 1) { FlipLED(); } } private void Timer_Tick(object sender, object e) { FlipLED(); } private void Delay_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (timer == null) { return; } if (e.NewValue == Delay.Minimum) { DelayText.Text = "Stopped"; timer.Stop(); TurnOffLED(); } else { DelayText.Text = e.NewValue + "ms"; timer.Interval = TimeSpan.FromMilliseconds(e.NewValue); timer.Start(); } }
TO build Win 10 IoT Core see this link http://ms-iot.github.io/content/GetStarted.htm
First demo Win IoT with Universal apps!