Volume Control using Phidgets Sensors

Phidgets.com offers USB sensors which can be controlled by .NET. They also offer the interface kit with which you can use the sensor. You can get variety of sensors like vibration, light, IR, voltage, temperature, PH, rotation etc. For this blog entry I have bought a Phidget Interface kit with LCD attachment. This kit has 8 digital input/output and 8 analog sensors and with 2 line LCD attachment as display.

Phidgets.com provides the .NET assemblies to control the interface kit and to read the input from the attached sensors. I have always wanted a physical volume control for my laptop which is missing, I have to either use windows volume control or function keys to control the volume. The rotation sensor provides a old school way of changing the volume. The following is a picture of the rotation sensor along with the interface kit. You can see the digital inputs, outputs and analog sensor ports, also notice that I have attached my sensor to port 7.

RotationSensor

Now for the software to read the sensor values and to adjust the volume, I have created a C# program to do this. Primarily, I am leveraging a piece of code from Microsoft forums that has the implementation of IAudioEndpointVolume which is the interface for volume related functions on Vista. All that is left is to instantiate the interface kit and listen for sensor changes. The following code snippet illustrates the same.

    1: private void Form1_Load(object sender, EventArgs e)
    2: {
    3:     ifKit = new InterfaceKit();
    4:     ifKit.Attach += new AttachEventHandler(ifKit_Attach);
    5:     ifKit.SensorChange += new SensorChangeEventHandler(ifKit_SensorChange);
    6:     ifKit.open();
    7: }

In the sensor change method, I convert the value and pass it on the volume control class and change the vista master volume.

    1: void ifKit_SensorChange(object sender, SensorChangeEventArgs e)
    2: {
    3:     //checking which sensor triggered the change
    4:     //in this case the rotation sensor is attached to the 8th input
    5:     if (e.Index == 7)
    6:     {
    7:         //converting the sensor value to float
    8:         //sensor value could be anywhere from 0-1000
    9:         //in case of rotation sensor right side twist decreases the value
   10:         //thus we are substracting it from 1000 to get a higher 
   11:         //value when rotating right side
   12:         float fVolume = (float)(1000-e.Value) / 1000;
   13:  
   14:         //float value is passed to the SetVolumeScalar method which sets 
   15:         //the master volume
   16:         objMasterVol.SetVolumeScalar(fVolume);
   17:  
   18:         //updating the UI
   19:         SetTrackbarVolume();   
   20:     }
   21:  
   22: }
   23:  
   24: /// <summary>
   25: /// Method update the UI
   26: /// </summary>
   27: private void SetTrackbarVolume() 
   28: {
   29:     //getting the current master volume
   30:     //returned in a range of 0 to 1
   31:     float vol = objMasterVol.MasterVolume;
   32:     //multiplying by 100
   33:     int iVol = (int)(vol * 100);
   34:     //setting the trackbar value
   35:     trackVolume.Value = iVol;
   36:     lblLevel.Text = trackVolume.Value.ToString();
   37: }

Very simple to achieve, there are other types of sensors such as Light Sensor, Vibration sensor etc, which could result in more cool ways to change the volume. Full code is attached below, please use the code at your own risk.

Thanks
Anil Revuru

Links:
Phidgets Site - www.phidgets.com
Phidgets 8/8/8 interface kit with LCD - https://www.phidgets.com/products.php?product_id=1203
Phidgets Rotation Sensor - https://www.phidgets.com/products.php?product_id=1109
Vista Volume Control C# code - https://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1287868&SiteID=1

VolumeControl.zip