Arduino PIR sensor to unity
Hi, I'm novice and I try to send Arduino Pir motion sensor values to Unity.5. What I want to do its: When someone approach, stay front and so activate the IR sensor that change the scene in Unity3D. When this person leaves the detection zone, the original scene or another scene is loaded. Without use UNIDUINO, how I can do this?
For the moment just my sketch arduino works, I follow this tutorial > https://www.youtube.com/watch?v=vJgtckLzoKM but it is really the good one for what i want to do?
Thanks community!
Answer by xflagx · Oct 24, 2016 at 09:03 PM
Here you will find an excellent tutorial targeting your case!
http://www.alanzucconi.com/2015/10/07/how-to-integrate-arduino-with-unity/
I try to understand this tutorial but finally Its not too easy for me. I have an arduino error about "errorhandler" and the final patches are too difficult to know. I'm lost in the code...@xflagx
First at all, stay calm :)
I recommend to make the arduino part as simple as possible... just read the sensor data and send it over the serial port use a delay of 50-100ms between the sensor samples...
Post the arduino code here... so we could take a look at it... Please be a bit more specific towards the error... I will provide you an complete example which should work out of the box See here... https://github.com/xfleckx/ReactionTimeExperiment you just need to replace the logic for the button press with your sensor data...
Thanks you to help me @xflagx i'm very novice and i am thinking is more easy to program. :) Ok I keep calm..
So, in beggining I try to make this arduino tutorial video https://www.youtube.com/watch?v=vJgtckLzo$$anonymous$$$$anonymous$$ ( i success ) and this https://www.youtube.com/watch?v=of_oLAvWfSI tutorial video and try to combine them and edit for my sensor ( for PIR sensor, not for buttons) but i did'nt success.
I buy UNIDUINO to help me but is not so clear and I have always errors. I make a simple count in arduino and via serialport i print this count in unity, thats works but how i can set up unity to read the on/off of my PIr sensor and use this on/off to make what i want?
Here my last messy script!!!! :DD
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Uniduino;
using System.IO.Ports;
[RequireComponent(typeof(AudioSource))]
public class uniduinopotard : $$anonymous$$onoBehaviour {
SerialPort str= new SerialPort("/dev/cu.usbmodem411", 9600);
public Arduino arduino;
private AudioSource audio;
public $$anonymous$$ovieTexture movie1;
public $$anonymous$$ovieTexture movie2;
public bool loop;
public int pin = 2;
public int pinValue;
void Start( )
{
str.Open(); // Ouvre le serialport str
str.ReadTimeout = 1;
// GetComponent<RawImage>().texture = movie1 as $$anonymous$$ovieTexture;
// audio = GetComponent <AudioSource> ();
// audio.clip = movie1.audioClip;
// movie1.Play ();
// movie1.loop = true;
arduino = Arduino.global;
arduino.Setup(ConfigurePins);
arduino.reportDigital(0, 1);
}
void ConfigurePins( )
{
arduino.pin$$anonymous$$ode(2, Pin$$anonymous$$ode.INPUT);
}
void readData ()
{
if (!str.IsOpen) // Si str n'est pas ouvert, ouvre-le!
{
str.Open ();
}
{
try
{
Debug.Log(str.ReadLine());
}
catch(System.Exception) {}
}
}
void Update ()
{
pinValue = str.ReadLine (); //<<<<< i can do that but its precisely what i want to do how can i ???
if (pinValue == 0)
{
GetComponent<RawImage>().texture = movie1 as $$anonymous$$ovieTexture;
audio = GetComponent <AudioSource> ();
audio.clip = movie1.audioClip;
movie1.Play ();
movie1.loop = true;
movie2.Stop ();
}
if (pinValue == 1)
{
GetComponent<RawImage>().texture = movie2 as $$anonymous$$ovieTexture;
audio = GetComponent <AudioSource> ();
audio.clip = movie2.audioClip;
movie2.Play ();
movie2.loop = true;
movie1.Stop ();
}
}
}
since C# is statical typed 'pinValue = str.ReadLine()' will not work... str.ReadLine() returns a String object which could be parsed to a Int with Int.Parse or Int.TryParse
var aStringFromSerialPort = str.ReadLine (); int aTempValue; if( ! int.TryParse(aStringFromSerialPort, out aTempValue)){ // do some Error Handling since the String did not contain a number }else{ pinValue = aTempValue; // the string could be parsed and can be interpreted... }
if this is not the only issue... please post all errors you get!
Your answer
![](https://koobas.hobune.stream/wayback/20220612095313im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I manage the information from a sensor photoresistor in arduino? 0 Answers
Read multiple sensor values independently from arduino to unity 2 Answers
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 to use RaycastHit ? 0 Answers