Back to Parent

#include <RFID.h>

/*
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS/SDA: Pin 10
* RST: Pin 9
*/

#include <SPI.h>
#include <RFID.h>
#include <Servo.h>

#define SS_PIN 10
#define RST_PIN 9

Servo myservo;  // create servo object to control a servo

RFID rfid(SS_PIN,RST_PIN);


int led = 7;
int power = 8; 
int serNum[5];
int cards[][5] = {
  {112,93,73,122,30},
  {176,209,62,156,195},
  {160,6,236,155,209},
  {224,253,92,156,221},
  {32,71,232,155,20},
  {16,18,101,156,251},
  {240,219,249,122,168}
  
};

int servoAngle = 0;

int angle1 = 30;
int angle2 = 60;
int angle3 = 90;
int angle4 = 120;
int angle5 = 150;
int angle6 = 180;

bool access = false;

void setup(){

    myservo.attach(3);  // attaches the servo on pin 9 to the servo object

    Serial.begin(9600);
    SPI.begin();
    rfid.init();

    pinMode(led, OUTPUT);

    digitalWrite(led, LOW);
   
}

void check(){
        if(rfid.readCardSerial()){
            Serial.print(rfid.serNum[0]);
            Serial.print(" ");
            Serial.print(rfid.serNum[1]);
            Serial.print(" ");
            Serial.print(rfid.serNum[2]);
            Serial.print(" ");
            Serial.print(rfid.serNum[3]);
            Serial.print(" ");
            Serial.print(rfid.serNum[4]);
            Serial.println("");
            
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] == cards[0][i]) {
                     servoAngle = angle1;
                  } else if (rfid.serNum[i] == cards[1][i]){
                     servoAngle = angle2;
                  } else if (rfid.serNum[i] == cards[2][i]){
                     servoAngle = angle3;
                  } else if (rfid.serNum[i] == cards[3][i]){
                     servoAngle = angle4;
                  } else if (rfid.serNum[i] == cards[4][i]){
                     servoAngle = angle5;
                  } else if (rfid.serNum[i] == cards[5][i]){
                     servoAngle = angle6;
                  } else if (rfid.serNum[i] == cards[6][i]){
                     servoAngle = 0;
                  } else {
                     break;
                  }
           
              }
              
    rfid.halt();
  }
}
void loop(){
    myservo.write(servoAngle);
    
    if(rfid.isCard()){
      check();
      delay(1000);
    }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0