Vous êtes sur la page 1sur 14

#include <SPI.

h>

#include <MFRC522.h>

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

#include <RTClib.h>

RTC_DS1307 RTC;

/* for normal hardware wire use above */

LiquidCrystal_I2C lcd(0x27,16,2); // 0x27 is the I2C bus address for an unmodified backpack

#define RSTPIN 9

#define SSPIN 10

MFRC522 rc(SSPIN, RSTPIN); // Create MFRC522 instance.

int readsuccess;

int Data;

byte defcard[][4]={{0xFC,0x57,0x85,0xB4},{0x3C,0xAE,0x26,0xCE},{0x36,0xCB,0x24,0xD9}}; //for multiple


cards

int N=3; //change this to the number of cards/tags you will use

byte readcard[4]; //stores the UID of current tag which is read

const int LAMP_PB = 4;

const int AC_PB = 3;

const int PRJTOR_PB = 2;


int BUZZER = 5;

int AC_LED = 6;

int LAMP_LED = 8;

int PRJTOR_LED = 7;

int AC_OUT = A1;

int PRJTOR_OUT = A2;

int LAMP_OUT = A3;

int LAMP_ADJ = A0;

int TIMER = 10;

int TIMER1;

void setup() {

Serial.begin(57600);

Wire.begin();

RTC.begin();

Serial.begin(9600); // Initialize serial communications with the PC

SPI.begin(); // Init SPI bus

rc.PCD_Init(); //initialize the receiver

rc.PCD_DumpVersionToSerial(); //show details of card reader module

Serial.println(F("the authorised cards are")); //display authorised cards just to demonstrate you may
comment this section out

for(int i=0;i<N;i++){

Serial.print(i+1);
Serial.print(" ");

for(int j=0;j<4;j++){

Serial.print(defcard[i][j],HEX);

Serial.println("");

Serial.println("");

Serial.println(F("Scan Access Card to see Details"));

lcd.init(); // initialize the lcd

lcd.backlight();

lcd.home();

lcd.setCursor(0, 0);

lcd.print("SMART CLASS- M/C");

lcd.setCursor(0, 1);

lcd.print("DITO-TELKOM@2018");

delay(3000);

lcd.setBacklight(LOW); // Backlight off

delay(1000);

lcd.setBacklight(HIGH); // Backlight on

delay(1000);

lcd.clear();

pinMode(LAMP_PB, INPUT);

pinMode(AC_PB, INPUT);

pinMode(PRJTOR_PB, INPUT);

pinMode(LAMP_LED, OUTPUT);
pinMode(AC_LED, OUTPUT);

pinMode(PRJTOR_LED, OUTPUT);

pinMode(BUZZER, OUTPUT);

pinMode(LAMP_OUT, OUTPUT);

pinMode(AC_OUT, OUTPUT);

pinMode(PRJTOR_OUT, OUTPUT);

pinMode(LAMP_ADJ, OUTPUT);

ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine

Data++;

void loop() {

START:

Data = 0;

// initialize timer1

noInterrupts(); // disable all interrupts

TCCR1A = 0;

TCCR1B = 0;

TCNT1 = 0;

OCR1A = 31250; // compare match register 16MHz/256/2Hz


TCCR1B |= (1 << WGM12); // CTC mode

TCCR1B |= (1 << CS12); // 256 prescaler

TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt

interrupts(); // enable all interrupts

lcd.home();

lcd.setCursor(0, 0);

lcd.print("SMART CLASS- M/C");

lcd.setCursor(0,1);

lcd.print("READING.......");

delay(100);

readsuccess = getid();

if(readsuccess){

int match=0;

//this is the part where compare the current tag with pre defined tags

for(int i=0;i<N;i++){

Serial.print("Testing Against Authorised card no: ");

Serial.println(i+1);

if(!memcmp(readcard,defcard[i],4)){

match++; }

if(match)

{Serial.println("CARD AUTHORISED");

digitalWrite(BUZZER,HIGH);

delay(2000);
digitalWrite(BUZZER,LOW);

delay(500);

goto AUTORISED;

else {

Serial.println("CARD NOT Authorised");

digitalWrite(BUZZER,HIGH);

delay(500);

digitalWrite(BUZZER,LOW);

delay(500);

digitalWrite(BUZZER,HIGH);

delay(500);

digitalWrite(BUZZER,LOW);

delay(500);

goto START;

return;

AUTORISED:

lcd.clear();

lcd.home();

lcd.setCursor(0, 0);

lcd.print("RFID DETECTED");

lcd.setCursor(0, 1);

lcd.print("CARD AUTORISED");

digitalWrite(BUZZER, HIGH);
delay(2000);

digitalWrite(BUZZER, LOW);

delay(2000);

goto MENU ;

return;

MENU:

lcd.clear();

lcd.home();

lcd.setCursor(0, 0);

lcd.print("-- WELCOME TO --");

lcd.setCursor(0, 1);

lcd.print("SMART CLASS- M/C");

digitalWrite(BUZZER, HIGH);

delay(1000);

digitalWrite(BUZZER, LOW);

delay(1000);

lcd.clear();

lcd.home();

lcd.setCursor(0, 0);

lcd.print("SMART CLASS- M/C");

lcd.setCursor(0, 1);

lcd.print("STARTING........");

delay(2000);

lcd.clear();

goto SYSTEM ;

return;

SYSTEM:
Serial.begin(9600); // Initialize serial communications with the PC

DateTime now = RTC.now();

lcd.home();

lcd.setCursor(0, 0);

lcd.print("SCR");

lcd.setCursor(8,0);

lcd.print(now.hour(), DEC);

lcd.print(':');

lcd.print(now.minute(), DEC);

lcd.print(':');

TIMER1 = (now.minute(), DEC);

lcd.print(now.second(), DEC);

lcd.print(' ');

lcd.setCursor(0, 1);

lcd.print("LOAD:");

delay(100);

if(Data == 300)

goto BACK;

int button1State, button2State, button3State;

button1State = 1;

button1State = digitalRead(LAMP_PB);

delay(100);

if (button1State == LOW)

{
digitalWrite(LAMP_LED, HIGH); // turn the LAMP on

digitalWrite(LAMP_OUT, HIGH); // Data D0 to ThingSpeak.com

digitalWrite(LAMP_ADJ, HIGH);

digitalWrite(BUZZER, HIGH); // turn the Buzzer on

delay(250);

digitalWrite(BUZZER, LOW);

lcd.setCursor(5, 1);

lcd.print("(1)");

button2State = digitalRead(AC_PB);

delay(100);

if (button2State == LOW)

digitalWrite(AC_LED, HIGH); // turn the AC on

digitalWrite(AC_OUT, HIGH); // Data D1 to ThingSpeak.com

digitalWrite(BUZZER, HIGH); // turn the Buzzer on

delay(250);

digitalWrite(BUZZER, LOW);

lcd.setCursor(9, 1);

lcd.print("(1)");

button3State = digitalRead(PRJTOR_PB);

delay(100);

if (button3State == LOW)

digitalWrite(PRJTOR_LED, HIGH); // turn the PROJECTOR on

digitalWrite(PRJTOR_OUT, HIGH); // Data D2 to ThingSpeak.com


digitalWrite(BUZZER, HIGH); // turn the Buzzer on

delay(250);

digitalWrite(BUZZER, LOW);

lcd.setCursor(13, 1);

lcd.print("(1)");

button1State = 1;

button2State = 1;

button3State = 1;

delay(500);

button1State = digitalRead(LAMP_PB);

delay(100);

if (button1State == LOW)

digitalWrite(LAMP_LED, LOW); // turn the LAMP OFF

digitalWrite(LAMP_OUT, LOW); // Data D0 to ThingSpeak.com

digitalWrite(LAMP_ADJ, LOW);

digitalWrite(BUZZER, HIGH); // turn the Buzzer on

delay(250);

digitalWrite(BUZZER, LOW);

lcd.setCursor(5, 1);

lcd.print("(0)");

button2State = digitalRead(AC_PB);

delay(100);

if (button2State == LOW)
{

digitalWrite(AC_LED, LOW); // turn the AC OFF

digitalWrite(AC_OUT, LOW); // Data D1 to ThingSpeak.com

digitalWrite(BUZZER, HIGH); // turn the Buzzer on

delay(250);

digitalWrite(BUZZER, LOW);

lcd.setCursor(9, 1);

lcd.print("(0)");

button3State = digitalRead(PRJTOR_PB);

delay(100);

if (button3State == LOW)

digitalWrite(PRJTOR_LED, LOW); // turn the PROJECTOR OFF

digitalWrite(PRJTOR_OUT, LOW); //Data D2 to ThingSpeak.com

digitalWrite(BUZZER, HIGH); // turn the Buzzer on

delay(250);

digitalWrite(BUZZER, LOW);

lcd.setCursor(13, 1);

lcd.print("(0)");

readsuccess = getid();

if(readsuccess){

int match=0;
//this is the part where compare the current tag with pre defined tags

for(int i=0;i<N;i++){

Serial.print("Testing Against Authorised card no: ");

Serial.println(i+1);

if(!memcmp(readcard,defcard[i],4)){

match++; }

if(match)

{Serial.println("CARD AUTHORISED");

digitalWrite(BUZZER,HIGH);

delay(2000);

digitalWrite(BUZZER,LOW);

delay(500);

goto BACK;

else {

Serial.println("CARD NOT Authorised");

digitalWrite(BUZZER,HIGH);

delay(500);

digitalWrite(BUZZER,LOW);

delay(500);

digitalWrite(BUZZER,HIGH);

delay(500);

digitalWrite(BUZZER,LOW);

delay(500);

goto SYSTEM;
return;

BACK:

lcd.clear();

lcd.home();

lcd.setCursor(0, 0);

lcd.print("BACK TO SYSTEM");

lcd.setCursor(0, 1);

lcd.print("RE-STARTING....");

digitalWrite(BUZZER, HIGH);

delay(1000);

digitalWrite(BUZZER, LOW);

digitalWrite(LAMP_LED, LOW);

digitalWrite(AC_LED, LOW);

digitalWrite(PRJTOR_LED, LOW);

digitalWrite(LAMP_OUT, LOW);

digitalWrite(AC_OUT, LOW);

digitalWrite(PRJTOR_OUT, LOW);

digitalWrite(LAMP_ADJ, LOW);

delay(3000);

goto START;

return;

//function to get the UID of the card

int getid(){

if(!rc.PICC_IsNewCardPresent()){

return 0;
}

if(!rc.PICC_ReadCardSerial()){

return 0;

Serial.println("THE UID OF THE SCANNED CARD IS:");

for(int i=0;i<4;i++){

readcard[i]=rc.uid.uidByte[i]; //storing the UID of the tag in readcard

Serial.print(readcard[i],HEX);

Serial.println("");

Serial.println("Now Comparing with Authorised cards");

rc.PICC_HaltA();

return 1;