Rebuilding...

Sensitive Buildings: Romantic Lighting Sensor · Sunday October 16, 2011

Assignment

The fourth assignment for Sensitive Buildings has the class creating a wireless romantic lighting sesnor with 2 xBees, an Arduino, an LED, and a photocell. The project, taken from chapter 4 of Rob's book is meant to guide it's user in detecting just the right level of lighting for romance. Not too bright, and not too dark.

Goal

Create a wireless romantic lighting sensor, and then make it trigger music playback in iTunes when the moment is just right.

Documentation

Once the xBees were configured using X-CTU, one as a Coordinator in API mode, and the other as a Router, it was just a matter of getting them wired up. The sensor unit had a photocell attached to pin 20.

The base station was attached to the RX and TX pins on an Arduino Uno. I initally tried to use one of my Arduino Pro Minis so that the entire thing would be on a single breadboard, but it just wasn't working for some reason. I included a couple of amber LEDs on the xBees to help with troubleshooting, but eventually I just switched to using a full sized Arduino, and mysteriously everything started working.

I was able to get the basic romantic lighting sensor working, using a green LED for feedback. However, I wanted mine to play some romantic music when the conditions were just right. I created 2 AppleScripts, one for initiating playback in iTunes, and another for pausing playback. I didn't want to music to begin and end abruptly, that's not sexy, so I made sure the music would fade in and out. I had some prior experience with AppleScript, but I found Doug's AppleScripts for iTunes and an Apple Support discussion really helpful with the iTunes specific stuff.



AppleScript Play Code

on run
	tell application "iTunes"
		set currentvolume to the sound volume
		set the sound volume to 0
		play playlist "Romance"
		repeat with i from currentvolume to 100 by 2
			set the sound volume to i
			delay 0.05
		end repeat
	end tell
end run

AppleScript Pause Code

on run
	tell application "iTunes"
		set currentvolume to the sound volume
		if (player state is playing) then
			repeat with i from currentvolume to 0 by -2
				set the sound volume to i
				delay 0.05
			end repeat
			pause
		end if
	end tell
end run

To trigger the AppleScripts I created a custom version of Giuseppe. Giuseppe is a desktop application that I made using Real Studio to monitor and manipulate serial and socket communication. I've used it for a number of projects over the years, including Lumens and Ada's Shop. I'm working on a new version now that should be ready for release shortly. With just a few additional lines of code, I had the Arduino controlling iTunes over the serial port.

Because I needed to keep the Arduino's hardware serial connection tied to my Mac, I switched the xBee over to a software port on the Arduino using NewSoftSerial. I'm simply sending "R"s and "N"s to the Mac to designate when the lighting is right. "R" is for romantic. "N" is for not romantic.

Arduino Code

#include <NewSoftSerial.h>
int led = 11;
int debugLed = 13;
int analogValue = 0;
byte rx = 6;
byte tx = 7;

NewSoftSerial mySerial(rx, tx);

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  pinMode(led,OUTPUT);
  pinMode(debugLed,OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available() >= 21) {
    if (mySerial.read() == 0x7E) {
      digitalWrite(debugLed, HIGH); 
      delay(10); 
      digitalWrite(debugLed, LOW);
      for (int i = 0; i<18; i++) {
        byte discard = mySerial.read();
      }
      int analogHigh = mySerial.read();
      int analogLow = mySerial.read();
      analogValue = analogLow + (analogHigh * 256);
    }
  }
  if (analogValue > 0 && analogValue <= 350) {
    digitalWrite(led, LOW);
    Serial.write("N");
  }
  if (analogValue > 350 && analogValue <= 750) {
    digitalWrite(led, HIGH);
    Serial.write("R");
  }
  if (analogValue > 750 && analogValue <= 1023) {
    digitalWrite(led, LOW);
    Serial.write("N");
  }
}

Video

Video Player

Video of me testing the romantic lighting sensor.

Next Steps

I still need to experiment with the xBee API mode outlined in Rob's book so that I can send commands from the base station to the sensor unit. I'll update this page with further documentation once I've completed that.

› Matt


Make A Comment
Name:
Website:
Message: