Unlike other Unity/Arduino plugins, Uduino is compatible with other Arduino libraries. The default Uduino sketch includes the Servo library.

To get started, connect your servo motor to GND, 5V and one of the PWN pin following the tutorial on Arduino Website.

We describe two ways of using a servo motor.

Simple method

With this built-in method, you can control the servo with the default Arduino code.

Servo.cs (Unity)

using UnityEngine;
using System.Collections;
using Uduino;

public class Servo : MonoBehaviour {

    public int servoPin = 11;
    [Range(0, 180)]
    public int servoAngle = 0;
    private int prevServoAngle = 0;

    void Start() {
      UduinoManager.Instance.pinMode(servoPin , PinMode.Servo);
    }

    void Update()
    {
        if (servoAngle != prevServoAngle) // Condition to not send data each frame 
        {
            UduinoManager.Instance.analogWrite(servoPin, servoAngle);
            prevServoAngle = servoAngle;
        }
    }
}

Advanced method

If you don't want to use the "simple" way provided by Uduino, you can create your own arduino sketch with specific commands to control the servo.
In this example, we add a command named "R" dedicated to turn the motor.

CustomServo.ino (Arduino)

#include<Uduino.h>
Uduino uduino("advancedBoard");

Servo myservo;

void setup()
{
  Serial.begin(9600);
  uduino.addCommand("turnLeft", turnLeft);
  uduino.addCommand("off", disable);
  myservo.attach(11);
}

void turnLeft() {
  char *arg;
  arg = uduino.next();
  myservo.write(atoi(arg));
}

void disable() {
  digitalWrite(11, LOW);
}

void loop()
{
  uduino.udapte();
  delay(15);
}

On Unity, we use the function Write() to send a specific command to our board. This command is named "R" and has the angle as parameter.

Servo.cs (Unity)

using UnityEngine;
using System.Collections;
using Uduino;

public class Servo : MonoBehaviour {

    [Range(0, 180)]
    public int servoAngle = 0;
    private int prevServoAngle = 0;

    void Update()
    {
        if (servoAngle != prevServoAngle) // Condition to not send data each frame 
        {
            UduinoManager.Instance.sendCommand("servoBoard",  "R",  servoAngle);
            prevServoAngle = servoAngle;
        }
    }
}

Example: Uduino\Examples\Advanced\Servo