I'm doing a school project, and i need to do some action once i received a specific signal, and in first I'm choosing using RFID as my signal, and to giving action to my motor.
I'm facing a problem which is, after one time scan of my RFID, the code will stop run and not detect any new card already, but if the second card without any action it can still continue run, just the after action then stop looping the code. The result will be in figure that upload
How can i solve it? Please and thankful.
my code will shown in below
/////////////////////////////////////////////////////////////////////
#include <SPI.h>
#include <MFRC522.h>
/////////////////////////////////////////////////////////////////////
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
String tag;
/////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
pinMode(8, OUTPUT);
}
/////////////////////////////////////////////////////////////////////
void loop() {
String stu_name, stu_id ;
String temp_val;
if ( ! rfid.PICC_IsNewCardPresent())
return;
if (rfid.PICC_ReadCardSerial()) {
for (byte i = 0; i < 4; i++) {
tag += rfid.uid.uidByte[i];
}
Serial.println(tag);
if (tag == "11513317322") {
// action testing
Serial.println("Access Granted!");
Serial.println("in pattern 1");
pattern_1();
delay(500);
Serial.println("done pattern 1");
}
else if (tag == "21113024422") {\
// do nothing testing
Serial.println("Access Granted!");
}
else {
Serial.println("Access Denied!");
digitalWrite(8, HIGH);
delay(2000);
digitalWrite(8, LOW);
}
Serial.println("Next Work");
tag = "";
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
Serial.println("redo work");
}
/////////////////////////////////////////////////////////////////////////////// Pattern Define
void pattern_1(){
Serial.println("in pattern 1");
straightt();
delay(1000);
stopp();
delay(500);
leftt();
delay(440);
stopp();
delay(500);
rightt();
delay(870);
rightt();
delay(440);
stopp();
delay(500);
straightt();
delay(1000);
leftt();
delay(870);
stopp();
delay(300);
Serial.println("out pattern 1");
return;
}
void pattern_2(){
Serial.println("in pattern 2");
leftt();
delay(440);
stopp();
delay(500);
straightt();
delay(250);
stopp();
delay(500);
straightt();
delay(250);
stopp();
delay(500);
rightt();
delay(870);
stopp();
delay(500);
straightt();
delay(250);
stopp();
delay(500);
straightt();
delay(250);
stopp();
delay(500);
leftt();
delay(440);
stopp();
Serial.println("out pattern 2");
return;
}
////////////////////////////////////////////////////////////////////////// Moving Define
void stopp(){
analogWrite (10,LOW);
digitalWrite (5,LOW);
analogWrite (6,LOW);
digitalWrite (9,LOW);
return;
}
void straightt(){
analogWrite (10,100);
digitalWrite (5,LOW);
analogWrite (6,112);
digitalWrite (9,LOW);
delay (200);
analogWrite (10,100);
digitalWrite (5,LOW);
analogWrite (6,128);
digitalWrite (9,LOW);
delay (200);
analogWrite (10,100);
digitalWrite (5,LOW);
analogWrite (6,113);
digitalWrite (9,LOW);
return;
}
void leftt(){
analogWrite (10,LOW);
analogWrite (5,75);
analogWrite (6,85);
analogWrite (9,LOW);
return;
}
void rightt(){
analogWrite (10,85);
analogWrite (5,LOW);
analogWrite (6,LOW);
analogWrite (9,75);
return;
}