sparkwlf
Published © MIT

The Running Bug

The bug toy is running when it detected by near-infrared sensors.

BeginnerShowcase (no instructions)3 hours1,058
The Running Bug

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Breadboard (generic)
Breadboard (generic)
面包板(www.taobao.com)
×1
Jumper wires (generic)
Jumper wires (generic)
连接线若干(www.taobao.com)
×1
Resistor 10k ohm
Resistor 10k ohm
9千欧电阻(www.taobao.com)
×1
Relay Module (Generic)
新款 1路继电器模块 5V高电平触发 继电器扩展板(www.taobao.com)
×1
Infrared Module (Generic)
HC-SR501 进口探头 人体红外感应模块 热释电 红外传感器进口探头(www.taobao.com)
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core
10586
Visual Studio 2015
Microsoft Visual Studio 2015

Story

Read more

Schematics

schematic diagram

Code

The bug toy code

C#
Program code is very simple ,Init GPIO and Infrared trigger detection.
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);

        }
    }
}

Credits

sparkwlf

sparkwlf

3 projects • 0 followers
上善若水,厚德载物。

Comments