Making Things Interactive

May 12, 2008

Magic Lantern

Filed under: Assignments — tyang1 @ 8:15 am

Many times in a hotel room or even your own room, when you turn off the lights before going to bed you stumble over a few things before reaching your bed.  This is a night table next to your bed that will light up depending on what you are doing in the room.  The different states are:

1.  Turn off lights, leave the room:  night table lights off

2.  Turn off lights, stay in room but not going to bed yet (checking last minute email, etc): night table lights on

3.  Turn off lights, go to bed to read: night table lights on

4.  Turn off lights, go to bed to sleep: night table lights off

If at any time the room is bright, the night table lights will remain off no matter what your motions are.  

The sensors I used are: PIR sensor (very fickle!), ultrasonic sensor and photosensor. 

Photosensor: used to detect overhead light

PIR sensor: detects motion in the room

ultrasonic sensor: angled to detect if someone is sitting up in bed or not (sometimes when you read there is not enough motion for the PIR sensor to detect that someone is in the room)


int photoPin = 1;

int light;

float sens = 0.85;

int val = 0;

int pirPin = 3;

int calibrateTime = 60;                          // time given to PIR sensor to calibrate (10 -60 secs according to datasheet)

unsigned long lowIn;                         // the time when the sensor outputs a low impulse

unsigned long pause = 60000;                              // millisecondes the sensor has to be low before we assume all motion has stopped

boolean lockLow = true;

boolean takeLowTime;

int ultraPin = 5;                                // sets ultrasonic range finder to digital pin 5

int timecount = 0; 

int ultraVal = 0;

int ledPin = 11;                                 // sets LEDs in table to digital pin 11;

int ledActive = 12;                              // sets LED to digital pin 12; blinks when PIR sensor finishes calibrating


void setup() {


  pinMode(photoPin, INPUT);                      // declares photosenor as INPUT

  pinMode(pirPin, INPUT);                        // declares PIR sensor as INPUT

  digitalWrite(pirPin, LOW);                     // initially sets PIR sensor to LOW = no motion

  pinMode(ledPin, OUTPUT);                       // declares LEDs in table as OUTPUT

  pinMode(ledActive, OUTPUT);                    // declares calibration LED as OUTPUT


  val = analogRead(photoPin);                     // reads value of photosensor


  // Gives sensor some time to calibrate

  Serial.println("Calibrating PIR sensor ");      

  for (int i = 0; i < calibrateTime; i++) {       




  Serial.println(" SENSOR ACTIVE");               // notifies user when PIR is finished calibrating

  digitalWrite(ledActive, HIGH);                  // blinks LED when PIR finishes calibrating 


  digitalWrite(ledActive, LOW);


  digitalWrite(ledActive, HIGH);


  digitalWrite(ledActive, LOW);



int getPing() {                                    // sends a 10us pulse to activate the sonar

  pinMode(ultraPin, OUTPUT);                       // sets ultrasonic sensor to OUTPUT

  digitalWrite(ultraPin, LOW);                     // sends a LOW pulse  

  delayMicroseconds(2);                            // wait 2 microseconds

  digitalWrite(ultraPin, HIGH);                    // sends a HIGH pulse

  delayMicroseconds(5);                            // wait for 5 microseconds

  digitalWrite(ultraPin, LOW);                     // holdoff

  pinMode(ultraPin, INPUT);                        // switches signal pin to INPUT to listen for echo pulse

  return pulseIn(ultraPin, HIGH);                  //  returns echo singal i.e. object's distance



void loop() {


  light = analogRead(photoPin);                    // reads photosensor value and sets it to light variable

  ultraVal = getPing();                            // writes value of ultrasonic sensor from getPing to ultraVal variable

  Serial.println("pir value");




  if ((light < val * sens) || (light <= 250)) {              // if the room becomes darker or the amount of light is below threshold...


    if (digitalRead(pirPin) == HIGH) {                       // ... and there is MOTION 


        lockLow = false; 



      takeLowTime = true;

      if (ultraVal <= 2000) {                                // ... and the person is sitting up 

        digitalWrite(ledPin, HIGH);                          // ... LIGHTS ON



      else if (ultraVal > 2000) {                            // ... and the person is lying down

        digitalWrite(ledPin, LOW);                           // ... LIGHTS OFF



    else if (digitalRead(pirPin) == LOW) {                   // ... and there is NO MOTION

      digitalWrite(ledPin, LOW);                             // ... LIGHTS OFF

      if (takeLowTime){

        lowIn = millis();

        takeLowTime = false; 


      if(!lockLow && millis() - lowIn > pause){  

        //makes sure this block of code is only executed again after 

        //a new motion sequence has been detected

        lockLow = true;  





  else if (light <= 250 && ultraVal <= 2000) {                // my PIR is this is saying if there is someone in bed sitting up even if it detects no motion, then the lights will be on

    digitalWrite(ledPin, HIGH);



  else if (light > val * sens) {

    digitalWrite(ledPin, 0);


  else {                                                      // not really needed since the previous else statement should cover                                                                       all other logic but this is insurance

   digitalWrite(ledPin, 0);





Final prototype of Magic Lantern

Location of sensors on table topHow the electronics are fitted inside the table

Small models of tables whose forms would hide/embed the sensors better.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: