Opening Refrigerator Detection
Situation: The refrigerator door is closed.
Decision: If the refrigerator opens and it's been opened several times within a short amount of time and there is nothing inside or nothing has been taken out, then blink the red LED and play an ominous tune.
Action: Execute script that blinks LED and plays ominous tune.
I began my experiment with a simplified version of the decision: just detecting whether or not the refrigerator door has been opened. I would figure out how to determine if nothing has been taken out later.
Initially, I had a couple of ideas for how to detect whether or not the refrigerator has been opened: sense changes in temperature, changes in light intensity levels, or motion of the door swinging.
I eventually decided to use the photoresistor to detect changes in light intensity from the outside of the refrigerator and a paper towel roll to point the sensor at the refrigerator door and block out ambient light.
Circuit
Code
const int ledPin=9;
const int sensorPin = A0;
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
// initialize serial communications (for debugging only):
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin,INPUT);
}
void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the analog input range (in this case, 70 - 400 from the photoresistor)
// to the output LED range (0-255)
if (sensorReading <= 950) {
digitalWrite(ledPin, HIGH);
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
No comments:
Post a Comment