Hey guys, I have a question for everyone. I would like to make my program more efficient in terms of saving battery power. I currently have an adafruit PN532 NFC reader connected to a SparkFun Fio V3 that powers a servo to lock and unlock my front door when the card is detected. It is being powered through the micro USB port by an external 16,000 mAh battery pack (the same ones commonly used for charging phones on the go).
However, it seems that after 3 days of having the unit powered the battery is already halfway gone. Can you all look at my code to see if there are any inefficiencies within it that would cause the battery to be drained so fast. Is the PN532 is constantly being polled and asking (is there a card.... is the a card now??) and draining my battery?
#include <Wire.h>
#include<Servo.h>
#include<SPI.h>
#include <Adafruit_PN532.h>
#include "Servo.h"
#include "Wire.h"
#include "SPI.h"
#include "Adafruit_PN532.h"
#define PN532_IRQ (7)
#define PN532_RESET (3)
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
Servo myservo; // create servo object to control a servo
int servoPin = 9; // servo pin on Arduino
int lockState = 2; // IMPORTANT: if state = 1; locked, state = 2; unlocked
int doorLock;
void setup() {
Serial.begin(115200);
Serial.println("Hello!");
// find Adafruit RFID/NFC shield
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN532 board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.SAMConfig();
}
//***********LOOP************
unsigned digit = 0;
void loop(void) {
uint8_t 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)
// 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)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
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);
Serial.println("Add this tag # to code");
// repeat this for loop as many times as you have RFID cards
switch (cardidentifier) {
case 1303311769: // these are the tags numbers
lockUnlock();
myservo.detach();
delay(2000); // do not allow the card to read for x second(s)
break;
case 1694073287:
lockUnlock();
myservo.detach();
delay(2000); // do not allow the card to read for x second(s)
break;
case 1694526935:
lockUnlock();
myservo.detach();
delay(2000); // do not allow the card to read for x second(s)
break;
case 1304635385:
lockUnlock();
myservo.detach();
delay(2000); // do not allow the card to read for x second(s)
break;
case 1245611012:
lockUnlock();
myservo.detach();
delay(2000); // do not allow the card to read for x second(s)
break;
case 3723402814:
lockUnlock();
myservo.detach();
delay(2000); // do not allow the card to read for x second(s)
break;
}
}
}
void lockUnlock() { // locks or unlocks door
myservo.attach(servoPin);
if (lockState == 1) {
myservo.write(180); // unlock the door
delay(500); // move servo for X second(s)
lockState = 2;
}
else if (lockState == 2) {
myservo.write(0); // lock the door
delay(500); // move servo for X second(s)
lockState = 1;
}
myservo.detach();
}