Sensor headphones, part 1

For a while now, I’ve been wanting a pair of headphones that automatically pause your music when you remove them. I’d been dreaming up complicated ways to do this, but my friend Kyle reminded me that it is easy and I should just do it. So, I put together a quickie project to do just so! It’s based on an OPTEK OPB606A distance sensor that I got from Jameco, which puts out a low voltage when an object is near it, and a high voltage when all is clear. I whipped together a really sketchy arduino+processing script to allow the sensor to turn the music on and off, and voilĂ , it’s done!

Well, almost. I left my headphones at work, so it will be more done tomorrow evening after I affix the sensor to them. But for now, you can whet your appetite on the circuit diagram aforementioned sketchy code, which are available after the break.
Here’s the circuit! There’s not much to it, except to use a 220 ohm resistor to limit the current supplied to the LED in the sensor, and a 10k ohm resistor to pull the output signal high when the sensor isn’t getting any light in:

Here is the Arduino portion of the sketchiness:

// Connect the distance sensor to this input pin
#define sensorInputPin 0
 
// This stores the state that the headphones were in the last
// time we checked them
boolean lastHeadphonesOn;
 
// Return true if the headphones were just taken off or put back on.
boolean checkHeadphoneState() {
 
  // If the sensor value is really high, it means that nothing
  // is near it, so the headphones must not be on your head.
  int sensorIn = analogRead(sensorInputPin);
  boolean headphonesOn = sensorIn < 800;
 
  // If our results are different from the last time we checked,
  // return true.
  if (headphonesOn != lastHeadphonesOn) {
    lastHeadphonesOn = headphonesOn;
    return true;
  }
 
  // Otherwise, return false.
  lastHeadphonesOn = headphonesOn;
  return false;
}
 
void setup() {
  Serial.begin(9600);
}  
 
void loop() {
  if (checkHeadphoneState()) {
    if (lastHeadphonesOn) {
      // headphones just put on
      Serial.println("headphones on!");
    }
    else {
      // headphones just removed
      Serial.println("headphones off!");
    } 
  }
}

And the Processing portion, to watch the serial port for headphone on/off messages, and control the Banshee music player

// Tell processing how to use a serial port
import processing.serial.*;
Serial myPort;
 
void setup() {
  // Open up the first available serial port, and set it to 9600 baud
  myPort = new Serial(this, Serial.list()[0], 9600);
}
 
void draw() {
    // Read from the serial port until we get a linefeed
    int lf = 10;
    byte[] inBuffer = new byte[200];
    myPort.readBytesUntil(lf, inBuffer);
 
    // If we actually got some text, check if it contains a message we were
    // waiting for
    if (inBuffer != null) {
      String myString = new String(inBuffer);
      if (myString.contains("headphones on!")) {
        // Hooray! Turn the music on
        println("Headphones on!!!");
        // Run the Banshee music program with the play command specified.
        // If banshee was already running, it starts up the music again.
        try{
          Runtime.getRuntime().exec("banshee --play");
        }
          catch(java.io.IOException e){
          println(e);
        }
      }
      else if (myString.contains("headphones off!")) {
        println("Headphones off!!!");
        // Run the Banshee music program with the pause command specified.
        // If banshee was already running, it just pauses the music.
        try{
          Runtime.getRuntime().exec("banshee --pause");
        }
          catch(java.io.IOException e){
          println(e);
        }
      }
    }
}
This entry was posted in tech. Bookmark the permalink.

5 Responses to Sensor headphones, part 1

  1. This is a really great idea! I could have used a pair of phones like this in college.

  2. Robin J. says:

    Matt, I think that’s pretty ding dong awesome :P

  3. L. Chen says:

    Wonder if this was patentable in the U.S. …

    If it was patentable, it’s likely too late, as it’s been more than 1 year after the date of the article.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>