Microsoft is betting on Windows 10, finally fulfilling the dream of apps that run on cross-platform platforms. Combined with official Arduino support, you have a powerful new tool at your disposal: the ability to easily create Universal Windows Apps that have a hardware connection to the real world.

Here’s how to get started, even if you’ve never programmed a Windows app before.

Before reading further, check out a demo of what we’re about to do.

I should note that I didn’t use Visual Studio or even touch C# until about 10 years ago. I’m approaching this from the perspective of a fresh install of Visual Studio and have forgotten everything I know.

If you’re already quite experienced with programming Windows apps, or even looking for an easy way to get started programming on Windows without this Arduino, try Ryan’s tutorial on building a simple Visual Basic app. create . Newcomers to absolute programming may want to familiarize themselves with my programming. 101 (and part 2 article).

You must be familiar with some projects (and maybe even read our Arduino Guide), but this is probably the first time you’ve tried to build a real software product to interface with it.

Downloads

First, you need to join the Windows Insider program to get the latest preview Visual Studio 2015 and latest version Windows 10 preview . Do it now — it’s free. Windows 10 is a developer preview and should not be installed as the primary operating system. This is fucking buggy.

  • Join the Windows Insider Program and download the Windows 10 Technical Preview.
  • Get Visual Studio 2015 Community Edition Preview [больше не доступно].

visual studio is Microsoft’s own development environment that we will use to create a C# application for Windows.

Why C#? With a strong resemblance to Java, it’s a relatively easy programming language for beginners to get up and running with a running app, yet powerful enough to let you create amazing apps (even games: C# — Unity’s scripting language of choice — check out our freebies). eBook, The Beginner’s Guide to Game Programming with Unity).

If you haven’t already, download Arduino IDE from the Arduino.cc official website and install standard boards for a fee. You will find it in the section Examples -> Firm -> Standard Firm . This just turns it into a «dumb» serial device that will do what our application says it will do — there will be no application logic on the board itself, just an interface between our application and any sensors or output devices connected to the Arduino. ,

In terms of wiring, you can either attach an LED directly to pin 13 and GND as shown below, or you can use an onboard LED. You will also need a variable resistor (I used a 10 kΩ linear potentiometer) going into A0 (with matching pins on GND and +5V obviously).

Arduino-Windows 10-demo

Finally, download the Remote Wiring package from GitHub [Больше не доступно]. This is the layer we need to add in order for our Windows application to communicate with the Arduino.

Create Application

Go ahead and open Visual Studio. If you are running it for the first time, you will be prompted to sign in. If you want, ignore it. Select Visual C# as a design option and continue; Either way, it takes a few minutes to get Visual Studio up and running for the first time.

Create a new project using a template Visual C# -> Blank App (Windows Universal) . I named mine «Arduino Test» but it doesn’t matter.

new universal application for Windows with Sharp

At this point, I ran into an error about having to switch Windows 10 to developer mode if I really wanted to run the app. Do this though, if you find your build of Windows 10 doesn’t work with this setting, it’s a known bug and you’ll need to use the Group Policy Editor to enable Developer Mode.

Then right click anywhere Solution Explorer (it’s on the right) and select » Add» -> » Existing project» .

Solution Explorer add projects

Navigate to where you downloaded the Remote Wiring files from Github — if it was unzipped, it should be a folder named remote-wiring-development . Inside you will find Microsoft.Maker.win10 ; and inside that you will find 3 more folders. In turn, add each of them by going to these three folders and locating the project file.

If you’re getting any «XAML 8.2 file not found» error messages, then you have the wrong version of Visual Studio or you haven’t installed the developer tools yet. Go back to the beginning of this article and make sure you have downloaded and installed both of the associated Visual Studio files.

Those 3 projects you just added are just different layers of the Arduino interface. In Solution Explorer, if you right-click and select » Dependencies» -> » Build Dependencies» you can see which layers depend on (on what Serial depends; on Serial depends Firmata ; from both — remotewiring ) . The only change you need to make is to select your project from the drop down list and check the boxes in each box to indicate that your project depends on all these other projects.

check dependencies

Last step: Again in Solution Explorer, right-click on the item » Links» in your project and select » Add link» . On the left go to Windows Universal then check the box next to Microsoft Visual C++ AppLocal Runtime Package . Don’t close the dialogue just yet.

project links

Then go to Projects (also in the same dialog, from the list on the left) and check the box next to each of the three projects Microsoft Maker .

manufacturer links

It was harder than it should be, but you only have to do it once; now we can have fun with programming — I promise it’s not so scary.

programming

If you run into problems after that, the full code is available on Pastebin. However, I highly recommend reading the notes below to explain what the code actually does.

First, we need to add some code that says we need a USB port to communicate with the Arduino. Find a file Package.appxmanifest in browser solutions and double-click it to edit it. We need to paste some code here — technically, we’ll «insert a child node» because it’s an XML file, but just replace the entire section with the following code so that it looks like this:

capabilities

The exact code will be different if you’re using Bluetooth, or if you’re targeting Win8.1 instead of 10, but the next one is Windows 10, USB connection.

        

Go to menu » Assembly» -> » Rebuild Solution» and make sure you don’t get any errors.

In Solution Explorer, expand the node MainPage.xaml . Double-clicking on it will load the Form Builder, which we’ll come back to later, but for now open MainPage.xaml.cs file which contains the main logic of our application.

Add two lines in the first section to indicate that we will be «using» the Arduino bits.

 using Microsoft.Maker.serial; using Microsoft.Maker.RemoteWiring; 

I also added a line to say that we will use System.Diagnostics; which allows us to use Debug.WriteLine() function to display debug messages in the IDE.

First of all, let’s define a few variables that we’ll be using throughout. Add these lines just before the declaration public function MainPage() .

 UsbSerial connection; RemoteDevice arduino; UInt16 lastvalue; 

Then we move on to the MainPage() function — this is called the constructor, and this is the function that is called immediately when our application is created, so we use it to set everything up.

First, add a line to establish a USB serial connection to a specific USB device (Arduino).

 connection =new UsbSerial("VID_2341", "PID_0043"); 

Note that the USB IDs of the standard Arduino Uno are already encoded in the block, but this can be checked in Device Manager -> Ports (COM and LPT) -> Arduino Uno -> tab » Intelligence » -> Hardware IDs .

check USB HID PID

Then add the following lines right after that.

 arduino = new RemoteDevice(connection); connection.ConnectionEstablished += OnConnectionEstablished; connection.begin(57600, SerialConfig.SERIAL_8N1); 

Several things are happening here. First, we create a RemoteDevice object — remember, this is the layer that gives us a bunch of Arduino commands — and give it a variable name of «arduino». The following line is attached to the ConnectionEstablished event to say: « After successful USB connection, please run the OnConnectionEstablish() function.»

The final just reports that the connection has already started, at 57600 baud, using an 8-bit pattern.

You also need to create this OnConnectionEstablished() function, so step outside of the MainPage() function and just create an empty function like this.

 private void OnConnectionEstablished() { // LOGIC HERE } 

Go back to the form builder I mentioned earlier (if you forgot how to do it: double click MainPage.xaml in browser solutions). Create two buttons. To do this, open the «Toolbar» tab, which you will find pinned vertically on the left side of the screen. Drag two buttons onto the page, then label one On and one Off .

form designer add buttons

Select the first button, then in the «Properties» field in the lower right corner, name it «OnButton». Click on the small lightning icon — this is a list of events and actions that is used to indicate what happens when interacting with form elements. Enter «OnButtonClick» in the » Click» .

When you press enter, it automatically generates the code for that particular button event and loads the standard code view. Go back to the form designer and do the same for the «Off» button, but this time name it «OffButton» and «OffButtonClick». While you’re there, go ahead and name the main window form behind it «Page» — we’ll use that later. You should now have something that looks like the screenshot below:

designer creates events

Switch back to the designer again and again for each button set for properties isEnabled meaning False . You can do this by injecting the property directly into the XAML Code View, or you can find this checkbox in the Properties box (click the wrench if you’re still in the Actions View) — it’s in the extended category » General properties».

This is not required, but it is recommended to disable the buttons until we are sure the Arduino is up and connected.

To enable them again, add the following to the OnConnectionEstablished() function. Don’t worry about the exact meaning of the code for now, it’s just the way you should handle form updates in modern Windows applications to get good performance. After we connected to the Arduino, we set the property to IsEnabled buttons set to true.

 private void OnConnectionEstablished() { // enable the on off buttons var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => { OnButton.IsEnabled = true; OffButton.IsEnabled = true; })); arduino.pinMode(14, PinMode.ANALOG); arduino.AnalogPinUpdatedEvent += MyAnalogPinUpdateCallback; Debug.WriteLine(arduino.analogRead(14)); } 

You’ll also see the more familiar pinMode() statement, which tells us we have an analog input on pin 14 (there are 13 digital pins, so A0 starts counting at 14). Then we have another event declaration — when the value of the analog pin is updated, the MyAnalogPinUpdateCallback function is called.

Finally, we need to edit the button click events and decide what happens when the analog input changes. Let’s start with the buttons. We can interact with the Arduino using function names like normal Arduino code like this:

 arduino.digitalWrite(13, PinState.HIGH); 

Paste this for the onButton event, and this for the offButton:

 arduino.digitalWrite(13, PinState.LOW); 

Just. The analog input from the Arduino is a little more complicated, but here’s what I came up with.

 public void MyAnalogPinUpdateCallback(byte pin, UInt16 value) { if(value-lastvalue >5 || lastvalue-value > 5){ 
 Debug.WriteLine("Pin A" + pin + " is now " + value); var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => { byte num = Convert.ToByte(value / 4); Page.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, num, num, Convert.ToByte(255 -num))); })); } lastvalue = value; } 

Note that I store the previous value we got from the output in the lastValue variable; this allows us to check how much the value is changing and only react if the difference is significant (a kind of signal smoothing). If it changes in any direction by more than 5 (0-1024 is the full range of values), then we react by changing the Page element’s RGB background color value.

Since we only have one number that I was working with and I was in a hurry, I made up a little here the 3 digits needed to make R, G and B. You can probably come up with something prettier.

Finished

Arduino Windows 10 completed

That’s it — run the code and you should see something similar to the screenshot above. The buttons turn the LED on and off, and the variable resistor changes the background. If you’re having trouble, don’t forget the full code here.

Now that you can write your own Windows 10 apps that interact with the Arduino, what will you do? Possibly the controller ? Let me know in the comments.

Похожие записи