Nick Stanley
Published © GPL3+

Smart Fridge Light - Adabox 001

A fridge light that alerts you when the door's been open too long, with optional data logging. Inspired by Adabox 001.

BeginnerWork in progress2 hours732
Smart Fridge Light - Adabox 001

Things used in this project

Hardware components

Adafruit Feather Adalogger
×1
Adafruit NeoPixel FeatherWing
×1
Buzzer
Buzzer
×1
Temperature Sensor
Temperature Sensor
×1

Story

Read more

Schematics

Basic Schematic

For the lights and buzzer only version

Code

Fridge Light Basic

C/C++
Board powered by fridge outlet, flashes red light and sounds piezo after 10 seconds
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include "Timer.h"

#ifndef PSTR
 #define PSTR // Make Arduino Due happy
#endif

#define PIN_PIEZO A0
#define PIN_NEO 6
#define OPEN_TOO_LONG 10000

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(4, 8, PIN_NEO,
  NEO_MATRIX_TOP     + NEO_MATRIX_RIGHT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
  NEO_GRB            + NEO_KHZ800);

uint8_t RGBVal = 255;
uint16_t OnColor = matrix.Color(255,255,255);
Timer OpenTimer;
bool Alert = false;
bool Toggle = true;

void OpenAlert()
{
  Alert = true;
}

void Chirp()
{
  for(int i=0; i < 1000; i++)
  {
    digitalWrite(PIN_PIEZO, HIGH);
    delayMicroseconds(500);
    digitalWrite(PIN_PIEZO, LOW);
  }
}

void setup() 
{
  matrix.begin();
  matrix.setBrightness(255);
  matrix.show();
  OpenTimer.after(OPEN_TOO_LONG, OpenAlert);
}

void loop() 
{  
  if(Alert)
  {
    if(Toggle)
      OnColor = matrix.Color(255,0,0);
    else
      OnColor = matrix.Color(0,0,0);
    Chirp();
    Toggle = !Toggle;
  }
  matrix.fillScreen(OnColor);
  matrix.show();
  OpenTimer.update();
  delay(500);  
}

Fridge Light Advanced

C/C++
Board is powered by battery, so watch USB to detect door open/closed. Log the temperature every loop iteration
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include "Timer.h" //https://github.com/JChristensen/Timer
#include <SPI.h>
#include <SD.h>

#ifndef PSTR
 #define PSTR // Make Arduino Due happy
#endif

//Pin Definitions
#define PIN_PIEZO A0
#define PIN_NEO 6
#define PIN_USB_POWER D1
#define PIN_SDCARD 4
#define PIN_TEMPERATURE 0

//Timer 10 seconds
#define OPEN_TOO_LONG 10000

//Neopixels
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(4, 8, PIN_NEO,
  NEO_MATRIX_TOP     + NEO_MATRIX_RIGHT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
  NEO_GRB            + NEO_KHZ800);

//Global Variables
uint16_t OnColor;   //The color of the light, white, red or off
Timer OpenTimer;    //Timer to watch for 10 seconds
bool Alert = false; //Play piezo and flash lights red
bool Toggle = true; //Used to flash lights every other loop cycle
int TimerID;        //ID for tracking our timer
File Log;           //File to log our temperatures
bool FileError;     //File could not be opened/read/created/SD missing/etc

/*
 * FridgeOpen()
 * Start a 10 second timer, turn on the lights
 * Called when the USB power pin voltage is detected
 */
void FridgeOpen()
{
  TimerID = OpenTimer.after(OPEN_TOO_LONG, OpenAlert);
  OnColor = matrix.Color(255,255,255);
}

/*
 * FridgeClosed()
 * Turn the lights off, stop alert, stop timer
 * Called when the USB power pin voltage disappears
 */
void FridgeClosed()
{
  OpenTimer.stop(TimerID);
  OnColor = matrix.Color(0,0,0);
  Alert = false;
}

/*
 * OpenAlert()
 * Sets a flag to falsh the lights in the loop
 * Called when timer expires
 */
void OpenAlert()
{
  Alert = true;
}

/*
 * Buzz()
 * Turn piezo on/off quickly to generate square tone
 * For alerting the fridge is open
 */
void Buzz()
{
  for(int i=0; i < 1000; i++)
  {
    digitalWrite(PIN_PIEZO, HIGH);
    delayMicroseconds(500);
    digitalWrite(PIN_PIEZO, LOW);
  }
}

/*
 * setup()
 * Setup the Neopixel matrix and interrupts
 */
void setup() 
{
  //Neopixel setup
  matrix.begin();
  matrix.setBrightness(255);
  matrix.show();

  //Interrupt when door is opened/closed
  attachInterrupt(DigitalPinToInterrupt(PIN_USB_POWER), FridgeOpen, RISING);
  attachInterrupt(DigitalPinToInterrupt(PIN_USB_POWER), FridgeClosed, FALLING);

  //File IO, per adalogger tutorial 
  //https://learn.adafruit.com/adafruit-feather-32u4-adalogger/using-the-sd-card
  FileError = false;
  // see if the card is present and can be initialized:
  if (!SD.begin(PIN_SDCARD)) 
  {
    //Card Init Failed!
    FileError = true;
  }
  char filename[15];
  strcpy(filename, "LOG.TXT");
  Log = SD.open(filename, FILE_WRITE);
  if(!Log) 
  {
    //Could not create file!
    FileError = true;
  }
}

/*
 * loop()
 * Show the Neopixels as defined by interrupts and timers
 */
void loop() 
{  
  if(Alert) //The fridge has been open too long!
  {
    //Flash on/off
    if(Toggle)
      OnColor = matrix.Color(255,0,0);
    else
      OnColor = matrix.Color(0,0,0);
    Toggle = !Toggle;
    //Play piezo
    Buzz();
  }

  //Show the Neopixel after any updates
  matrix.fillScreen(OnColor);
  matrix.show();

  //Timer count
  OpenTimer.update();

  //Logging
  if(!FileError)
    log.print("Temperature = "); log.println(analogRead(PIN_TEMPERATURE));

  delay(500);  
}

Credits

Nick Stanley

Nick Stanley

5 projects • 13 followers
Software Engineer - System Integrator - Maker of Things

Comments