SHARE
TWEET

Raspberry Pi 2 IoT LED Demo

aznGiLL Oct 5th, 2015 208 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.        private const int LED_PIN = 6;
  2.         private const int PB_PIN = 5;
  3.         private GpioPin pin;
  4.         private GpioPin pushButton;
  5.         private DispatcherTimer timer;
  6.         private GpioPinValue pushButtonValue;
  7.  
  8.         public MainPage()
  9.         {
  10.             InitializeComponent();
  11.  
  12.             timer = new DispatcherTimer();
  13.             timer.Interval = TimeSpan.FromMilliseconds(1);
  14.             timer.Tick += Timer_Tick;
  15.             timer.Start();
  16.  
  17.             Unloaded += MainPage_Unloaded;
  18.  
  19.             InitGPIO();
  20.         }
  21.  
  22.         private void InitGPIO()
  23.         {
  24.             var gpio = GpioController.GetDefault();
  25.  
  26.             if (gpio == null)
  27.             {
  28.                 pin = null;
  29.                 return;
  30.             }
  31.             pushButton = gpio.OpenPin(PB_PIN);
  32.             pin = gpio.OpenPin(LED_PIN);
  33.  
  34.             pushButton.SetDriveMode(GpioPinDriveMode.Input);
  35.             pin.Write(GpioPinValue.Low);
  36.             pin.SetDriveMode(GpioPinDriveMode.Output);
  37.  
  38.         }
  39.  
  40.         private void MainPage_Unloaded(object sender, object args)
  41.         {
  42.             pin.Dispose();
  43.             pushButton.Dispose();
  44.         }
  45.  
  46.         private void FlipLED()
  47.         {
  48.             pushButtonValue = pushButton.Read();
  49.             if (pushButtonValue == GpioPinValue.High)
  50.             {
  51.                 pin.Write(GpioPinValue.High);
  52.             }
  53.             else if (pushButtonValue == GpioPinValue.Low)
  54.             {
  55.                 pin.Write(GpioPinValue.Low);
  56.             }
  57.         }
  58.  
  59.         private void Timer_Tick(object sender, object e)
  60.         {
  61.             FlipLED();
  62.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top