Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

/*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>


//RFID SENSOR INITIALIZATION
#include "SPI.h"
#include "MFRC522.h"


/* STEPPER MOTOR Right
Pin1 - port 4
Pin2 - port 5
Pin3 - port 6
Pin4 - port 7
*/
/* STEPPER MOTOR Left
Pin1 - port 8
Pin2 - port 9
Pin3 - port 10
Pin4 - port 11
*/


// Define two motor objects

/*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>


//RFID SENSOR INITIALIZATION
#include "SPI.h"
#include "MFRC522.h"


/* STEPPER MOTOR Right
Pin1 - port 4
Pin2 - port 5
Pin3 - port 6
Pin4 - port 7
*/
/* STEPPER MOTOR Left
Pin1 - port 8
Pin2 - port 9
Pin3 - port 10
Pin4 - port 11
*/


// Define two motor objects
// The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48 stepper motor
AccelStepper stepperRight(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
AccelStepper stepperLeft(AccelStepper::FULL4WIRE, 8, 9, 10, 11);

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

}
}


float sonarDistance() { //collects data from sonar
// sonar function
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Call pulseIn function to wait for High pulse
// result will be time in microseconds until pulse is detected
duration = pulseIn(echoPin, HIGH);

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


void setup() { //this sets up everything in the code
//serial monitor
Serial.begin(9600);

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

//right motor
stepperRight.setMaxSpeed(100.0);
stepperRight.setAcceleration(100.0);
stepperRight.setSpeed(50);
stepperRight.setCurrentPosition(0);

//left motor
stepperLeft.setMaxSpeed(100.0);
stepperLeft.setAcceleration(100.0);
stepperLeft.setSpeed(50);
stepperLeft.setCurrentPosition(0);

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");
}


if (sonarDistance() <= 30) { //if pet is approaching, scan for RFID tag
delay(500);
if (sonarDistance() <= 30) {
//Serial.println("Scanning for chip...");
rfidScan();
}
}
}


float microSecondsToCentimeters(long microseconds) { //sonar relays microseconds and this converts it to distance
// Sound travels 29.386 microseconds per centimeter
// This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
return (microseconds / 29.387 / 2.0);
}

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


if ( ! rfid.PICC_IsNewCardPresent())
{
//Serial.println("no new card");
return;
}
if ( ! rfid.PICC_ReadCardSerial())
{
//Serial.println("can't read");
return;
}
//Serial.println("starting read");
String strID = "";
for (byte i = 0; i < rfid.uid.size; i++)
{
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.println(rfid.uid.uidByte[i], HEX);
strID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
strID.concat(String(rfid.uid.uidByte[i], HEX));
}
strID.toUpperCase();
Serial.println(strID);
delay(1000);

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);
}
}


// The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48 stepper motor
AccelStepper stepperRight(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
AccelStepper stepperLeft(AccelStepper::FULL4WIRE, 8, 9, 10, 11);

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

}
}


float sonarDistance() { //collects data from sonar
// sonar function
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Call pulseIn function to wait for High pulse
// result will be time in microseconds until pulse is detected
duration = pulseIn(echoPin, HIGH);

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


void setup() { //this sets up everything in the code
//serial monitor
Serial.begin(9600);

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

//right motor
stepperRight.setMaxSpeed(100.0);
stepperRight.setAcceleration(100.0);
stepperRight.setSpeed(50);
stepperRight.setCurrentPosition(0);

//left motor
stepperLeft.setMaxSpeed(100.0);
stepperLeft.setAcceleration(100.0);
stepperLeft.setSpeed(50);
stepperLeft.setCurrentPosition(0);

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");
}


if (sonarDistance() <= 30) { //if pet is approaching, scan for RFID tag
delay(500);
if (sonarDistance() <= 30) {
//Serial.println("Scanning for chip...");
rfidScan();
}
}
}


float microSecondsToCentimeters(long microseconds) { //sonar relays microseconds and this converts it to distance
// Sound travels 29.386 microseconds per centimeter
// This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
return (microseconds / 29.387 / 2.0);
}

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


if ( ! rfid.PICC_IsNewCardPresent())
{
//Serial.println("no new card");
return;
}
if ( ! rfid.PICC_ReadCardSerial())
{
//Serial.println("can't read");
return;
}
//Serial.println("starting read");
String strID = "";
for (byte i = 0; i < rfid.uid.size; i++)
{
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.println(rfid.uid.uidByte[i], HEX);
strID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
strID.concat(String(rfid.uid.uidByte[i], HEX));
}
strID.toUpperCase();
Serial.println(strID);
delay(1000);

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);
}
}

  • No labels