Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

/*Sakshi Kulkarni

Design Statement:
This code is designed to run an RFID enabled pet feeder.
When the button is pressed the box opens and green LED flashes.
When button is pressed again, box closes and red LED flashes.
If the pet is approaching and within 30 cm, the RFID sensor scans the chip on the pet's collar.
If th chip ID matches the ID in the database, the box will open and a green LED will flash.
If it is not a match, the box will remain closed and a red LED will flash.
Once the box is open, the sonar verifies if the pet is still present
and will keep the box open until the pet is more than 30 cm away from sonar.
The stepper motors drive the hinge linkage on either side of the box.
*/
// BUTTON INITIALIZATION
int buttonPin = A0;
int buttonState = LOW; // variable for reading the pushbutton status
//button
int prevButtonState = HIGH;

// STEPPER MOTOR INITIALIZATION
// Include the AccelStepper Library
#include <AccelStepper.h>

...

/*Sakshi Kulkarni

Design Statement:
This code is designed to run an RFID enabled pet feeder.
When the button is pressed the box opens and green LED flashes.
When button is pressed again, box closes and red LED flashes.
If the pet is approaching and within 30 cm, the RFID sensor scans the chip on the pet's collar.
If th chip ID matches the ID in the database, the box will open and a green LED will flash.
If it is not a match, the box will remain closed and a red LED will flash.
Once the box is open, the sonar verifies if the pet is still present
and will keep the box open until the pet is more than 30 cm away from sonar.
The stepper motors drive the hinge linkage on either side of the box.
*/
// BUTTON INITIALIZATION
int buttonPin = A0;
int buttonState = LOW; // variable for reading the pushbutton status
//button
int prevButtonState = HIGH;

// STEPPER MOTOR INITIALIZATION
// Include the AccelStepper Library
#include <AccelStepper.h>

...

int motorPosition = 400;

/* RFID SENSOR layout
RST/Reset RST 47 brown
SPI SS SDA(SS) 53 yellow
SPI MISO MISO 50 orange
SPI MOSI MOSI 51 white
SPI SCK SCK 52 purple
3.3 V purple
GND black
*/
#include "SPI.h"
#include "MFRC522.h"

#define SS_PIN 53
#define RST_PIN 47

MFRC522 rfid(SS_PIN, RST_PIN);
bool box;

//SONAR INITIALIATION
/* SONAR SENSOR Layout
VCC -> 5V white
Trig -> port 38 blue/purple
Echo -> port 39 brown/yellow
GND -> GND black
*/
#define trigPin 38// Which digital (output) pin will we trigger the pulse on
#define echoPin 39 // Which digital (input) pin will we listen for echo on
long duration; // Duration used to calculate distance
float cm;

// LED INITIALIZATION
#define LED_PIN A6 //green led
#define LED_PIN A7 //red led

void openBox() { //instructs motors to open the box
digitalWrite(A6, HIGH); // green light flashes
delay (2000);
digitalWrite(A6, LOW);
Serial.println("open test");
if (box == false ) { //if the box is closed, open it
stepperRight.runToNewPosition(motorPosition);
stepperLeft.runToNewPosition(motorPosition);
Serial.println("opened box");
stepperRight.stop();
stepperLeft.stop();
delay(3000);
box = true; //set to open

}
}

void closeBox() { //instructs motors to close the box
digitalWrite(A7, HIGH); // red light flashes
delay (2000);
digitalWrite(A7, LOW);
Serial.println("closing test");
if (box == true ) { //if the box is open, close it
stepperRight.runToNewPosition(0);
stepperLeft.runToNewPosition(0);
Serial.println("closed box");
stepperRight.stop();
stepperLeft.stop();
delay(3000);
box = false; //set to close

}
}

...

if (duration < 100000)
{
cm = microSecondsToCentimeters(duration);
}
delay(100);
return cm;
}

...

//sonar
SPI.begin();
rfid.PCD_Init();

...

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() { //this is the main code that continuously loops
// button loop

buttonState = digitalRead(buttonPin);
prevButtonState = buttonState;

if ((buttonState == HIGH and prevButtonState == LOW) && (box == false)) { //if box is closed, open box
Serial.println("The button is pressed, opening box.");
openBox();
Serial.println("Box is now open");
}

else if ((buttonState == HIGH and prevButtonState == LOW) && (box == true)) { //if box is open, close box
Serial.println("The button is pressed, closing box.");
closeBox();
Serial.println("Box is now closed");
}

...

void rfidScan() { //scans RFID chip and either grants or denies access

...

if (strID.substring(1) == "41 17 6B 1B") { //this is the unique chip ID that grants access.
Serial.println("**Access granted**");
digitalWrite(A6, HIGH); // green light flashes
delay (3000);
digitalWrite(A6, LOW);
openBox();

...

else {
Serial.println("**Access denied**");
digitalWrite(A7, HIGH); //red light flashes
delay (3000);
digitalWrite(A7, LOW);
}
}

...

int motorPosition = 400;

/* RFID SENSOR layout
RST/Reset RST 47 brown
SPI SS SDA(SS) 53 yellow
SPI MISO MISO 50 orange
SPI MOSI MOSI 51 white
SPI SCK SCK 52 purple
3.3 V purple
GND black
*/
#include "SPI.h"
#include "MFRC522.h"

#define SS_PIN 53
#define RST_PIN 47

MFRC522 rfid(SS_PIN, RST_PIN);
bool box;

//SONAR INITIALIATION
/* SONAR SENSOR Layout
VCC -> 5V white
Trig -> port 38 blue/purple
Echo -> port 39 brown/yellow
GND -> GND black
*/
#define trigPin 38// Which digital (output) pin will we trigger the pulse on
#define echoPin 39 // Which digital (input) pin will we listen for echo on
long duration; // Duration used to calculate distance
float cm;

// LED INITIALIZATION
#define LED_PIN A6 //green led
#define LED_PIN A7 //red led

void openBox() { //instructs motors to open the box
digitalWrite(A6, HIGH); // green light flashes
delay (2000);
digitalWrite(A6, LOW);
Serial.println("open test");
if (box == false ) { //if the box is closed, open it
stepperRight.runToNewPosition(motorPosition);
stepperLeft.runToNewPosition(motorPosition);
Serial.println("opened box");
stepperRight.stop();
stepperLeft.stop();
delay(3000);
box = true; //set to open

}
}

void closeBox() { //instructs motors to close the box
digitalWrite(A7, HIGH); // red light flashes
delay (2000);
digitalWrite(A7, LOW);
Serial.println("closing test");
if (box == true ) { //if the box is open, close it
stepperRight.runToNewPosition(0);
stepperLeft.runToNewPosition(0);
Serial.println("closed box");
stepperRight.stop();
stepperLeft.stop();
delay(3000);
box = false; //set to close

}
}

...

if (duration < 100000)
{
cm = microSecondsToCentimeters(duration);
}
delay(100);
return cm;
}

...

//sonar
SPI.begin();
rfid.PCD_Init();

...

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() { //this is the main code that continuously loops
// button loop

buttonState = digitalRead(buttonPin);
prevButtonState = buttonState;

if ((buttonState == HIGH and prevButtonState == LOW) && (box == false)) { //if box is closed, open box
Serial.println("The button is pressed, opening box.");
openBox();
Serial.println("Box is now open");
}

else if ((buttonState == HIGH and prevButtonState == LOW) && (box == true)) { //if box is open, close box
Serial.println("The button is pressed, closing box.");
closeBox();
Serial.println("Box is now closed");
}

...

void rfidScan() { //scans RFID chip and either grants or denies access

...

if (strID.substring(1) == "41 17 6B 1B") { //this is the unique chip ID that grants access.
Serial.println("**Access granted**");
digitalWrite(A6, HIGH); // green light flashes
delay (3000);
digitalWrite(A6, LOW);
openBox();

Serial.println("pet is eating, box is open");
while (box == true) {
if (sonarDistance() >= 30){
Serial.println("closing box called");
closeBox();
}
}
//keep the box open while pet is present and eating
Serial.println("pet left, box closed");
}else {
Serial.println("**Access denied**");
digitalWrite(A7, HIGH); //red light flashes
delay (3000);
digitalWrite(A7, LOW);
}
}Electronics 

Electronics Components used:

  • Arduino mega
  • two 28byj-48 stepper motors
  • two UNL-2003 stepper motor drivers
  • RFID-RC522 Sensor 
  • two RFID tags
  • wires, electrical tape, solder, etc.
  • Ultrasonic Distance Sensor, HC-SR04
  • breadboard
  • 2 LED lights
  • push button



Software (coded on Arduino in C++)

View file
namefinal_code.ino.txt
height400