Getting Started With Windows Remote Arduino PDF
Getting Started With Windows Remote Arduino PDF
com
www.ebook777.com
Copyright
free ebooks ==> www.ebook777.com
Table of Contents
Copyright
Preface
1. Preparing Development Environment
1.1 Arduino
1.1.1 Arduino Uno
1.1.2 Arduino Leonardo
1.1.3 Arduino Mega 2560
1.1.4 Arduino Due
1.2 Electronics Components
1.2.1 Arduino Starter Kit
1.2.2 Fritzing
1.2.3 Cooking-Hacks: Arduino Starter Kit
1.2.4 Arduino Sidekick Basic kit v2
1.2.5 Grove - Starter Kit for Arduino
1.2.6 DFRobot - Arduino Kit for Beginner v3
1.3 Windows 10 and Visual Studio 2015
1.4 Windows Remote Arduino
1.5 Arduino Software
1.6 Testing
2. Windows Remote Arduino for Windows 10
2.1 Setting Up Arduino on Windows 10
2.1.1 Arduino Hardware Driver on Windows 10
2.1.2 Simple Testing
2.2 Arduino and Windows Remote Arduino (WRA)
2.2.1 Configuring Arduino Board
2.2.2 Compiling Windows Remote Arduino
2.3 Windows Remote Arduino with USB
2.3.1 Wiring
2.3.2 Creating A Project
www.ebook777.com
2.3.3 Adding Reference Files for Windows Remote Arduino
free ebooks ==> www.ebook777.com
2.3.4 Configuring Windows Remote Arduino (WRA) on Project
2.3.5 Writing Program
2.3.6 Testing
2.4 Windows Remote Arduino with Bluetooth
3. Digital I/O
3.1 Getting Started
3.2 Wiring
3.3 Creating and Configuring a Project
3.4 Writing a Program
3.5 Testing
4. Analog I/O
4.1 Getting Started
4.2 Demo Analog Output: RGB LED
4.2.1 Arduino Analog output (PWM)
4.2.2 Wiring
4.2.3 Writing Program
4.2.4 Testing
4.3 Demo Analog Input: Working with Potentiometer
4.3.1 Wiring
4.3.2 Writing Program
4.3.3 Testing
5. Working with I2C
5.1 Getting Started
5.2 Writing Program
5.3 Testing
6. Servo Motor
6.1 Servo Motor
6.2 Wiring
6.3 Writing A Program
6.4 Testing
free ebooks ==> www.ebook777.com
Source Code
Contact
www.ebook777.com
Preface
free ebooks ==> www.ebook777.com
This book was written to help anyone want to get started with Arduino and Windows Remote Arduino. It describes
the basic elements of the integration of Arduino and Windows Remote Arduino.
Agus Kurniawan
Depok, June 2015
free ebooks ==> www.ebook777.com
1. Preparing Development Environment
www.ebook777.com
1.1 Arduino
free ebooks ==> www.ebook777.com
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use
hardware and software. This board uses Atmel microcontroller series. There are many
Arduino hardware models that you can use. Further information about Arduino products,
you can visit on website http://arduino.cc/en/ .
You must one Arduino hardware to follow practices in this book. I recommend to obtain
one of the following Arduino hardware:
Arduino Uno
Arduino Leonardo
Arduino Mega 2560
Arduino Due
You can buy this product on your local electronic store. You also can order it by online.
Find it on http://arduino.cc/en/Main/Buy. The following is the list of Arduino store you
can buy
Because Arduino is an open-source hardware, people can build it. It’s called Arduino
compatible. Generally it’s sold in low prices.
www.ebook777.com
http://arduino.cc/en/Main/ArduinoBoardMega2560 .
free ebooks ==> www.ebook777.com
1.2.2 Fritzing
Store website: http://shop.fritzing.org/ .
You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with Arduino
Mega.
www.ebook777.com
free ebooks ==> www.ebook777.com
www.ebook777.com
free ebooks ==> www.ebook777.com
1.2.5 Grove - Starter Kit for Arduino
Another option, you can buy this kit on
Seeedstudio, http://www.seeedstudio.com/depot/Grove-Starter-Kit-for-Arduino-p-
1855.html .
www.ebook777.com
1.3 Windows 10 and Visual Studio 2015
free ebooks ==> www.ebook777.com
To develop app with Windows Remote Arduino, we use Visual Studio 2015 and Windows
10. Please install them for our development environment.
free ebooks ==> www.ebook777.com
1.4 Windows Remote Arduino
Windows Remote Arduino is an open-source Windows Runtime Component library which
allows Makers to control an Arduino through a Bluetooth or USB connection! It is
intended for Windows Runtime (WinRT) Developers who want to harness the power of
Arduino hardware using the Windows Runtime Languages. This library can be download
on https://github.com/ms-iot/remote-wiring/.
We will deploy and test program based Windows Remote Arduino on the next chapter.
www.ebook777.com
1.5 Arduino Software
free ebooks ==> www.ebook777.com
To develop application based on Arduino board, we need Arduino software. You can
obtain it on http://arduino.cc/en/Main/Software . Download Arduino IDE for Windows
platform.
Execute that setup file. Select all items on installation.
At the middle of installation process, you get security confirmation. Please click Install
button to install Arduino driver.
After completed installation, you can run Arduino at the first. You may get security
confirmation. Please give a permission by clicking Allow access button.
free ebooks ==> www.ebook777.com
www.ebook777.com
free ebooks ==> www.ebook777.com
free ebooks ==> www.ebook777.com
1.6 Testing
For testing, I used Arduino Uno R3 on Windows 10.
www.ebook777.com
2. Windows Remote Arduino for Windows 10
free ebooks ==> www.ebook777.com
This chapter explains how to work with Arduino and Windows 10 for getting started.
free ebooks ==> www.ebook777.com
2.1 Setting Up Arduino on Windows 10
After you installed Arduino software, you can plugin Arduino board into computer via
USB.
Then you run Arduino software. In general it will detect Arduino hardware include
Arduino type and model.
www.ebook777.com
free ebooks ==> www.ebook777.com
However, if you get a problem about Arduino hardware driver, you update that driver.
You can update this device driver by navigating hardware driver on the driver folder of
Arduino software installation folder.
free ebooks ==> www.ebook777.com
2.1.2 Simple Testing
Now you’re ready to get started. For illustration, I use the sample code from Arduino,
Blink. You can visit on http://arduino.cc/en/Tutorial/Blink for configuration.
For this scenario, we use attached LED which is connected on Digital 13. Open Arduino
IDE.
On Arduino software, Click File -> Examples -> 01.Basics -> Blink.
www.ebook777.com
free ebooks ==> www.ebook777.com
Now try to connect your Arduino into computer. Select Arduino Uno for board target. You
can select it by clicking menu Tools -> Board : Arduino Uno.
www.ebook777.com
free ebooks ==> www.ebook777.com
Furthermore, you must select Arduino port. Select menu Tools -> Port -> (your Arduino
port which already connected).
Now you can compile and upload this program into Arduino board.
www.ebook777.com
2.2 Arduino and Windows Remote Arduino (WRA)
free ebooks ==> www.ebook777.com
To communicate between Arduino and computer/tablet/embedded board based Windows
10, we can use Windows Remote Arduino. There are two types of connectivity for
Windows Remote Arduino:
I will explain them on the next section. Now we must configure Arduino board and
computer to establish a connection.
www.ebook777.com
free ebooks ==> www.ebook777.com
By default, Firmata on Arduino uses a baud rate 57600. You can change it according to
your serial port (USB) or Bluetooth, for instance, 115200. After that, please compile and
upload this program into Arduino board.
www.ebook777.com
2.3 Windows Remote Arduino with USB
free ebooks ==> www.ebook777.com
In this section, we build a blinking LED program via USB using Windows Universal app.
The USB usage is applied as a bridge to remote Arduino board.
Our scenario needs the following items:
Three LEDs
USB cable
Jumper cables
Wiring
Creating a project
Adding reference files for Windows Remote Arduino
Configuring Windows Remote Arduino on project
Writing program
Testing
Let’s start!
2.3.1 Wiring
Connect three LEDs on Digital 12, 11, 10. Other LED pins are be connected to GND.
Now you connect Arduino board to Computer via USB cable.
free ebooks ==> www.ebook777.com
www.ebook777.com
free
If done, click OK button. ebooks ==> www.ebook777.com
Then, we add our compiled files from Windows Remote Arduino (read section 2.2.2).
Add the following files (depend on your target: ARM, x64, x86):
Microsoft.Maker.RemoteWiring.winmd
Microsoft.Maker.Firmata.winmd
Microsoft.Maker.Serial.winmd
free ebooks ==> www.ebook777.com
www.ebook777.com
free ebooks ==> www.ebook777.com
You also need to configure Package.appxmanifest file to enable Serial port capability.
Open Package.appxmanifest file and add the following script on <Capabilities>
</Capabilities> tag.
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
www.ebook777.com
free ebooks ==> www.ebook777.com
</Grid>
</Page>
www.ebook777.com
free ebooks ==> www.ebook777.com
connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);
txtStatus.Text = "Connected";
free ebooks ==> www.ebook777.com
}
You can see parameters on UsbSerial(). These values can be obtained from SerialPort
(USB) property which Arduino board is connected. For instance, My Arduino is
connected to COM3. You can open COM property from Device Manager.
www.ebook777.com
private void HandleUnchecked(object sender, RoutedEventArgs e)
{
free ebooks ==> www.ebook777.com
CheckBox cb = sender as CheckBox;
if (cb.Name == "chkLed1")
{
arduino.digitalWrite(LED1, PinState.LOW);
}
if (cb.Name == "chkLed2")
{
arduino.digitalWrite(LED2, PinState.LOW);
}
if (cb.Name == "chkLed3")
{
arduino.digitalWrite(LED3, PinState.LOW);
}
}
We also declare TurnOffLeds() to turn off LEDs. This is connected to button clicked
event.
private void TurnOffLeds(object sender, RoutedEventArgs e)
{
chkLed1.IsChecked = false;
chkLed2.IsChecked = false;
chkLed3.IsChecked = false;
}
this.Unloaded += MainPage_Unloaded;
InitWRA();
}
2.3.6 Testing
Run this program. If success, you can see our program UI. Try to do checked on
CheckBoxs. Then, you should see lighting LED.
free ebooks ==> www.ebook777.com
www.ebook777.com
2.4 Windows Remote Arduino with Bluetooth
free ebooks ==> www.ebook777.com
WRA also support using Bluetooth to remote Arduino. You can use Bluetooth module
from SparkFun Bluetooth Mate Silver, https://www.sparkfun.com/products/12576 .
Don’t forget to modify Firmata code on Arduino about baud rate.
Just connect your Bluetooth module on Tx and Rx from Arduino. Then, pair Bluetooth
module on Arduino to Bluetooth PC. Add Bluetooth capability on Package.appxmanifest
file.
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
On instantiate stage, you just construct connection object with BluetoothSerial() with
passing Bluetooth name.
connection = new BluetoothSerial("MyBluetooth");
connection.ConnectionFailed += Connection_ConnectionFailed;
arduino = new RemoteDevice(connection);
connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(115200, 0);
Just it!
Now you can program as usual with RemoteDevice object.
www.ebook777.com
3. Digital I/O
free ebooks ==> www.ebook777.com
In this chapter I’m going to explain how to remote Arduino Digital I/O using WRA.
free ebooks ==> www.ebook777.com
3.1 Getting Started
In this chapter, we learn how to access Arduino digital I/O via Windows Remote Arduino.
Our scenario is to use a pushbutton and LED. If pushbutton is pressed, LED will lighting.
You can find a pushbutton on the following online store:
Sparkfun, https://www.sparkfun.com/products/97
Adafruit, http://www.adafruit.com/products/1400
eBay, http://www.ebay.com
In this scenario, I use USB as bridge to Arduino. You can replace it by Bluetooth.
Let’s start!.
www.ebook777.com
3.2 Wiring
free ebooks ==> www.ebook777.com
Connect LED to Digital 12 on Arduino and pushbutton to Digital 11. The following is a
sample of wiring.
free ebooks ==> www.ebook777.com
3.3 Creating and Configuring a Project
Create a new project, called DigitalIoDemo. Follow instructions to create and configure
the project as explained on section 2.3.2, 2.3.3 and 2.3.4.
www.ebook777.com
3.4 Writing a Program
free ebooks ==> www.ebook777.com
We modify MainPage.xaml.cs. Add the following namespace.
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;
connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);
arduino.pinMode(LED, PinMode.OUTPUT);
arduino.pinMode(PUSHBUTTON, PinMode.INPUT);
arduino.DigitalPinUpdatedEvent += Arduino_DigitalPinUpdatedEvent;
InitWRA();
}
www.ebook777.com
3.5 Testing
free ebooks ==> www.ebook777.com
Now you can compile and run this program. For testing, try to press pushbutton. You
should see a lighting LED.
free ebooks ==> www.ebook777.com
4. Analog I/O
This chapter explains how to work with Arduino Analog I/O via WRA.
www.ebook777.com
4.1 Getting Started
free ebooks ==> www.ebook777.com
Arduino board provides Analog I/O which can be connected to sensor or actuator devices.
In this chapter, we try to access Arduino Analog I/O using Windows Remote Arduino.
There are two scenarios for our cases:
All scenarios use USB for connectivity between Arduino and PC.
Let’s start.
free ebooks ==> www.ebook777.com
4.2 Demo Analog Output: RGB LED
In this scenario we build a Windows Universal application to control RGB LED color
using Arduino Analog output (PWM). RGB LED has 4 pins that you can see it on Figure
below.
Note:
Pin 1: Red
www.ebook777.com
Pin 2: Common pin
Pin 3: Green free ebooks ==> www.ebook777.com
Pin 4: Blue
For Arduino Uno R3, you can see PWM pins as below.
free ebooks ==> www.ebook777.com
4.2.2 Wiring
For our testing, we configure the following PWM pins.
Arduino Mega 2560:
www.ebook777.com
free ebooks ==> www.ebook777.com
connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);
}
private void SetColor(ushort red, ushort green, ushort blue)
{
arduino.analogWrite(RED, red);
arduino.analogWrite(GREEN, green);
arduino.analogWrite(BLUE, blue);
}
A timer is used to running our RGB LED which consists of 6 color demo by calling
ColorDemo().
private void Connection_ConnectionEstablished()
{
System.Diagnostics.Debug.WriteLine("Connected");
arduino.pinMode(RED, PinMode.PWM);
arduino.pinMode(GREEN, PinMode.PWM);
arduino.pinMode(BLUE, PinMode.PWM);
index = 1;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += Timer_Tick;
timer.Start();
www.ebook777.com
private void Timer_Tick(object sender, object e)
{
free ebooks ==> www.ebook777.com
ColorDemo(index);
index++;
if (index > 6)
index = 1;
InitWRA();
}
private void MainPage_Unloaded(object sender, RoutedEventArgs e)
{
arduino.Dispose();
}
4.2.4 Testing
Compile and run the program. You should see several color on RGB LED.
The following is a sample demo on RGB LED.
free ebooks ==> www.ebook777.com
www.ebook777.com
4.3 Demo Analog Input: Working with Potentiometer
free ebooks ==> www.ebook777.com
In this section, we learn how to read analog input on Arduino board. For illustration, I use
Potentiometer as analog input source. Our scenario is to read analog value from
Potentiometer. Then, display it on app UI.
Let’s start!.
4.3.1 Wiring
To understand Potentiometer, you see its scheme in Figure below.
You can connect VCC to Arduino board VCC 5V. Vout to Arduino board Analog input
A0. In addition, GND to Arduino board GND. The following is hardware implementation.
I use slide potentiometer.
free ebooks ==> www.ebook777.com
</Grid>
</Page>
www.ebook777.com
using Windows.ApplicationModel.Core;
using Microsoft.Maker.Serial;
free ebooks ==> www.ebook777.com
using Microsoft.Maker.RemoteWiring;
connection.ConnectionEstablished += Connection_ConnectionEstablished;
connection.begin(57600, SerialConfig.SERIAL_8N1);
}
private async void UpdateData(ushort value)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.
() =>
{
txtVal.Text = Convert.ToString(value);
});
}
4.3.3 Testing
Compile and run this program. If success, you can see analog value on UI.
www.ebook777.com
5. Working with I2C
free ebooks ==> www.ebook777.com
In this chapter we learn how to work with I2C using Windows Remote Arduino.
free ebooks ==> www.ebook777.com
5.1 Getting Started
The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early ’80s to allow
easy communication between components which reside on the same circuit board. TWI
stands for Two Wire Interface and for most marts this bus is identical to I²C. The name
TWI was introduced by Atmel and other companies to avoid conflicts with trademark
issues related to I²C.
I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line).
Arduino Uno has I2C/TWI on Analog A4 (SDA) and Analog A5 (SCL).
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices.
You can find it on the following online store:
Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-
Conversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-
analog-converter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/
In addition, you can find this device on your local electronics store/online store.
www.ebook777.com
This module has mini form model too, for instance, you can find it on Amazon,
free ebooks ==> www.ebook777.com
http://www.amazon.com/WaveShare-PCF8591T-Converter-Evaluation-
Development/dp/B00KM6X2OI/ .
This module use PCF8591 IC and you can read the datasheet on the following URLs.
http://www.electrodragon.com/w/images/e/ed/PCF8591.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf
www.ebook777.com
5.2 Writing Program
free ebooks ==> www.ebook777.com
Firstly, create a new project, called I2CDemo. Follow instructions to create and configure
the project as explained on section 2.3.2, 2.3.3 and 2.3.4.
The first step is to develop app UI on MainPage.xaml. We add six TextBlock to display
Analog values. The following is complete code for MainPage.xaml file.
<Page
x:Class="I2CDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:I2CDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
</Grid>
</Page>
The next step is to modify MainPage.xaml.cs file. Firstly, we add our namespace.
free ebooks ==> www.ebook777.com
using System.Text;
using Windows.ApplicationModel.Core;
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;
Declare some variables. PCF8591 uses I2C address on 0x90. To get Thermistor value, we
send a command 0x40. Photo-voltaic value can be acquired by sending a command 0x41.
The last, Potentiometer, can be acquired using 0x43 command.
private UsbSerial connection;
private RemoteDevice arduino;
private const byte NUM_DIGITAL_PINS = 14; // Arduino Uno
private const byte SDA = 4;
private const byte SCL = 5;
private const byte PCF8591 = (0x90 >> 1); // Device address
private const byte PCF8591_ADC_CH0 = 0x40; // thermistor
private const byte PCF8591_ADC_CH1 = 0x41; // photo-voltaic cell
private const byte PCF8591_ADC_CH2 = 0x42;
private const byte PCF8591_ADC_CH3 = 0x43; // potentiometer
private int index = 0;
private Queue<int> i2cReading = new Queue<int>();
connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);
www.ebook777.com
timer.Tick += Timer_Tick;
timer.Start();
free ebooks ==> www.ebook777.com
}
On Tick event from Timer object, we read analog values from PCF8591. Then, we define
UpdateData() to update app UI.
private void Timer_Tick(object sender, object e)
{
switch (index)
{
case 0:
i2cReading.Enqueue(index);
System.Diagnostics.Debug.WriteLine("PCF8591_ADC_CH0"
ReadADC(PCF8591_ADC_CH0);
break;
case 1:
i2cReading.Enqueue(index);
System.Diagnostics.Debug.WriteLine("PCF8591_ADC_CH1"
ReadADC(PCF8591_ADC_CH1);
break;
case 2:
i2cReading.Enqueue(index);
System.Diagnostics.Debug.WriteLine("PCF8591_ADC_CH2"
ReadADC(PCF8591_ADC_CH2);
break;
}
index++;
if (index > 2)
index = 0;
InitWRA();
}
www.ebook777.com
5.3 Testing
free ebooks ==> www.ebook777.com
Now you can compile and run the program.
If success, you can see sensor values via I2C on app UI. The following is a sample output.
free ebooks ==> www.ebook777.com
6. Servo Motor
This chapter explains how to work with servo motor connected to Arduino board using
Windows Remote Arduino. We explore how to access PWM (Pulse Width Modulation)
Arduino.
www.ebook777.com
6.1 Servo Motor
free ebooks ==> www.ebook777.com
Servo motor provides a shaft movement 360 degree. We can control this movement based
on its degree. In this scenario, you can use any DC motor (servo) that will be connected to
Arduino. I used a mini servo from Arduino Sidekick Basic kit.
The following is a picture of my mini servo motor.
The next step we are going to build a Windows Universal with Arduino and servo motor.
free ebooks ==> www.ebook777.com
6.2 Wiring
To build hardware implementation, you can connect servo motor to Arduino by following
configuration:
www.ebook777.com
6.3 Writing A Program
free ebooks ==> www.ebook777.com
Firstly, create a new project, called ServoMotor. Follow instructions to create and
configure the project as explained on section 2.3.2, 2.3.3 and 2.3.4.
The first step is to develop app UI on MainPage.xaml. We add two TextBlock to display
servo motor angle. The following is complete code for MainPage.xaml file.
<Page
x:Class="ServoMotor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ServoMotor"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
</Grid>
</Page>
The next step is to modify MainPage.xaml.cs file. Firstly, we add our namespace.
using System.Text;
using Windows.ApplicationModel.Core;
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;
free ebooks ==> www.ebook777.com
Declare some variables. Servo motor uses PWM Digital 10. We also define a list of angle:
0, 30, 60, 90, 120, 150 and 180.
private UsbSerial connection;
private RemoteDevice arduino;
private const byte SERVO = 10;
private byte[] angles = new byte[] { 0, 30, 60, 90, 120, 150, 180
private int direction;
private bool fwd;
private DispatcherTimer timer;
connection.ConnectionEstablished += Connection_ConnectionEstablish
connection.begin(57600, SerialConfig.SERIAL_8N1);
On Tick event from Timer object, we read change servo motor angle. Then, we define
UpdateData() to update app UI.
private void Timer_Tick(object sender, object e)
{
System.Diagnostics.Debug.WriteLine("Dir " + direction.ToString() +
UpdateData(angles[direction]);
arduino.analogWrite(SERVO, angles[direction]);
if(fwd)
www.ebook777.com
direction++;
else
free ebooks ==>
direction--;
www.ebook777.com
if (direction > 6)
{
fwd = false;
direction = 6;
}
else
if (direction < 0)
{
fwd = true;
direction = 0;
}
}
InitWRA();
}
private void MainPage_Unloaded(object sender, RoutedEventArgs e)
{
connection.end();
arduino.Dispose();
}
www.ebook777.com
Source Code
free ebooks ==> www.ebook777.com
www.ebook777.com