Creative Commons License

 

I originally created this project to sell it at the Unity Asset Store but decided to share it to you for free now.
It's working with Unity 2019.

Download the Unity project with an example scene from my GitHub repository: PhidgetsPlayground

Optional: You can download the source code of the two .DLLs here:

 

Table of Contents:

 

Requirements

To measure the distance with an infrared sensor/s you need three hardware components and a Windows PC:

  1. Phidgets I/O Board:

    1. PhidgetInterfaceKit 8/8/8 ( ID: 1018 ) or

    2. PhidgetTextLCD 20X2 with Integrated PhidgetInterfaceKit 8/8/8 ( ID: 1203 ) or

    3. PhidgetInterfaceKit 8/8/8 w/6 Port Hub ( ID: 1019 ) or

    4. PhidgetInterfaceKit 2/2/2 ( ID: 1011 )

      More boards will be supported in the future.

  2. Infrared ( IR ) Distance Adapter ( ID: 1101 )

  3. Sharp Distance Sensor

    1. Short (4-30cm) ( ID: 3520 ) or

    2. Medium (10-80cm) ( ID: 3521 ) or

    3. Long (20-150cm) ( ID: 3522 )

You can use as many sensors as you want, as long as your hardware board has enough input channels.

 

Infrared Sensor API

Please have a look at the file InfraredExample.cs for an example of how to communicate with the component InfraredSensor.

 Events

  • ev_OnSensorInputChange: Event that takes a method with float parameter to return the distance in cm currently measured by the sensor.

 

Set Methods

  • SetDataInterval(int): Method to set the delta time in ms between voltage ratio requests.

  • SetVoltageRatioChangeTrigger(float): Method to set the voltage ratio delta in V/V between requests.

 

Get Methods

  • GetDataInterval(): Method to get the delta time in ms between voltage ratio requests.

  • GetVoltageRatioChangeTrigger(): Method to get the voltage ratio delta in V/V between requests.

  • GetVoltageRatio(): Method to get the voltage ratio in V/V currently measured by the sensor.

  • GetDistance(): Method to get the distance in cm currently measured by the sensor.

 

Installation and Test

  1. Install and start the Phidget Control Panel with admin rights: https://www.phidgets.com/docs/Phidget_Control_Panel
    You need to install the Phidget Control Panel to use any Phidgets!

  2. In Unity open the scene InfraredSensor.unity located in the Assets -> SingularityDefense -> ExampleScenes folder.

  3. Connect your I/O Board with attached adapter/s and sensor/s to your computer.

  4. Select the gameObject InfraredSensor_Short. By default, the InfraredSensor component uses a short-range infrared sensor. Change it to medium or long by clicking and selecting it in the drop-down menu.

  5. Make sure the Device Port and Sensor Channel are correct. You can use the Phidget Control Panel to look up the ports and channels. If you connect only one board with one sensor both are values are 0 ( default ).

  6. Hit the Play button and select the gameObject InfraredSensor_Short to watch the sensor output in the Inspector.

 

Update the Phidgets22.NET.dll and Phidgets22.NET.xml ( optional )

  1. Browse to https://www.nuget.org/packages/Phidget22.NET/ in your favorite internet browser and click the copy to clipboard button symbol of the Package Manager tab.
    ( For example: Install-Package Phidget22.NET -Version 1.0.0.20180522 )

  2. Open Visual Studio from within your Unity project:
    Unity menu bar -> Assets -> Open C# Project

  3. Open the NuGet Console in VS:
    VS main bar -> Tools -> NuGet Package Manager -> Package Manager Console

  4. Paste this command in the Package Manager Console after the PM> and press the Enter key to execute the command. The package will be downloaded and installed.

  5. Back in Unity open your file explorer ( e.g. right-click in Project window -> Assets -> Show in Explorer ) and navigate to the root folder of your project. There you'll find a folder called Packages next to your Assets folder.

  6. Navigate to the folder net20 in this Packages folder ( in my case this is .....\IRTest\Packages\Phidget22.NET.1.0.0.20180522\lib\net20 ) and drag & drop the files Phidget22.NET.dll and Phidget22.NET.xml into any of your projects Assets subfolders. I've created one called Phidgets. ( ...../IRTestProject/Assets/Phidgets )

  7. Restart Unity.

 

Build

  • You need to add the scene InfraredSensor.unity as the first scene ( index 0 ) in the build settings. The .dlls will be automatically added. No more work to do.

  • Make sure the PC has the Phidget Control Panel installed.

 

If you think this kind of content is helpful, please:

 

UP