PhidgetInterfaceKit 0/16/16 and Unity
Posted: Fri Apr 11, 2025 9:55 am
I'm trying to get the Interface Kit to be controlled by Unity. Right now I have a series of arcade buttons wired up. Input 0 properly triggers and output 0 properly lights an LED in the Phidgets Control Panel. Same for all the other outputs (12 total buttons). I'm using the 5v power from the input channel to send signal to all the inputs. I'm using an external 12v power source to light all the LEDs. All of that works fine in the Phidgets Control Panel.
I'm trying to get just one button to work in Unity to start. I have phidget22.dll in the right place.
Here is my button class:
With this, somehow LED1 is on at the start, and pressing button 0 will turn off LED1 until I release button 0.
Instead if I use this as my start function I get no lights at all:
What am I doing wrong? Do I need to tell Unity that this is an Interface Kit instead of a Unity Hub or something else?
Attached are the properties of my interface kit.
I'm trying to get just one button to work in Unity to start. I have phidget22.dll in the right place.
Here is my button class:
Code: Select all
using UnityEngine;
using Phidget22;
using System.Collections;
// https://www.phidgets.com/education/learn/projects/unity/buttons/
// https://www.phidgets.com/?prodid=1226#Tab_Code_Samples
public class PhidgetButton : MonoBehaviour
{
[SerializeField, Tooltip("The port numbers on the Phidget board")]
private int btnPort = 0, ledPort = 0;
[SerializeField] private bool initActive = true; // should the button be active at start?
[SerializeField] private float debounce = 0.25f; // how long to wait after a button press before registering a true press
private bool debounced = true;
private DigitalInput btn;
private DigitalOutput led;
private bool active = true; // true means input is measured
private void Start()
{
// button setup
btn = new DigitalInput();
btn.Channel = btnPort; // set the channel to the port number
// btn = new DigitalInput
// {
// HubPort = btnPort,
// IsHubPortDevice = true
// };
btn.Open(5000);
// led setup
led = new DigitalOutput();
led.Channel = ledPort; // set the channel to the port number
// led = new DigitalOutput
// {
// HubPort = ledPort,
// IsHubPortDevice = true
// };
led.Open(5000);
active = initActive; // init
}
private void Update()
{
if (active)
{
// light the LED if not pressing the button
led.State = !btn.State;
} else
{
led.State = false; // keep light off when not active
}
}
public void ActivateButton()
{
active = true;
}
public void DeactivateButton()
{
active = false;
}
public bool GetBtnState()
{
bool b = false; // default to false
if (active)
{
if (debounced)
{
b = btn.State; // report the current state
if (b) // if debounced and now it's true again, then start a new debounce delay
{
StartCoroutine(DebounceBtn());
}
}
}
return b;
}
private IEnumerator DebounceBtn()
{
debounced = false;
yield return new WaitForSeconds(debounce);
debounced = true;
}
//Required for Unity and Phidgets
private void OnApplicationQuit()
{
btn.Close();
btn = null;
led.Close();
led = null;
if (Application.isEditor)
{
Phidget.ResetLibrary();
}
else
{
Phidget.FinalizeLibrary(0);
}
}
}
Instead if I use this as my start function I get no lights at all:
Code: Select all
private void Start()
{
// button setup
btn = new DigitalInput();
// btn.Channel = btnPort; // set the channel to the port number
btn = new DigitalInput
{
HubPort = btnPort,
IsHubPortDevice = false
};
btn.Open(5000);
// led setup
led = new DigitalOutput();
// led.Channel = ledPort; // set the channel to the port number
led = new DigitalOutput
{
HubPort = ledPort,
IsHubPortDevice = false
};
led.Open(5000);
active = initActive; // init
}
Attached are the properties of my interface kit.