Working with many sensors

Rodolfo Cossovich
interface-lab
Published in
3 min readAug 7, 2020

In this example, we connect 3 sensors to Arduino: A push-button, a variable resistor and a light dependent resistor (LDR).

The first part, connecting and testing the components within Arduino IDE, uses the code snippet below. The video goes in greater detail about how to connect the two 10k ohm resistors and the 10k ohm potentiometer.

In Arduino IDE:

/*
ThreeSensors to connect with Unity
InterfaceLab 2020 - full instructions at https://medium.com/interface-lab/arduino/home
*/
int pushButton = 2; // digital pin 2 has a pushbutton with a pulldown of 10k attached to it.
int knobPin = A0; // potentiometer at A0
int lightPin = A1; // LDR sensor with 10k pull down at A1
void setup() {
Serial.begin(115200);
pinMode(pushButton, INPUT);
pinMode(knobPin, INPUT);
pinMode(lightPin, INPUT);
}
void loop() {
// read the inputs:
int buttonState = digitalRead(pushButton);
int knobValue = analogRead(knobPin);
int lightValue = analogRead(lightPin);
Serial.print(buttonState);
Serial.print(",");
Serial.print(knobValue);
Serial.print(",");
Serial.println(lightValue);
delay(50); // delay in between reads for stability
}

Then, we set up Unity to handle the serial port and we test that the messages are arriving within the SampleScene — Autopoll, provided in the Ardity asset. It is important to check the Build Settings/Player Settings/Other/API Compatibility Level to make sure it is not using NET xxx STANDARD. It should only say NET x.X.

In Unity, we need to parse the message that is written in a String variable (msg is the name in the code snippet below). With float.Parse, the strings could be converted to numbers. But they need to be separated comma by comma, for what we use the instruction Split with the parameter “,”. The result is not only one number but a set of numbers in an Array. To address each of these placeholders we use [n], with n as the index.

In order to modify objects directly, we can call them with the declaration “Public” and, within Unity, use the Inspector to address which object we are referring to.

It worths noting that within the script, to allow a better response from the physical world of turning left and right the knob to navigate with the cube ahead/backwards, we “calibrate” the input by subtracting half of the maximum total value, making the output (knob - 512). Similarly, we can scale values. As a demonstration of C# syntax, we use in the script the ? operator as a shorter notation of the if/else conditional.

/**
* Example for Unity: InterfaceLab 2020
* Ardity (Serial Communication for Arduino + Unity)
* Author: Daniel Wilches <dwilches@gmail.com>
*
* This work is released under the Creative Commons Attributions license.
* https://creativecommons.org/licenses/by/2.0/
*/
using UnityEngine;
using System.Collections;
/**
* When creating your message listeners you need to implement these two methods:
* - OnMessageArrived
* - OnConnectionEvent
*/
public class MyListener : MonoBehaviour
{
public GameObject cube;
public Light lamp;
private bool rotate;
private float speed;
private float lightSensed;
void Update(){
cube.transform.position += Vector3.forward * speed;
if (rotate) cube.transform.Rotate(0, 0, 10);
lamp.intensity = lightSensed;
}
// Invoked when a line of data is received from the serial device.
void OnMessageArrived(string msg)
{
// Debug.Log("Message arrived: " + msg);
string[] msgSplit = msg.Split(',');
rotate = (float.Parse(msgSplit[0]) > 0) ? true:false ;
speed = (float.Parse(msgSplit[1])-512) / 500;
lightSensed = float.Parse(msgSplit[2]) / 102;
}
void OnConnectionEvent(bool success)
{
if (success)
Debug.Log("Connection established");
else
Debug.Log("Connection attempt failed or disconnection detected");
}
}

--

--

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.