Advertisement
w0lfiesmith

Arduino / Windows 10 Example for MakeUseOf.com

May 13th, 2015
3,895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using Windows.Foundation;
  8. using Windows.Foundation.Collections;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Data;
  13. using Windows.UI.Xaml.Input;
  14. using Windows.UI.Xaml.Media;
  15. using Windows.UI.Xaml.Navigation;
  16. using Microsoft.Maker.Serial;
  17. using Microsoft.Maker.RemoteWiring;
  18.  
  19. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
  20.  
  21. namespace Arduino_Test
  22. {
  23.     /// <summary>
  24.     /// An empty page that can be used on its own or navigated to within a Frame.
  25.     /// </summary>
  26.     public sealed partial class MainPage : Page
  27.     {
  28.         UsbSerial connection;
  29.         RemoteDevice arduino;
  30.         UInt16 lastvalue;
  31.  
  32.         public MainPage()
  33.         {
  34.             this.InitializeComponent();
  35.  
  36.             connection = new UsbSerial("VID_2341", "PID_0043");
  37.             arduino = new RemoteDevice(connection);
  38.             connection.ConnectionEstablished += OnConnectionEstablished;
  39.             connection.begin(57600, SerialConfig.SERIAL_8N1);
  40.            
  41.  
  42.         }
  43.  
  44.         private void OnConnectionEstablished()
  45.         {
  46.             // enable the on off buttons
  47.             var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => {
  48.                 OnButton.IsEnabled = true;
  49.                 OffButton.IsEnabled = true;
  50.             }));
  51.             arduino.pinMode(14, PinMode.ANALOG);
  52.             arduino.AnalogPinUpdatedEvent += MyAnalogPinUpdateCallback;
  53.             Debug.WriteLine(arduino.analogRead(14));
  54.         }
  55.  
  56.         private void OnButtonClick(object sender, RoutedEventArgs e)
  57.         {
  58.             arduino.digitalWrite(13, PinState.HIGH);
  59.         }
  60.  
  61.         private void OffButtonClick(object sender, RoutedEventArgs e)
  62.         {
  63.             arduino.digitalWrite(13, PinState.LOW);
  64.         }
  65.  
  66.         public void MyAnalogPinUpdateCallback(byte pin, UInt16 value)
  67.         {
  68.             if(value-lastvalue >5 || lastvalue-value > 5){
  69.  
  70.                 Debug.WriteLine("Pin A" + pin + " is now " + value);
  71.                 var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => {
  72.                     byte num = Convert.ToByte(value / 4);
  73.                     Page.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, num, num, Convert.ToByte(255 -num)));
  74.  
  75.                 }));
  76.             }
  77.             lastvalue = value;
  78.  
  79.         }
  80.     }
  81.    
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement