Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
![]() |
| |||||
![]() |
|
My daughter needs a manual daily toy. With her, she asked me if the toy is too stupid, because it needs to start then it moves, if it like really bugs as I grabbed it then it ran off, the game is interesting as that. So I want to give this toy to add sensors and electronic switch, with the help of Raspberry Pi and Windows IoT, I finished "The running the worms".
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Gpio;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
//“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍
namespace SmartBug
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MainPage : Page
{
private const int INFRARED_PIN = 4;
private const int BUG_PIN = 5;
private GpioPin pin_infrared;
private GpioPin pin_bug;
private GpioPinValue pinValue_infrared;
private GpioPinValue pinValue_bug;
private DispatcherTimer timer;
public MainPage()
{
InitializeComponent();
InitGPIO();
while (true)
{
pinValue_infrared = pin_infrared.Read();
if (pinValue_infrared == GpioPinValue.High)
{
pinValue_bug = GpioPinValue.High;
pin_bug.Write(pinValue_bug);
Task.Delay(20).Wait();
pinValue_bug = GpioPinValue.Low;
pin_bug.Write(pinValue_bug);
}
else
{
pinValue_bug = GpioPinValue.Low;
pin_bug.Write(pinValue_bug);
}
}
}
private void InitGPIO()
{
var gpio = GpioController.GetDefault();
// Show an error if there is no GPIO controller
if (gpio == null)
{
pin_infrared = null;
pin_bug = null;
return;
}
pin_infrared = gpio.OpenPin(INFRARED_PIN);
pin_infrared.SetDriveMode(GpioPinDriveMode.Input);
pin_bug = gpio.OpenPin(BUG_PIN);
pinValue_bug = GpioPinValue.High;
pin_bug.Write(pinValue_bug);
pin_bug.SetDriveMode(GpioPinDriveMode.Output);
}
}
}
Comments
Please log in or sign up to comment.