Lesson 30: Fridge door alarm
The door is open!
- Many fridges beep when the door stays open, because the cold air goes out and food can go bad.
- A light sensor inside sees when the door is open. The Arduino measures how long it has been open with
millis(). - Opening the door for a moment is fine: the alarm waits 3 seconds first.
Words for this lesson
| Word | Meaning |
|---|---|
| alarm | a sound that warns you |
| timer | something that measures how much time has passed |
| log | a list of what happened |
| ! | NOT: !wasOpen is true when wasOpen is false |
Now and last time
| Open now | Open last time | What it means |
|---|---|---|
| yes | no | the door just opened: start the timer |
| yes | yes | still open: check the timer |
| no | yes | just closed: stop the alarm |
| no | no | closed |
- The program keeps the last reading in
wasOpen. Comparing now with last time finds the moment something changes.
A timer with millis()
void setup() {
Serial.begin(9600);
unsigned long start = millis();
delay(1200);
Serial.println(millis() - start);
}
void loop() {
}
millis()counts the milliseconds since the board started. The difference of two readings is the time between them: about 1200.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
A light sensor inside the fridge (A0) sees light when the door is open. Opening the door for a moment is fine, but if it stays open for more than 3 seconds, the buzzer (pin 8) must sound until the door is closed. The starter beeps at once. Remember when the door opened (openSince = millis() at the moment it changes from closed to open) and sound the buzzer only after 3000 ms.
Clique em Executar para ver a saída aqui.
Dificuldade 5/5 · Moedas 400 + pequeno bônus na primeira conclusão hoje
The fridge also keeps a log. Each time the door opens, add 1 to count and print Opened: and the count. And the small lamp inside (pin 13) should be on while the door is open. The alarm works as before.
Clique em Executar para ver a saída aqui.