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("
");
firstsensor();
secondsensor();
thirdsensor();
delay(100);
}