In most of the case, for simple prototypes, the default Uduino sketch is enough. However, when you develop a custom interactive piece, you sometimes need an optimized code.

For that, Uduino allows you to register custom commands on the Arduino side. These commands can be called from Unity and used to return a sensor value, trigger a tangible event and even contains parameters.

Arduino :

Register these commands can be done in the setup function, by using uduino.addCommand("myCommand", GetFunction);.

  1. "myCommand" correspond to the name of the command. That's the name you are going to call from unity. This name must not contain any space.
  2. GetFunction is the Arduino function that you want to launch

If you want to send back information to Unity, you can simply use Serial.Println() function. Be aware that if you do that all the time in the loop function, you will most likely overload the serial buffer.

Unity

On Unity you can communicate with the Arduino by calling the functions UduinoManager.Instance.sendCommand("myCommand");. It will simply trigger the Arduino command. Please refer to the documentation or the other examples.

Register a command

This simple example shows how to register a custom command. When the command "myCommand" is called from Unity, the function GetVariable prints the analog value.

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

void setup()
{
  Serial.begin(9600);
  uduino.addCommand("myCommand", GetVariable); // Command registered !
}

void GetVariable() {
  Serial.println(analogRead(A0));
}

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

Because the function GetVariable returns a value, don't forget to use the Read method on Unity.

Get the variable on Unity

Don't forget to add the Delegates, catching the returned values.

    void Awake()
    {
        UduinoManager.Instance.OnValueReceived += OnValueReceived; // Create the Delegate
    }

    void Update()
    {
        UduinoManager.Instance.Read("myArduinoName", "myCommand"); // Read every frame the value of the "myCommand" function on our board. 
    }

    void OnValueReceived(string data, string device)
    {
        Debug.Log(data); // Use the data as you want !
    }

If you prefer, you can also use the sort way to receive data. See Examples/Advanced/OneLineRead/

Trigger a command with parameters

Arduino custom commands can also have parameters. You declare the commands the same way, the only difference will happen inside the called function.
On Unity, the parameters are space-delimited. Your command to then will then be: "myCommand param1 param2 ...". The functiongetNumberOfParameters()returns the number of parameters. The functiongetParameter(x)returns the parameter ath the poxitionx`.

Unity
UduinoManager.Instance.sendCommand("myCommand", 10, 3);
Arduino
void setup() {
  uduino.addCommand("myCommand", Command);
}

void Command() {
  int parameters = uduino.getNumberOfParameters(); // returns 2
  if(parameters > 0) {
    int valueOne = uduino.charToInt(uduino.getParameter(0)); // returns 10
    int valueTwo = uduino.charToInt(uduino.getParameter(1)); // returns 3
  }
}

More information on the Arduino script reference

Char pointer

You can also use a char pointer char * to navigate through the parameters.

void setup() {
  uduino.addCommand("myCommand", SetVariable);
}

void SetVariable() { // Note that you don't need to add any parameter 
  char *arg;
  arg = uduino.next(); // arg now points to the first parameter
  if (arg != NULL)
    analogWrite(9, uduino.charToInt(arg));

  arg = uduino.next(); // arg now points to the second parameter
  if (arg != NULL)
    analogWrite(6, uduino.charToInt(arg));
}

Char pointer

You can also use a char pointer char * to navigate through the parameters.

void setup() {
  uduino.addCommand("myCommand", SetVariable);
}

void SetVariable() { // Note that you don't need to add any parameter 
  char *arg;
  arg = uduino.next(); // arg now points to the first parameter
  if (arg != NULL)
    analogWrite(9, uduino.charToInt(arg));

  arg = uduino.next(); // arg now points to the second parameter
  if (arg != NULL)
    analogWrite(6, uduino.charToInt(arg));
}