Making Things Interactive

February 2, 2008

Fridge Entry Detector (Updated)

Filed under: 4: Counting Sensor Input,Cat Adams — catadams @ 10:21 pm

After being informed that I did not actually fulfill the assignment of creating something that counts (although I did learn a bit about photoresistors), I revisited the assignment and added the counting portion.  When the door is opened, and then closed again, a red LED turns on.  The machine counts to three.  (Still haven’t mastered binary yet.  Or mildly understood).


/*
*Sensor Count Assignment
 *Cat Adams
 */

int phoPin = 0; // photoresistor connected to Analog 0
int ledPin1 = 13; //LED connected to pin 13
int ledPin2 = 12;  //LED connected to pin 12
int redPinA = 2;
int redPinB = 3;
int redPinC = 4;
int threshold = 512; //value of at which phoPin switches states, half of greatest value of resistance
int value = 0; //variable to store the value coming from the sensor in Analog 0
boolean isBright = false; //isBright=when phoPin detects light
//false=when phoPin does not detect light
int count = 0;

void setup() {
  pinMode(ledPin1, OUTPUT); //set the ledPin as an Output
  pinMode(ledPin2, OUTPUT); //etc
  pinMode(redPinA, OUTPUT); //etc
  pinMode(redPinB, OUTPUT); //etc
  pinMode(redPinC, OUTPUT); //etc
  Serial.begin(9600);       //set up Serial Library at 9600 bps
  pinMode(phoPin, INPUT);   //sets the photoresistor as INPUT
}

void loop() {

  while (!isBright){             //while isBright is false
    delay(50);
    digitalWrite(ledPin1, HIGH); //green light is on
    digitalWrite(ledPin2, LOW);  //red light is off
    value = analogRead(phoPin);    //read the resistance value from the sensor
    if (value >= threshold) {    //if the value of the phoPin is >512
      delay(50);
      isBright = true;           //go to true state
    }
    Serial.println(value); //computer displays message
  }
  while (isBright){              // will continue looping as long as the condition is true
    delay(50);                   //debouncing
    value = analogRead(phoPin);    //reads resistance value again
    digitalWrite(ledPin1, LOW);  //green light is off
    digitalWrite(ledPin2, HIGH); //red light flashes
    Serial.println("get out of mah fridge"); //computer displays message
    delay(50);
    digitalWrite(ledPin2, LOW);
    if (value < threshold) {     //if the value of phoPin is <512
      isBright = false;          //begin count when isBright returns to false
      count++;                  // count = count+1
    }
  }

  switch (count){                //count state
  case 1:                        // case=if
    digitalWrite(redPinA, HIGH);
    digitalWrite(redPinB, LOW);
    digitalWrite(redPinC, LOW);
    break;                      //tells program not to move onto other cases if case 1 is true
  case 2:
    digitalWrite(redPinA, HIGH);
    digitalWrite(redPinB, HIGH);
    digitalWrite(redPinC, LOW);
    break;                      //tells program not to move onto other cases if case 2 is true
  case 3:
    digitalWrite(redPinA, HIGH);
    digitalWrite(redPinB, HIGH);
    digitalWrite(redPinC, HIGH);
    break;
  default:                      //if case 1,2 and 3 are all false
    count = 0;     //if count reaches four, count resets to 0
    digitalWrite(redPinA, LOW);
    digitalWrite(redPinB, LOW);
    digitalWrite(redPinC, LOW);
    break;                     //tells program not to move onto other cases if default is true
  }

}

&#91;sourcecode&#93;

This little project is used to deter someone from stealing from your fridge.  
While the door is closed, the green light is on.  
When the door is opened and the fridge light is theoretically on, the green light turns off and the red light flashes.  A message displays on the computer screen saying "Get out of mah fridge!"

&#91;sourcecode language='cpp'&#93;

int phoPin = 0; // photoresistor connected to Analog 0
int ledPin1 = 13; //select the pin for the LED
int ledPin2 = 7;
int value = 0; //variable to store the value coming from the sensor in Analog 0

void setup() {
pinMode(ledPin1, OUTPUT); //set the ledPin as an Output
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);      //set up Serial Library at 9600 bps
pinMode(phoPin, INPUT); //sets the photoresistor as INPUT
}

void loop() {
value = analogRead(phoPin);  //read the resistance value from the sensor
if (value <= 700) {          //if there is low light then...
digitalWrite(ledPin1, HIGH); //green light is on
digitalWrite(ledPin2, LOW);  //red light is off
}
else if (value >= 700) {     //if there is extreme light
digitalWrite(ledPin1, LOW);  //green light is off
digitalWrite(ledPin2, HIGH);  //red light flashes
delay(50);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(50);
Serial.println("get out of mah fridge"); //computer displays message
}
delay(50);
}
Advertisements

1 Comment »

  1. U NO CAN HAZ!

    Comment by jet — February 3, 2008 @ 12:20 am | 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: