Hey guys, I’m using arduino to read hc sr04 sensor.
Ultrasonic sensor.
On arduino It read correctly my script, but on unity its giving weird values, I’m getting correct distance on arduino, but on unity i receive everytime the same values (10, 50, 47…) when it must be near de value 100.
Here is my unity simple code
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class SerialRead : MonoBehaviour {
SerialPort sp = new SerialPort("COM7", 9600);
// Use this for initialization
void Start ()
{
sp.Open();
sp.ReadTimeout = 1;
}
// Update is called once per frame
void Update ()
{
if(sp.IsOpen)
{
try
{
print(sp.ReadByte());
}
catch(System.Exception)
{
throw;
}
}
}
}
Now my arduino code
#define echoPin 13 //Pino 13 recebe o pulso do echo
#define trigPin 12 //Pino 12 envia o pulso para gerar o echo
void setup()
{
Serial.begin(9600); //inicia a porta serial
pinMode(echoPin, INPUT); // define o pino 13 como entrada (recebe)
pinMode(trigPin, OUTPUT); // define o pino 12 como saida (envia)
}
void loop()
{
//seta o pino 12 com um pulso baixo "LOW" ou desligado ou ainda 0
digitalWrite(trigPin, LOW);
// delay de 2 microssegundos
delayMicroseconds(2);
//seta o pino 12 com pulso alto "HIGH" ou ligado ou ainda 1
digitalWrite(trigPin, HIGH);
//delay de 10 microssegundos
delayMicroseconds(10);
//seta o pino 12 com pulso baixo novamente
digitalWrite(trigPin, LOW);
//pulseInt lê o tempo entre a chamada e o pino entrar em high
long duration = pulseIn(echoPin,HIGH);
//Esse calculo é baseado em s = v . t, lembrando que o tempo vem dobrado
//porque é o tempo de ida e volta do ultrassom
long distancia = duration /29 / 2 ;
Serial.println(distancia);
}
Hi, Change sp.ReadByte() to sp.ReadLine()
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class SerialPortTest : MonoBehaviour
{
//Setup parameters to connect to Arduino
public static SerialPort sp = new SerialPort(“COM3”, 9600, Parity.None, 8, StopBits.One);
public int message2;
private float updatePeriod = 0.0f;
public GameObject pushObject;
// Use this for initialization
void Start ()
{
OpenConnection();
}
void Update()
{
message2 = int.Parse(sp.ReadLine());
print (message2);
}
//Function connecting to Arduino
public void OpenConnection()
{
if (sp != null)
{
if (sp.IsOpen)
{
sp.Close();
print("Closing port, because it was already open!");
//message = "Closing port, because it was already open!";
}
else
{
sp.Open(); // opens the connection
sp.ReadTimeout = 1000; // sets the timeout value before reporting error
print("Port Opened!");
// message = "Port Opened!";
}
}
else
{
if (sp.IsOpen)
{
print("Port is already open");
}
else
{
print("Port == null");
}
}
}
void OnApplicationQuit()
{
sp.Close();
}
}
Olá Fred
Estou iniciando os estudos do Arduino, e gostaria de aplicar os estudos a Unity, mas estou com um pouco de dificuldade de entender como funciona a comunicação entre as plataformas.
Poderia me sanar algumas duvidas?
1º Como o arduino manda a informação para a Unity? Seria com a Serial.println()?
2º Como a Unity recebe os valores? tenho que fazer a variável que irá receber, mas como ela recebe? Se tiver alguma materia só para me dar o norte, ja ajuda bastante.
Parabens pelo post/ projeto.
Muito agradecido
Hello Fred
It is initiating studies of the Arduino, and would like to apply studies Unity, but I have some difficulty understanding how communication works across platforms.
could you help me some doubts?
1 As the Arduino sends the information to the Unity? Would be to Serial.println ()?
- As the Unity receives values? I have to make the variable that will receive, but as it gets? If you have any material just to give me the north, already helps.
Congratulations for post / project.
Thank you very much