Making Things Interactive

February 5, 2008

Save Energy: CLOSE THE DOOR ALREADY!

Filed under: 4: Counting Sensor Input,Brian Kish — bkish @ 10:02 pm
This is a project that utilizes a switch to tell whether a door is open or not.   It can be used on a refrigerator door or the exterior door  of your house to help  make sure that you are not wasting too much energy by leaving the door open.  When the switch is open  (meaning the door is open), the blue light starts to  flash faster and faster and it says "Door is open", as you wait longer the   green LED flashes and it says "You should close the door soon", finally the red light  flashes and tells you "CLOSE THE DOOR NOW!!  You are wasting energy".  IF the switch  is depressed (the door is closed), nothing happens and the energy fairy is happy.
Schematic of Wiring

http://www.youtube.com/watch?v=JIuhFRF08JY

/*
 *  Switch and LED test program
 */

int redLED = 9;                 // RedLED is connected to pin 9
int greenLED = 11;                // GreenLED is connected to pin 11
int blueLED = 13;                // BlueLED is connected to pin 13
int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int delayAmount = 1000;          // number of milliseconds to wait

void setup() {
  pinMode(redLED, OUTPUT);      // Set the redLED pin as output
  pinMode(greenLED, OUTPUT);    // Set the blueLED pin as output
  pinMode(blueLED, OUTPUT);     // Set the blueLED pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
}

void loop(){
  val = digitalRead(switchPin);    // read input value and store it in val
  if (val == HIGH) {               // check if the button is pressed
    Serial.begin(9600);
    Serial.println("The Door is Open");
    digitalWrite(blueLED, HIGH);   // turn blue LED on
    delay(1000);
    digitalWrite(blueLED, LOW);   // turn blue LED off
    delay(1000);
    digitalWrite(blueLED, HIGH);  // turn blue LED on
    delay(900);
    digitalWrite(blueLED, LOW);   // turn blue LED off
    delay(900);
    digitalWrite(blueLED, HIGH);  // turn blue LED on
    delay(600);
    digitalWrite(blueLED, LOW);   // turn blue LED off
    delay (600);
    digitalWrite(blueLED, HIGH);  // turn blue LED on
    delay (200);
    digitalWrite(blueLED, LOW);   // turn blue LED off
    delay (200);
    digitalWrite(blueLED, HIGH);  // turn blue LED on
    delay (200);
    digitalWrite(blueLED, LOW);   // turn blue LED off
    delay (200);
    Serial.begin(9600);
    Serial.println("You should close the door soon.");
    digitalWrite(greenLED, HIGH);   // turn green LED on
    delay(1000);
    digitalWrite(greenLED, LOW);   // turn green LED off
    delay(1000);
    digitalWrite(greenLED, HIGH);  // turn green LED on
    delay(900);
    digitalWrite(greenLED, LOW);   // turn green LED off
    delay(900);
    digitalWrite(greenLED, HIGH);  // turn green LED on
    delay(600);
    digitalWrite(greenLED, LOW);   // turn green LED off
    delay (600);
    digitalWrite(greenLED, HIGH);  // turn green LED on
    delay (200);
    digitalWrite(greenLED, LOW);   // turn green LED off
    delay (200);
    digitalWrite(greenLED, HIGH);  // turn green LED on
    delay (200);
    digitalWrite(greenLED, LOW);   // turn green LED off
    delay (200);
    Serial.begin(9600);
    Serial.println("CLOSE THE DOOR NOW!!! You are wasting energy!");
    digitalWrite(redLED, HIGH);   // turn red LED on
    delay(1000);
    digitalWrite(redLED, LOW);   // turn red LED off
    delay(1000);
    digitalWrite(redLED, HIGH);  // turn red LED on
    delay(900);
    digitalWrite(redLED, LOW);   // turn red LED off
    delay(900);
    digitalWrite(redLED, HIGH);  // turn red LED on
    delay(600);
    digitalWrite(redLED, LOW);   // turn red LED off
    delay (600);
    digitalWrite(redLED, HIGH);  // turn red LED on
    delay (200);
    digitalWrite(redLED, LOW);   // turn red LED off
    delay (200);
    digitalWrite(redLED, HIGH);  // turn red LED on
    delay (200);
    digitalWrite(redLED, LOW);   // turn red LED off
    delay (200);
  }
  else (val == LOW); {              // check if the button is not pressed
    digitalWrite(blueLED, LOW);    // turn blue LED off
    digitalWrite(greenLED, LOW);   // turn green LED off
    digitalWrite(redLED, LOW);     // turn red LED off
    Serial.begin(9600);
    Serial.println("The door is currently closed");
  }
  }
Advertisements

1 Comment »

  1. Here’s a hint: when you see lots of repetition in the code you are writing, you can shorten it by writing your own function (like loop and setup).

    You might define:

    void blinkBlue ( ) {
    digitalWrite(blueLED, HIGH); // turn blue LED on
    delay(1000);
    digitalWrite(blueLED, LOW); // turn blue LED off
    delay(1000);
    }

    (put it at the bottom of the code, after the “loop” function)

    Everywhere you now use this sequence:

    digitalWrite(blueLED, HIGH); // turn blue LED on
    delay(1000);
    digitalWrite(blueLED, LOW); // turn blue LED off
    delay(1000);

    You would now just say:
    blinkBlue( )

    Or .. if you wanted to be even fancier, you could define:

    void blinkBlue (delayTime) {
    digitalWrite(blueLED, HIGH); // turn blue LED on
    delay(delayTime);
    digitalWrite(blueLED, LOW); // turn blue LED off
    delay(delayTime);
    }

    and then you could call:

    blinkBlue (1000);

    Comment by mdgross — February 9, 2008 @ 6:21 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: