Alex MandelctSandra HelsleyAmir Mohtashami
Published

Fridgechat

ambient noise time capsule

Full instructions provided1,339
Fridgechat

Things used in this project

Hardware components

Arduino with voltage regulator
×1
Plastic
×1
Screws
×8
Nuts
×8
L-brackets
×8
Speaker (8ohm)
×1
Voice Recorder Breakout
×1
Sound amp
×1
9V battery (generic)
9V battery (generic)
×1
On/Off Switch
×1
Wires
×1
Resistors
×1
4.7 uF Capacitor
×1
White LED
×3
Photoresistor
×1
Magnets
×1

Story

Read more

Code

fridge.ino

C/C++
fridge.ino
#include <Wtv020sd16p.h>
#include <ISD.h>

//PINS
// As usual, we'll create constants to name the pins we're using.
// This will make it easier to follow the code below.

const int lightSensingPin = A0;

const int ledPin = 7;

// record/playback module
const int recordPlaybackPin = A1;
const int msgPins[] = {A5, A4, A3, A2};
const int numMsgs = 1;

int currentMsgToPlay, currentMsgToRecord;

// Light sensor threshold
// TODO: Fine tune magnitude by experimentation
const int LIGHT_THRESHOLD = 160;

// Record and playback length
const int RECORD_LENGTH = 5000;


// Internal State
enum State {
  IDLE_STATE,
  PLAYBACK,
  FINISHED_PLAYBACK,
  RECORD
} state;

long stateBeginTime;

void setup()
{
  // Set up Serial. We will print out debugging messages through Serial.
  Serial.begin(9600); 
  Serial.println("setup");

  // Initialize pin modes
  pinMode(lightSensingPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(recordPlaybackPin, OUTPUT);
  digitalWrite(recordPlaybackPin, LOW);
  for (int i = 0; i < numMsgs; i++) {
    pinMode(msgPins[i], OUTPUT);
    digitalWrite(msgPins[i], HIGH);
  }
  currentMsgToPlay = 0;
  currentMsgToRecord = 0;
  
  // Initialize state
  state = IDLE_STATE;
}


void loop()
{
  int lightLevel = analogRead(lightSensingPin); 
  long currentTime = millis();

  // Check if we need to transition to a new state. If so, do state-based behavior.
  switch (state) {
    // IDLE_STATE: nothing is happening currently.
    case IDLE_STATE:
      // Check the light sensor to see if the fridge was opened
      if (lightLevel > LIGHT_THRESHOLD){
        // The fridge was opened
        Serial.println("Fridge Opened.");
        handleFridgeOpenedEvent();
        // Update state
        state = PLAYBACK;
        stateBeginTime = currentTime;
      }
    break;

    // PLAYBACK: The recorded message is being played back currently.
    case PLAYBACK:
      // Check to see if the playback finished
      if (currentTime - stateBeginTime > RECORD_LENGTH) {
        // The playback finished
        Serial.println("Playback finished.");
        handlePlaybackFinishedEvent();
        // Update state
        state = FINISHED_PLAYBACK;
        stateBeginTime = currentTime;
      }
    break;

    // FINISHED_PLAYBACK: The recorded message finished playing. The fridge is still open currently.
    case FINISHED_PLAYBACK:
      // Check the light sensor to see if the fridge was closed
      if (lightLevel <= LIGHT_THRESHOLD) {
        // The fridge was closed
        Serial.println("Fridge Closed.");
        handleFridgeClosedEvent();
        // Update state
        state = RECORD;
        stateBeginTime = currentTime;
      }
    break;

    // RECORD: The device is currently recording.
    case RECORD:
      // Check to see if the recording time elapsed.
      if (currentTime - stateBeginTime > RECORD_LENGTH) {
        Serial.println("Record finished.");
        handleRecordFinishedEvent();
        // Update state
        state = IDLE_STATE;
        stateBeginTime = currentTime;
      }
    break;
  }
}


void handleFridgeOpenedEvent(){
  playback();
}


void handlePlaybackFinishedEvent(){
  digitalWrite(msgPins[currentMsgToPlay % numMsgs], HIGH);
  currentMsgToPlay = currentMsgToPlay + 1;
}


void handleFridgeClosedEvent(){
  record();
  digitalWrite(ledPin, HIGH);  
}


void handleRecordFinishedEvent(){
  digitalWrite(msgPins[currentMsgToRecord % numMsgs], HIGH);
  digitalWrite(ledPin, LOW);
  currentMsgToRecord = currentMsgToRecord + 1;
}


void record(){
  Serial.print("Recording msg ");
  Serial.println(currentMsgToRecord % numMsgs);
  // To record, set record/playback pin low, then select message by setting message pin low
  digitalWrite(recordPlaybackPin, LOW);
  digitalWrite(msgPins[currentMsgToRecord % numMsgs], LOW);
}


void playback(){
  Serial.print("Playing msg ");
  Serial.println(currentMsgToPlay % numMsgs);
  // To playback, set record/playback pin high, then select message by setting message pin low
  digitalWrite(recordPlaybackPin, HIGH);
  digitalWrite(msgPins[currentMsgToPlay % numMsgs], LOW);
}

Credits

Alex Mandel

Alex Mandel

4 projects • 1 follower
ct

ct

5 projects • 4 followers
Sandra Helsley

Sandra Helsley

2 projects • 2 followers
I'm a graduate student at the School of Information!
Amir Mohtashami

Amir Mohtashami

4 projects • 4 followers

Comments