Will Clay
Published © GPL3+

Snack Saver

A motion sensor to be placed in a cabinet or drawer that sends you a notifcation whenever it's disturbed, along with displaying a message.

BeginnerFull instructions provided2 hours630
Snack Saver

Things used in this project

Hardware components

PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Photon
Particle Photon
×2
Buzzer
Buzzer
×1
Particle Adafruit SSD1306 OLED Screen (Incl with maker kit)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×2

Software apps and online services

Maker service
IFTTT Maker service
Slack
Slack

Story

Read more

Schematics

Motion Sensor

This is the wiring diagram for the photon directly connected to the motion sensor

Relay/OLED Screen/Buzzer

This is the wiring diagram for the photon that is connected to the OLED Screen and piezo buzzer

Code

Motion Sensor

C/C++
Use this code on the Photon that is connected directly to the motion sensor
int ledPin = D7;                 // choose the pin for the LED
int inputPin = D0;               // choose the PIR sensor pin
bool available;                  // status of motion sensor
int motionCounter = 0;           // variable to count motion events

Timer timer(30000, determineMotion); // checks status every 30 sec

void setup() {
  pinMode(ledPin, OUTPUT);       // set LED as output
  pinMode(inputPin, INPUT);      // set sensor as input

  timer.start(); // start the determineMotion timer
}

void determineMotion() {    // this function determines if there's motion
    if(motionCounter < 2) { // if very little motion was detected
        if(available == false) { // only publish if the status changed
            Particle.publish("roomstatus","Empty"); //publish to status to other photon/Google Docs
            }
        available = true; // set the status to available
    } else if (motionCounter >= 2) {
        if(available == true) { // only publish if the status changed
            Particle.publish("roomstatus","Full"); //publish status to other photon/Google Docs
            }
        available = false; // set the status to in use
    }
    motionCounter = 0; // reset motion counter
}

void loop() {
  if (digitalRead(inputPin) == HIGH) {  // check if the input is HIGH
    digitalWrite(ledPin, HIGH);         // turn LED ON if high
    motionCounter++;                    // increment motion counter
  } else {
    digitalWrite(ledPin, LOW);          // turn LED OFF if no input
  }
  delay(500);                           // wait 0.5s
}

Relay/OLED Screen/Buzzer

C/C++
Use this code for the photon that is connected to the OLED screen and piezo buzzer
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_SSD1306.h>



#define OLED_DC     D3//Defines which pin will be used for which input of the OLED Screen
#define OLED_CS     D4
#define OLED_RESET  D5
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);


int  x, minX;// variables for scrolling code
void setup(){
     Particle.subscribe("roomstatus", myHandler);//Subscribes to function from motion sensor photon
  display.begin(SSD1306_SWITCHCAPVCC);//Turns on the OLED screen at high voltage every time

  display.setTextSize(1);       // text size
  display.setTextColor(WHITE); // text color 
  display.setTextWrap(true);// turn off text wrapping to allow scrolling
  x    = display.width(); // set scrolling frame to display width
}

void  myHandler(const char *event, const char *data) { //this function determines what will be displayed on screen, and what publish to make to webhook
    if (strcmp(data,"Full")==0) {//If the other photon sends a message saying motion has been detected
    tone(D0,5000,100); //makes the beeper make a noise when motion is detected
    delay(200);
   display.clearDisplay();//Clears whatever was already on the display
    display.setCursor(x/6, 7);//sets cursor 1/6 of the way across the screen before printing text
    display.print("Eat those pop tarts I dare you");//Put whatever text you want to print here
    display.display();
    if(--x < minX) x = display.width()*2;
    Particle.publish("room_full");//Publishes a function that will be picked up by a slack webhook to send notifications to your phone
} else if (strcmp(data,"Empty")==0) {//If the other photon sends a message saying motion has not been detected
    display.clearDisplay();
    display.setCursor(x/8, 7);
    display.print("Close the cabinet and forget you saw this");
    display.display();
    if(--x < minX) x = display.width()*2;
    Particle.publish("room_empty");//Publishes a function that will be picked up by a slack webhook to send notifications to your phone
  }
 
}

Credits

Will Clay
1 project • 2 followers
Makin stuff to make life easier

Comments