Read multiple sensor values independently from arduino to unity
Hello,
I have a project with arduino uno + 3 ultrasonic sensors in differents directions. I want to read the 3 values from my sensors in arduino and send them independently in Unity 3d to move 3 different 3d objets. How can i do this? Its already working with one ultra sensor, serial port and readline(), but with 3 values my script just read all the values and my object move in the 3 directions. I would like to choose which value move my object. Value 1, value 2 or value 3, and attach the others values to another objects. Thanks you community!
My unity code:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
public class simplePrintPort : MonoBehaviour
{
public static SerialPort port = new SerialPort("/dev/cu.usbmodem641", 9600, Parity.None, 8, StopBits.One);
public int sensorValue;
public GameObject pushObject;
// Use this for initialization
void Start () {
port.ReadTimeout = 100;
if (!port.IsOpen)
{
try
{
port.Open();
print ("Port is open");
}
catch(TimeoutException)
{
}
}
else
Debug.LogError("Port already open.");
}
// Update is called once per frame
void Update () {
sensorValue = Convert.ToInt32(port.ReadLine());
print (sensorValue);
Vector3 temp = pushObject.transform.position;
temp.z = (sensorValue/10);
pushObject.transform.position = temp;
}
void OnDestroy()
{
// Close the port when the program ends.
if (port.IsOpen)
{
try
{
port.Close();
}
catch(UnityException e)
{
Debug.LogError(e.Message);
}
}
}
}
My arduino code
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int trigPin1 = 7;
int echoPin1 = 8;
int trigPin2 = 4;
int echoPin2 = 5;
int trigPin3 = 2;
int echoPin3 = 3;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void firstsensor(){ // This function is for first sensor.
unsigned int duration1, distance1;
digitalWrite (trigPin1, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin1, LOW);
duration1 = pulseIn (echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
Serial.print("1st Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
if (distance1 < 30) { // Change the number for long or short distances.
digitalWrite (ledPin1, HIGH);
} else {
digitalWrite (ledPin1, LOW);
}
}
void secondsensor(){ // This function is for first sensor.
unsigned int duration1, distance1;
digitalWrite (trigPin2, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin2, LOW);
duration1 = pulseIn (echoPin2, HIGH);
distance1 = (duration1/2) / 29.1;
Serial.print("2st Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
if (distance1 < 30) { // Change the number for long or short distances.
digitalWrite (ledPin2, HIGH);
} else {
digitalWrite (ledPin2, LOW);
}
}
void thirdsensor(){ // This function is for first sensor.
unsigned int duration1, distance1;
digitalWrite (trigPin3, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin3, LOW);
duration1 = pulseIn (echoPin3, HIGH);
distance1 = (duration1/2) / 29.1;
Serial.print("3st Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
if (distance1 < 30) { // Change the number for long or short distances.
digitalWrite (ledPin3, HIGH);
} else {
digitalWrite (ledPin3, LOW);
}
}
void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
delay(100);
}
Answer by oswin_c · Dec 22, 2016 at 06:32 PM
I expect your serial monitor will show something like (using your exact printouts): 1st Sensor: 10cm 2st sensor: 12cm 3st Sensor: 14cm
In that case, when you use ReadLine and then ConvertToInt32, I do not think you are going to get anything sensible out of that.
What I suggest is to first use String.Replace to remove the units, and String.Split to isolate each number from the rest of the string. We can then parse these and build a vector.
string[] splitSerial = port.ReadLine().Replace("cm", "").Split(" ");
//splitSerial[0] = 1st Sensor:, splitSerial[1] = 10cm, splitSerial[2] = 2st Sensor: and so on.
float Sensor1;
float Sensor2;
float Sensor3;
if (!float.TryParse(splitSerial[1].Replace("cm", ""), out Sensor1)) print ("Failed to parse Sensor 1");
if (!float.TryParse(splitSerial[3].Replace("cm", ""), out Sensor2)) print ("Failed to parse Sensor 2");
if (!float.TryParse(splitSerial[5].Replace("cm", ""), out Sensor3)) print ("Failed to parse Sensor 3");
Vector3 movementVector = new Vector3(Sensor1, Sensor2, Sensor3);
...I have been doing stuff like this since I was a little girl, I know your pain
On a side note, beware of using serial ports in C#. The .net framework support for serial ports is literally broken and may never be fixed -- you might want to look into SerialPortNet, but if .net works for you then don't bother.
Also, beware of garbage collection -- you might want to wrap your serial port code in a using statement.
Answer by mariusorion · May 17, 2019 at 05:59 PM
Use the photon-pixel coupling method, it is a new approach in science for sampling an unlimited number of sensors in parallel.
Basically, each sensor output is an LED. If you have 10000 sensors, all of them are inserted in a LED array, a LED matrix as the authors say. After that, the LED array is filmed by a video camera and the images are processed in real time by a computer. A software reads one pixel from each LED from the LED array and converts it to numerical values. So, your LED array will be converted in a matrix (with 10000 elements) filled with numbers that can be processed as you wish in your software. I don't know if I was explicit but you can read their article here: https://www.sciencedirect.com/science/article/pii/S2215016119300901
Note that classic multiplexing is serial, this approach is parallel.
Schematics of the Photon-pixel coupling from the original article
The photon-pixel coupling method is truly ingenious because it solves two main problems in engineering: an unlimited number of sensors and their parallel sampling at video rate frequencies. Just imagine, we can read as many sensors as we wish. What I wander is if we can adapt the photon-pixel coupling to Arduino. I am new in the world of microcontrollers but I know Arduino can support a cam, so it should be possible.
If you are a PhD student then: P.A. Gagniuc, C. Ionescu-Tirgoviste, R.G. Serban, E. Gagniuc. Photon-pixel coupling: A method for parallel acquisition of electrical signals in scientific investigations. MethodsX, 6:968-979, 2019.
Your answer
Follow this Question
Related Questions
Not Fully Understanding how To Correctly Receive LDR Arduino Data 0 Answers
Change Value Of UI Light Slider to Read At Specific Points Of Slider Value 1 Answer
How can I manage the information from a sensor photoresistor in arduino? 0 Answers
Arduino PIR sensor to unity 1 Answer
how is the Input.compass.rawVector affected by a magnet? 0 Answers