/*
RFID_Display
@author RaskAttack
Library:
SPI.h
MFRC522.h
LiquidCrystal.h
Servo.h
pitches.h
Most of this program is RFID. Anything that is not RFID will be marked.
*/
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h> //LCD
#include <Servo.h> //Servo
#include "pitches.h" //Buzzer
// Other hardware pins
const byte rstPin = 5; // Reset pin
const byte ssPin = 53; // Slave Select pin
// Instantiate MFRC522 object class
MFRC522 rfidReader(ssPin, rstPin);
// Other Global Constants
const long timeout = 30000; // Timeout in ms
/* ***********************************************************
* Global Variables *
* ********************************************************* */
char* myTags[100] = {};
int tagsCount = 0;
String tagID = "";
bool readRFID(long _timeout=timeout, bool useTimeout=false){
/* readRFID will continuously check the RFID reader for the presence of
* a tag from and will attempt to get tag IDs. Updates global value
* tagID via getTagID function.
* Parameters:
* _timeout - [optional] the length of time before functio gives up
* default value = global timout value
* useTimeout - [optional] boolean to enforce timout period or wait
* indefinately. Default value = false.
* Returns:
* true - successfully reads tag ID
* false - unsuccessful in reading the tag ID
*/
bool successRead = false;
unsigned long startTime = millis();
unsigned long currentTime = startTime;
// S'U'+S'T'
// T = (currentTime-startTime) > timeout
// T' = (currentTime-startTime) < timeout
while (((successRead==false)&&(useTimeout==false)) || ((successRead==false)&&((currentTime - startTime) < _timeout))) {
if (isTagPresent() == true){ successRead = getTagID(); }
currentTime = millis();
}
return successRead;
}
// include the library code:
// I2C library:
//#include <LiquidCrystal_I2C.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 7, d4 = 23, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define BLUE 10 //RGB LED
#define GREEN 11 //RGB LED
#define RED 12 //RGB LED
#define back 43 //LCD
int redValue; //RGB LED
int greenValue; //RGB LED
int blueValue; //RGB LED
// I2C: use the line below
// Parameters: LCD address, number of columns = 16, and the number of rows = 2
//LiquidCrystal lcd(0x3F, 16, 2);
Servo myservo;
void setup() {
// Normal begin statement for LCD requiring the number of columns and rows:
lcd.begin(16, 2);
pinMode(RED, OUTPUT); //RGB LED
pinMode(GREEN, OUTPUT); //RGB LED
pinMode(BLUE, OUTPUT); //RGB LED
digitalWrite(RED, HIGH); //RGB LED
digitalWrite(GREEN, LOW); //RGB LED
digitalWrite(BLUE, LOW); //RGB LED
pinMode(back, OUTPUT); //LCD
digitalWrite(back, HIGH); //LCD
myservo.attach(9); //Servo
myservo.write(50); //Servo
// Initiating
Serial.begin(9600); // Start the serial monitor
SPI.begin(); // Start SPI bus
rfidReader.PCD_Init(); // Start MFRC522 object
while (!Serial); // Do nothing if no serial port is opened
// Obviously this is an over simplified sketch
// Master tags would be save in flash storage and
// retrieved here. OR a special PIN entered to set
// Master Tag.
// But for the sake of simplicity, the sketch will
// obtain a new master tag when restarted.
// Prints the initial message
lcd.clear(); //LCD
lcd.setCursor(0, 0); //LCD
lcd.print("-NO MASTER TAG!-"); //LCD
lcd.setCursor(4, 1); //LCD
lcd.print("SCAN NOW"); //LCD
Serial.println(F("-NO MASTER TAG!-"));
Serial.println(F(" SCAN NOW"));
redValue = 255; //RGB LED
greenValue = 191; //RGB LED
blueValue = 0; //RGB LED
analogWrite(BLUE, blueValue); //RGB LED
analogWrite(RED, redValue); //RGB LED
analogWrite(GREEN, greenValue); //RGB LED
// readRFID will wait until a master card is scanned
if (readRFID() == true) {
myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array
lcd.clear(); //LCD
lcd.setCursor(0, 0); //LCD
lcd.print("Master tag set!"); //LCD
Serial.println(F("Master tag set!")); //LCD
redValue = 0; //RGB LED
greenValue = 0; //RGB LED
blueValue = 0; //RGB LED
analogWrite(BLUE, blueValue); //RGB LED
analogWrite(RED, redValue); //RGB LED
analogWrite(GREEN, greenValue); //RGB LED
tagsCount++;
}
printNormalModeMessage();
}
void loop() {
//looks for keypresses
//checks for any tags
if (isTagPresent()==true){
getTagID();
checkTagID();
} else {
delay(50);
//return;
}
}
/* ***********************************************************
* Functions *
* ********************************************************* */
bool isTagPresent(){
/* isTagPresent uses the MFRC522 methods to determine if
* a tag is present or the read card serial is enabled.
* Parameters: (none)
* Returns:
* true - if tag detected or read card serial is true
* false - no tag detected or no read card serial true
*/
bool returnValue = true;
// NOT a new PICC_IsNewCardPresent in RFID reader
//OR
// NOT a PICC_ReadCardSerial active in Serial
if ( !rfidReader.PICC_IsNewCardPresent() || !rfidReader.PICC_ReadCardSerial() ) {
returnValue = false;
}
return returnValue;
}
byte checkMyTags(String tagID){
/* checkMyTags function loops through the array of myTags
* Parameters:
* tagID - a string to look for
* Returns:
* tagIndex - index in the array of myTags
* default 0
*/
byte tagIndex = 0;
//Serial.println("checkMyTags Started");
// Zero is reserved for master tag
for (int i = 1; i < 100; i++) {
if (tagID == myTags[i]) { tagIndex = i; }
}
//Serial.println("checkMyTags ended");
return tagIndex;
}
void checkTagID(){
/* checkTagID check the tag ID for authorized tag ID values
* if Master tag found switch to program mode
* Parameters: (none)
* Returns: (none)
*/
// Checks for Master tag
if (tagID == myTags[0]) {
// Switch to program mode
lcd.clear(); //LCD
lcd.setCursor(1, 0); //LCD
lcd.print("Program mode:"); //LCD
lcd.setCursor(1, 1); //LCD
lcd.print("Add/Remove Tag"); //LCD
Serial.println(F("Program mode"));
Serial.println(F("Add/remove tag"));
digitalWrite(back, HIGH); //LCD
// Now with timeout
// readRFID will skip if timeout exceeded
if (readRFID(timeout,true)==true) {
//Check for authorized tag
byte tagIndex = checkMyTags(tagID);
if (tagIndex!=0){
//Remove existing tag
myTags[tagIndex] = '\0';
digitalWrite(back, HIGH); //LCD
lcd.clear(); //LCD
lcd.setCursor(0, 0); //LCD
lcd.print("Tag romoved!");//LCD
Serial.println(F("Tag Romoved!"));
tagsCount--;
} else {
//Not existing, add tag
myTags[tagsCount] = strdup(tagID.c_str());
lcd.clear(); //LCD
lcd.setCursor(0, 0); //LCD
lcd.print("Tag added!"); //LCD
Serial.println(F("Tag Added!"));
tagsCount++;
digitalWrite(back, HIGH); //LCD
}
} else {
lcd.clear(); //LCD
digitalWrite(back, LOW); //LCD
lcd.setCursor(0, 0); //LCD
lcd.print("Timeout!"); //LCD
Serial.println(F("Timeout"));
}
} else {
//Check for authorized tag
byte tagIndex = checkMyTags(tagID);
if (tagIndex!=0){
//Authorized tag
lcd.clear(); //LCD
digitalWrite(back, HIGH); //LCD
lcd.setCursor(0, 0); //LCD
lcd.print("Access Granted!"); //LCD
Serial.println(F(" Access Granted!"));
redValue = 0; //RGB LED
greenValue = 255; //RGB LED
blueValue = 0; //RGB LED
analogWrite(BLUE, blueValue); //RGB LED
analogWrite(RED, redValue); //RGB LED
analogWrite(GREEN, greenValue); //RGB LED
myservo.write(150); //Servo
tone(6, NOTE_C6); //Buzzer
} else {
//Not authorized tag
lcd.clear(); //LCD
digitalWrite(back, HIGH); //LCD
lcd.setCursor(0, 0); //LCD
lcd.print("Access Denied!"); //LCD
Serial.println(F(" Access Denied!"));
redValue = 255; //RGB LED
greenValue = 0; //RGB LED
blueValue = 0; //RGB LED
analogWrite(BLUE, blueValue); //RGB LED
analogWrite(GREEN, greenValue); //RGB LED
tone(6, NOTE_C5); //Buzzer
Serial.println(F(" New UID & Contents:"));
rfidReader.PICC_DumpToSerial(&(rfidReader.uid));
myservo.write(50); //Servo
}
}
printNormalModeMessage();
}
bool getTagID() {
/* getTagID retrieves the tag ID. Modifies global variable tagID
*
* Parameters: (none)
* Returns: true
*/
tagID = "";
Serial.print(F(" UID tag: "));
for (byte i = 0; i < rfidReader.uid.size; i++){
// The MIFARE PICCs that we use have 4 byte UID
Serial.print(rfidReader.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfidReader.uid.uidByte[i], HEX);
// Adds the bytes in a single String variable
tagID.concat(String(rfidReader.uid.uidByte[i] < 0x10 ? " 0" : " "));
tagID.concat(String(rfidReader.uid.uidByte[i], HEX));
}
Serial.println();
Serial.println();
tagID.toUpperCase();
rfidReader.PICC_HaltA(); // Stop reading
return true;
}
void printNormalModeMessage() {
/* printNormalModeMessage sends the standard greeting
* to the serial monitor and LCD display.
* Parameters: (none)
* Returns: (none)
*/
delay(1500);
redValue = 0; //RGB LED
greenValue = 0; //RGB LED
blueValue = 0; //RGB LED
analogWrite(BLUE, blueValue); //RGB LED
analogWrite(RED, redValue); //RGB LED
analogWrite(GREEN, greenValue); //RGB LED
noTone(6); //Buzzer
myservo.write(50);// Servo
lcd.clear(); //LCD
lcd.setCursor(1, 0); //LCD
lcd.print("Scan Your Tag!"); //LCD
lcd.setCursor(7, 1); //LCD
lcd.print(tagsCount - 1); //LCD
Serial.println();
Serial.println(F("-Access Control-"));
Serial.println(F(" Scan Your Tag!"));
}
I am trying to make the backlight turn off while keeping the code running. I would also like the backlight to be turned off within 30s. Make sure that it only runs if the printNormalModeMessage is running. Thanks!