En este proyecto se realizo una conexión entre un control con 4 botones de colores usando Arduino y Unity, la idea principal es tener una interfaz de comunicación que sirva para hacer diferentes actividades de aprendizaje para niños.
Para la comunicación entre Arduino y Unity se uso el package Ardity que se puede encontrar en el Asset Store de Unity o descargar desde GitHub, gracias de antemano a Daniel Wilches por el desarrollo de Ardity.
Se puede encontrar más información sobre Aridity en los siguientes links:
Web Page: http://ardity.dwilches.com/ GitHub Project: https://github.com/DWilches/Ardity
A continuación vamos a encontrar el código que se utilizo en Arduino para realizar la comunicación, simplemente lo que hace es enviar un mensaje cuando se presiona alguno de los botones, estos mensajes son recibidos en Unity por medio de la función OnMessageArrived y luego los mensajes que llega se usan para activar otra función que con base en el botón presionado genera una acción.
/* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: - LED attached from pin 13 to ground - pushbutton attached to pin 2, 3, 4, 5 from +5V - 10K resistor attached to pin 2 from ground - Note: on most Arduinos there is already an LED on the board attached to pin 13. created 2005 by DojoDave <http://www.0j0.org> modified 28 April 2019 by Victor Cardona This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button*/unsignedlonglast_time = 0;
// constants won't change. They're used here to set pin numbers:constintbuttonPinBlue = 2; // the number of the pushbutton pinconstintbuttonPinRed = 3;
constintbuttonPinGreen = 4;
constintbuttonPinYellow = 5;
constintledPin = 13; // the number of the LED pin// variables will change:intbuttonStateBlue = 0; // variable for reading the pushbutton statusintbuttonStateRed = 0; // variable for reading the pushbutton statusintbuttonStateGreen = 0; // variable for reading the pushbutton statusintbuttonStateYellow = 0; // variable for reading the pushbutton statusvoidsetup() {
Serial.begin(9600);
// initialize the LED pin as an output:pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:pinMode(buttonPinBlue, INPUT);
pinMode(buttonPinRed, INPUT);
pinMode(buttonPinGreen, INPUT);
pinMode(buttonPinYellow, INPUT);
}
voidloop() {
// read the state of the pushbutton value:buttonStateBlue = digitalRead(buttonPinBlue);
buttonStateRed = digitalRead(buttonPinRed);
buttonStateGreen = digitalRead(buttonPinGreen);
buttonStateYellow = digitalRead(buttonPinYellow);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:if (buttonStateBlue == HIGH && millis() > last_time + 2000) {
// turn LED on:digitalWrite(ledPin, HIGH);
Serial.println("1"); // 1 = Bluelast_time = millis();
} if (buttonStateRed == HIGH && millis() > last_time + 2000) {
// turn LED on:digitalWrite(ledPin, HIGH);
Serial.println("2"); // 2 = Redlast_time = millis();
}
if (buttonStateGreen == HIGH && millis() > last_time + 2000) {
// turn LED on:digitalWrite(ledPin, HIGH);
Serial.println("3"); // 3 = Greenlast_time = millis();
}
if (buttonStateYellow == HIGH && millis() > last_time + 2000) {
// turn LED on:digitalWrite(ledPin, HIGH);
Serial.println("4"); // 4 = Yellowlast_time = millis();
}
}Los mensajes que se enviaron desde Arduino a Unity salen como un String pero luego en Unity se convierten a Int, debido a que se presentaban problemas de comunicación y en el Switch donde se trataba de activar la función según el mensaje que llegaba no ocurria nada, debido a esto se tomo la decisión de enviar numeros como String y luego convertirlo a Int usando el siguiente código:
publicvoidOnMessageArrived(stringmsg){Debug.Log("Arrived: "+msg);Debug.Log(msg);CambioColor(msg);}publicvoidCambioColor(stringclave){Debug.Log("Función Cambio Color");Debug.Log("El mensaje que llego es "+clave);intvalorEntero=int.Parse(clave);switch(valorEntero){case1:m_Material.color=Color.blue;iTween.ShakeScale(this.gameObject,newVector3(0.5f,0.5f,0.5f),2);Debug.Log("Parte Blue del Switch");break;case2:m_Material.color=Color.red;iTween.ShakeScale(this.gameObject,newVector3(0.5f,0.5f,0.5f),2);Debug.Log("Parte Red del Switch");break;case3:m_Material.color=Color.green;iTween.ShakeScale(this.gameObject,newVector3(0.5f,0.5f,0.5f),2);Debug.Log("Parte Green del Switch");break;case4:m_Material.color=Color.yellow;iTween.ShakeScale(this.gameObject,newVector3(0.5f,0.5f,0.5f),2);Debug.Log("Parte Yellow del Switch");break;}}Es importante configurar el puerto adecuadamente, tanto en el Arduino como en Unity, para hacerlo vamos al IDE de Arduino y buscamos el puerto en el cual esta conectado y ese mismo nombre lo debemos agregar en Unity.
Luego ese mismo nombre lo agregamos a Unity para que pueda funcionar bien la comunicación entre Arduino y Unity
Todo este ejemplo fue realizado en Mac, pero los mismos pasos se puede seguir en Windows.






