The following code is an implementation of the producer consumer pro.pdf
Arduino
1. ////Arduino libraries
#include <avr/sleep.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <TimerObject.h>
//Camera and SD card
#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>
SoftwareSerial cameraconnection = SoftwareSerial(69, 3);
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);
#define chipSelect 53
#define door
//NFC
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
#define IRQ (22)
#define RESET (24) // Not connected by default on the NFC Shield
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
unsigned long tag = 2763526164;
int motor[] = {4, 5};
int ledPin = 13;
int pirState = 0;
int pirPin = 21;
int reedState = 0;
int reedPin = 6;
int doorState;
int counter =0;
int timer = millis();
int fpPin = 12;
int fpsave = LOW;
int nfccounter = 0;
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes
depending on ISO14443A card type)
TimerObject *timer1 = new TimerObject(20000);
TimerObject *timer2 = new TimerObject(5000);
int getFingerprintIDez();
SoftwareSerial mySerial(10,11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
//attachInterrupt(5,wakeUpNow, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
/////////////NFC
Serial.begin(115200);
2. nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF,
HEX);
nfc.setPassiveActivationRetries(0x01);
// configure board to read RFID tags
nfc.SAMConfig();
//Serial.begin(9600);
pinMode(53, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(reedPin, INPUT);
finger.begin(57600);
timer1->setOnTimer(MCUsleep);
timer2->setOnTimer(Picture);
int i;
for(i = 0; i < 2; i++){
pinMode(motor[i], OUTPUT);
}
}
void loop()
{
nfc.begin();
Serial.print("ddddddddddddd");
doorState = LOW;
///Open state function
Open();
///Closed State function
Close();
timer1->Start();
timer2->Start();
///Checks the magenetic reed switch to see if the door is open or closed
reedState=digitalRead(reedPin);
while(reedState == HIGH && doorState == LOW)
{
digitalWrite(13, HIGH);
if(counter == 1)
{
3. Serial.print("Stop");
///takes a picture after 5 seconds
timer2->setOnTimer(Picture);
//prevents the counter from reseting
timer2->Stop();
}
///checks magnetic reed switch to see if some one has opened the door
reedState=digitalRead(reedPin);
///Finger print confirmation to unlock the door
getFingerprintIDez();
///NFC confermation to unlock the door
NFCcon();
nfccounter=0;
Serial.println("waiting");
Serial.println(counter);
timer1->Update();
timer2->Update();
}
timer1->Stop();
Open();
Close();
}
///Open door function to prevent the microcontroller from falling asleep
void Open()
{
reedState=digitalRead(reedPin);
while(reedState == LOW)
{
doorState = HIGH;
reedState=digitalRead(reedPin);
delay(500);
Serial.println("Open");
}
}
///Locks the door when the door is closed from an open state
void Close()
{
reedState=digitalRead(reedPin);
if(reedState==HIGH && doorState == HIGH)
{
Lock();
4. reedState=digitalRead(reedPin);
Serial.println("Close");
// delay(1000);
MCUsleep();
}
}
///NFC confirmation function
void NFCcon()
{
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
if (nfccounter==0)
{
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLeng
th);
uint32_t cardidentifier = 0;
if (success) {
// Display some basic information about the card
//Serial.println("Found an ISO14443A card");
//Serial.println("Card detected #");
// turn the four byte UID of a mifare classic into a single variable #
cardidentifier = uid[3];
cardidentifier <<= 8; cardidentifier |= uid[2];
cardidentifier <<= 8; cardidentifier |= uid[1];
cardidentifier <<= 8; cardidentifier |= uid[0];
//Serial.println(cardidentifier);
if (cardidentifier == tag) {
Unlock();
Serial.println("It's a match");
timer2->Stop();
}
else {
Serial.println("It's not a match");
}
Serial.println("");
}
nfccounter = 1;
}
}
/// Tell the camera to take a picture
void Picture()
{
Serial.println("VC0706 Camera snapshot test");
// see if the card is present and can be initialized:
5. if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
// return;
}
// Try to locate the camera
if (cam.begin()) {
Serial.println("Camera Found:");
} else {
Serial.println("No camera found?");
// return;
}
cam.setImageSize(VC0706_320x240); // biggest
Serial.println("Snap in 1 secs...");
// delay(1000);
if (! cam.takePicture())
Serial.println("Failed to snap!");
else
Serial.println("Picture taken!");
// Create an image with the name IMAGExx.JPG
char filename[13];
strcpy(filename, "IMAGE00.JPG");
for (int i = 0; i < 100; i++) {
filename[5] = '0' + i/10;
filename[6] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after
write
if (! SD.exists(filename)) {
break;
}
}
// Open the file for writing
File imgFile = SD.open(filename, FILE_WRITE);
// Get the size of the image (frame) taken
uint16_t jpglen = cam.frameLength();
Serial.print("Storing ");
Serial.print(jpglen, DEC);
Serial.print(" byte image.");
int32_t time = millis();
pinMode(8, OUTPUT);
// Read all the data up to # bytes!
byte wCount = 0; // For counting # of writes
while (jpglen > 0) {
// read 32 bytes at a time;
6. uint8_t *buffer;
uint8_t bytesToRead = min(64, jpglen); // change 32 to 64 for a speedup
but may not work with all setups!
buffer = cam.readPicture(bytesToRead);
imgFile.write(buffer, bytesToRead);
if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't
appear locked up
Serial.print('.');
wCount = 0;
}
//Serial.print("Read "); Serial.print(bytesToRead, DEC);
Serial.println(" bytes");
jpglen -= bytesToRead;
}
imgFile.close();
time = millis() - time;
counter = 1;
Serial.println("done!");
Serial.print(time); Serial.println(" ms elapsed");
//return;
}
//FingerPrint confirmation function
int getFingerprintIDez() {
finger.begin(57600);
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
//Picture();
counter = 1;
Unlock();
Serial.println("FIngerprint");
return finger.fingerID;
}
/// tells the door the lock by setting the motor driver polarity
void Lock()
{
Serial.print("Lock");
digitalWrite(motor[0], LOW);
digitalWrite(motor[1], HIGH);
//digitalWrite(13, HIGH);
7. delay(1500);
digitalWrite(motor[0], LOW);
digitalWrite(motor[1], LOW);
//digitalWrite(13, LOW);
}
///Unlocks the door by setting the motor driver polarity
void Unlock()
{
timer2->Stop();
Serial.print("Unlock");
digitalWrite(motor[0], HIGH);
digitalWrite(motor[1], LOW);
//digitalWrite(13, HIGH);
delay(1500);
digitalWrite(motor[0], LOW);
digitalWrite(motor[1], LOW);
//digitalWrite(13, LOW);
}
/// function to Wake the microcontroller up from sleep
void wakeUpNow()
{
timer1->Start();
timer2->Start();
}
///function for microcontroller to go to sleep
void MCUsleep()
{
Serial.println("Sleep");
delay(300);
counter = 0;
Serial.println(counter);
digitalWrite(13, LOW);
;
delay(500);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(5,wakeUpNow, LOW);
sleep_mode();
detachInterrupt(5);
}