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
  }

}

[sourcecode]

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!"

[sourcecode language='cpp']

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);
}

counting code: where is everyone?

Filed under: 4: Counting Sensor Input, Assignments, Gee Kim — gskim @ 6:17 pm

This program has a input switch that counts how many people are in the room, and depending on the number of people in the room, the red and green LED will light up, and the computer will print a certain phrase.

The red LED stays lit and the computer prints “WHERE IS EVERYONE?!?” when the switch is hit 5 or less times.

The red LED turns off, the green LED lights up, and the computer prints “HOORAH! everyone is here.” when the switch is hit for the 6th to 10th time.

The green LED turns off, the red LED turns back on, and the computer prints “GET OUT! there are too many ppl here.” when the switch is hit more than 10 times.

 

/*
* Where is everyone? (count code)
*/

int switchPin = 2;          // switch connected to digital pin 2
int count = 0;
int ledPin = 8;             // red led connected to digital pin 8
int ledPin2 = 10;           // green led connected to digital pin 10

void setup()                // run once, when the sketch starts
{
Serial.begin(9600);         // set up Serial library at 9600 bps
pinMode(switchPin, INPUT);  // sets the digital pin as input to read switch
pinMode(ledPin, OUTPUT);    // sets the digital pin as output to read pin
pinMode(ledPin2, OUTPUT);   // sets the digital pin as output to read pin 2
}

void loop()                                         // run over and over again
{
int newCount = count + digitalRead(switchPin);      // show count only when +1 than previous count
if( (newCount == count + 1)) {                      // if newCount is +1 than previous count
Serial.print("Number of ppl in the room: ");        // than print "Number of ppl in the room:"
Serial.println(newCount);                           // print the value of new count
count = newCount;                                   //
if (count <= 5) {                                   // if count <= 5
digitalWrite(ledPin, HIGH);                         // than turn on red led
Serial.println("WHERE IS EVERYONE?!?");             // and print "WHERE IS EVERYONE?!?"
}
else if ((count >= 6) && (count <= 10)) {           // if count is >= 6 AND <= 10
digitalWrite(ledPin, LOW);                          // than turn off red led
digitalWrite(ledPin2, HIGH);                        // and turn on green led
Serial.println("HOORAH! everyone is here.");        // and print "HOORAH! everyone is here."
}
else if (count >=11) {                              // if count is >= 11
digitalWrite(ledPin2, LOW);                         // than turn off green led
digitalWrite(ledPin, HIGH);                         // and turn on red led
Serial.println("GET OUT! too many ppl here!");      // and print "GET OUT! too many ppl here!"
}
}
delay(2000); // read every 2 seconds
}

Blog at WordPress.com.