#include "SPI.h" #include "MFRC522.h" #include "pitches.h" #define SS_PIN 10 #define RST_PIN 9 int M2S=3; int M2D=7; int M1S = 5; int M1D = 6; int x=0; int y=0; int z=0; int n=0; int f=0; MFRC522 rfid(SS_PIN, RST_PIN); MFRC522::MIFARE_Key key; void setup() { Serial.begin(9600); SPI.begin(); rfid.PCD_Init(); pinMode(M1S, OUTPUT); pinMode(M1D, OUTPUT); pinMode(M2S, OUTPUT); pinMode(M2D, OUTPUT); pinMode(8,OUTPUT); } void loop() { if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return; // Serial.print(F("PICC type: ")); MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); // Serial.println(rfid.PICC_GetTypeName(piccType)); // Check is the PICC of Classic MIFARE type if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { Serial.println(F("Your tag is not of type MIFARE Classic.")); return; } String strID = ""; for (byte i = 0; i < 4; i++) { strID += (rfid.uid.uidByte[i] < 0x10 ? "0" : "") + String(rfid.uid.uidByte[i], HEX) + (i!=3 ? ":" : ""); } strID.toUpperCase(); Serial.print("Tap card key: "); Serial.println(strID); if (strID.indexOf("29:2D:A1:B2") >= 0 && n==0 && y==0 && z==0) { digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); // Moving forward digitalWrite(M1D,LOW); analogWrite(M1S,255); digitalWrite(M2D,LOW); analogWrite(M2S,255); x=1; } else if(strID.indexOf("29:2D:A1:B2") >= 0 && n==1) { //True int melody[] = { NOTE_D7 , NOTE_B6 , NOTE_D7, NOTE_G7 }; int noteDurations[] = { 4, 8, 4, 4 }; // iterate over the notes of the melody: for (int thisNote = 0; thisNote < 4; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/4, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(4); } y=0; z=0; n=0; f=0; } else if(strID.indexOf("29:2D:A1:B2") >= 0 && n==0 && f==1){ // False int melody[] = { NOTE_A2 , NOTE_B1 , NOTE_A2 }; int noteDurations[] = { 4, 4, 4 }; for (int thisNote = 0; thisNote < 3; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/3, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(3); } } if (strID.indexOf("00:7D:8D:7C") >= 0 && y==0 && z==0 && n==0) { digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); if(x==1){ // Moving forward digitalWrite(M1D,LOW); analogWrite(M1S,255); digitalWrite(M2D,LOW); analogWrite(M2S,255); } else if(x==2){ // Moving left digitalWrite(M1D,HIGH); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); // Moving forward digitalWrite(M1D,LOW); analogWrite(M1S,255); digitalWrite(M2D,LOW); analogWrite(M2S,255); } else if(x==3){ // Moving right digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,HIGH); analogWrite(M2S,0); delay(800); // STOP digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); // Moving forward digitalWrite(M1D,LOW); analogWrite(M1S,255); digitalWrite(M2D,LOW); analogWrite(M2S,255); } else if(x==4){ // STOP digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); } } else if (strID.indexOf("00:7D:8D:7C") >= 0 && f==1) { // False int melody[] = { NOTE_A2 , NOTE_B1 , NOTE_A2 }; int noteDurations[] = { 4, 4, 4 }; for (int thisNote = 0; thisNote < 3; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/3, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(3); } } if (strID.indexOf("93:47:B4:2E") >= 0 && z==0 && y==0 && n==0) { digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); // Moving left digitalWrite(M1D,HIGH); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); x=2; digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); // Moving forward digitalWrite(M1D,LOW); analogWrite(M1S,255); digitalWrite(M2D,LOW); analogWrite(M2S,255); //delay(7000); } else if(strID.indexOf("93:47:B4:2E") >= 0 && z==1) { //True int melody[] = { NOTE_D7 , NOTE_B6 , NOTE_D7, NOTE_G7 }; int noteDurations[] = { 4, 8, 4, 4 }; // iterate over the notes of the melody: for (int thisNote = 0; thisNote < 4; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/4, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(4); } n=1; z=0; } else if(strID.indexOf("93:47:B4:2E") >= 0 && z==0 && f==1){ // False int melody[] = { NOTE_A2 , NOTE_B1 , NOTE_A2 }; int noteDurations[] = { 4, 4, 4 }; for (int thisNote = 0; thisNote < 3; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/3, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(3); } } if (strID.indexOf("F2:79:B5:2E") >= 0 && y==0 && z==0 && n==0) { digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); // Moving right digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,HIGH); analogWrite(M2S,0); delay(800); x=3; // STOP digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); // Moving forward digitalWrite(M1D,LOW); analogWrite(M1S,255); digitalWrite(M2D,LOW); analogWrite(M2S,255); //delay(7000); } else if(strID.indexOf("F2:79:B5:2E") >= 0 && y==1) { //True int melody[] = { NOTE_D7 , NOTE_B6 , NOTE_D7, NOTE_G7 }; int noteDurations[] = { 4, 8, 4, 4 }; // iterate over the notes of the melody: for (int thisNote = 0; thisNote < 4; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/4, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(4); } z=1; y=0; } else if(strID.indexOf("F2:79:B5:2E") >= 0 && y==0 && f==1){ // False int melody[] = { NOTE_A2 , NOTE_B1 , NOTE_A2 }; int noteDurations[] = { 4, 4, 4 }; for (int thisNote = 0; thisNote < 3; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/3, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(3); } } if (strID.indexOf("F2:85:EE:2E") >= 0 && y==0 && z==0 && n==0) { // STOP digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); x=4; } else if (strID.indexOf("F2:85:EE:2E") >= 0 && f==1) { // False int melody[] = { NOTE_A2 , NOTE_B1 , NOTE_A2 }; int noteDurations[] = { 4, 4, 4 }; for (int thisNote = 0; thisNote < 3; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/3, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(3); } } if (strID.indexOf("E2:F6:07:2E") >= 0 && y==0 && z==0 && n==0) { y=1; f=1; unsigned char l,j;//define variable for(int k=0;k<3;k++) { for(l=0;l<80;l++)// output a frequency sound { digitalWrite(8,HIGH);// sound delay(1);//delay1ms digitalWrite(8,LOW);//not sound delay(1);//ms delay } for(l=0;l<100;l++)// output a frequency sound { digitalWrite(8,HIGH);// sound digitalWrite(8,LOW);//not sound delay(2);//2ms delay } } delay(300); // Moving right digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,HIGH); analogWrite(M2S,0); delay(800); // STOP digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(3000); // Moving left digitalWrite(M1D,HIGH); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(800); digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(3000); // Moving forward digitalWrite(M1D,LOW); analogWrite(M1S,255); digitalWrite(M2D,LOW); analogWrite(M2S,255); delay(2000); digitalWrite(M1D,LOW); analogWrite(M1S,0); digitalWrite(M2D,LOW); analogWrite(M2S,0); delay(300);} else if(strID.indexOf("E2:F6:07:2E") >= 0 && f==1){ // False int melody[] = { NOTE_A2 , NOTE_B1 , NOTE_A2 }; int noteDurations[] = { 4, 4, 4 }; for (int thisNote = 0; thisNote < 3; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/3, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(3); } } rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); }