Introduction
Source code contains 4 projects:
- Arduino project (includes the way Arduino handles the commands and servo, led control)
dllArduinoConnection
: This is a library you can use to control Arduino by C# MotorController
: This is a DLL. You can use it alone for controlling your KinectV2 by Windows Forms application. WinKinectControl
: This program Windows Forms application to see all works together.
and more projects for making sure that everything is working fine:
- Microsoft demo for kinectV2 as example (from KinectV2 SDK)
This simple programs allow you to:
- Control your Arduino device using Kinectv2 .NET environment.
- It is useful for controlling Arduino using C#.
- Control Arduino using Microsoft Kinect Device V2.
Background
I see many examples for the Arduino and how it works and is programmed. For .NET Environment, I made a DLL to control the Arduino from C# using Serial Port using (USB cable ) comes with and Arduinoono. Also, I applied this in a project using Kinect v2 and Servo as a simple example.
Using the Code
The project is divided into 4 parts:
- Arduino Code
- Arduino-C# Interface
- Servo C# control
- Kinect Control Servo
Part 1: Arduino Program
For Arduino part program send messages as parameters over serial port, Arduino device is just waiting for serial data which contains the command. The most important line is this one:
val = Serial.read();
Then switch parameters read like this code:
void SwitchParam1()
{
switch (serialData.param1)
{
case 'm':
serialData.intParam3 = serialData.convertToInt(serialData.param3);
serialData.intParam4 = serialData.convertToInt(serialData.param4);
Motor(serialData.param2);
break;
case 'l':
Light(serialData.param2);
break;
}
}
Part 2: Arduino-C# Interface
This part in the code is made by C# using:
System.IO.Ports.SerialPort
to send data (Parameters) to the Arduino. Here is a simple example:
public string Send(string data,bool close,int waitForReplayMS=2000)
{
string strReturnData;
try
{
Send(data,false);
System.Threading.Thread.Sleep(waitForReplayMS);
strReturnData = Read();
System.Threading.Thread.Sleep(waitForReplayMS);
strReturnData = currentRead;
if (close)
BluetoothPort.Close();
return strReturnData;
}
catch (Exception ex)
{
throw ex;
}
}
Part 3: Servo C# Control
This is the code for control Servo connected to Arduino from C#.
It's similar to leds in C# for Arduino handle. You can see the handle in Arduino code because it converts the degree, Command to be Servo, etc.
public string MoveMotor(Motor motor,MotorAction action, int degree, int waitForReplay=2000)
string Command ="";
switch (action)
{
case MotorAction.MoveTo:
Command = MoveTo(motor, degree);
break;
case MotorAction.More:
Command = More(motor, degree);
break;
case MotorAction.Less:
Command = Less(motor,degree);
break;
}
try
{
string duinoReturn = blueTooth.Send(Command ,true, waitForReplay);
return duinoReturn;
}
catch (Exception ex)
{
throw ex;
}
Part 4: Kinect Control Servo
To use all together, here is an example (included in the source code) Controls Servo using the left hand.
if (hand.GetHandPosition(MotorController.utilities.Side.Left).x >=0)
{
MoveResult = moveMotor(dllArduinoConnection.clsMotor.Motor.leftMotor,
dllArduinoConnection.clsMotor.MotorAction.MoveTo,100);
}
Points of Interest
Now, you can run control your Arduino Servos, Ports from your Windows Application using dllArduinoConnection DLL. You can also use your Kinect Device in a Windows Forms Application using MotorController DLL.
Notes
- You have to upload the Arduino source attached in your Arduino before using the dllArduinoConnection DLL.
- You can just add a reference to MotorController DLL in your program to use KinectV2 in your Windows Forms Application.
History
I plan to control Arduino using Bluetooth nested of just serial port using USB, also add more commands to Arduino.