Bidirectional communication

Rodolfo Cossovich
interface-lab
Published in
2 min readAug 11, 2020

Arduino can be also used as an output using actuators, in this case, the onboard LED and a servo-motor.

In order to connect the servo-motor, we need to check the colors of the cables. The middle cable (generally red or dark orange) is power and it should be connected to 3.3V (or +). The other two are one ground (dark brown or black color) and one signal (yellow or light orange). The first test should be carried out with the example from Arduino IDE File/Examples/Servo/Sweep.

In the video we build a -very- basic interface that features the Serial.read and it will react from our Serial monitor window within Arduino IDE. We use the numbers 0 and 1 to open / close an imaginary door, using Serial.read() to receive the incoming character and a switch/case.

/* InterfaceLab 2020
Code for servo-motor
Using Servo example by BARRAGAN <http://barraganstudio.com>
and code sample from https://ardity.dwilches.com/
This example code is in the public domain.
modified 8 Nov 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>Servo myservo; // create servo object to control a servovoid setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
switch (Serial.read())
{
case '0':
myservo.write(0);
delay(500);
break;
case '1':
myservo.write(180);
delay(500);
break;
}
}

In order to control it from Unity, we simply need to send a message to Arduino and we do it with the instruction: serialController.SendSerialMessage(“1”);

The script shared here is also hosted at https://github.com/todocono/InterfaceLab2020 Unity Package that imports scenes into the project directly.

--

--

Rodolfo Cossovich
interface-lab

Rudi. From Argentina but with a dozen of years in China, and counting. My research interests are diverse as robotics, education and interactive arts.