My coding after one run then stop running, how can I let it keep looping, and how should I solve it

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

arduino check

/////////////////////////////////////////////////////////////////////
#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;
}

These seem to be a contradiction. Does your RFID reader remember the ID of a card and will not respond a second time?

hi thank for your replying
not really, since i not giving any memory function on it.
like the card 2, it can keep detect and looping after i scan the card
but it will stop looping after one run in scanning card 1

what does this do?

english seems to be not your native language. Your descriptions are very hard to understand
use google-translate. write a detailed description in your native language and let do google translate the translation.

It will be much more effective
best regards Stefan

this is for detect is there any card getting scan by the RFID module

but it exits from loop() if there is no "new" card detected.

so after detecting a card, what happens?

yes, if by my logical thinking, it will stop and waiting for new card detect. then it will do another set of action if getting scan a new card.
but my problem is, after it finished the pattern_1, it not able to receive any new signal already. the loop stop working

sorry about that, maybe later i will try to rewrite the question and make it clearer. thank you for suggestion

As gcjr pointed out, you should not leave loop.
You could use

while (true){
     if (!cardthere){
           break;
    }

This will wait until no card present and then continue)
Or reverse your logic...

if (cardthere) {
    readcard;
}

very old programmer-wisdom:
A program does ALWAYS what YOU have coded. If the code does something different than you want YOU don't understand your own code yet.

starting thinking: "but it should do that why does it not???!" does not change the codes behaviour. Add Serial debug-output at all interesting places to ANALYSE what the code is REALLY doing.

upload this code-version to see more details of what your code does

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *

// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


/////////////////////////////////////////////////////////////////////
#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);
  Serial.println("Setup-Start");
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522
  pinMode(8, OUTPUT);
  Serial.println("Setup done");
}

/////////////////////////////////////////////////////////////////////
void loop() {
  String stu_name, stu_id ;
  String temp_val;
  if ( ! rfid.PICC_IsNewCardPresent()) {
    dbgi("0: no NEW card present return", tag, 1000);
    return;
  }

  if (rfid.PICC_ReadCardSerial()) {
    for (byte i = 0; i < 4; i++) {
      tag += rfid.uid.uidByte[i];
    }

    //Serial.println(tag);
    dbg("1:", 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!");
      dbg("Access Denied!",tag);
      digitalWrite(8, HIGH);
      delay(2000);
      digitalWrite(8, LOW);
    }
    
    //Serial.println("Next Work");
    tag = "";
    dbg("Next Work tag empty",tag);
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();

  }
  Serial.println("redo work");
  dbgi("Z end of loop",tag,1000);
}



/////////////////////////////////////////////////////////////////////////////// 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");
}


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");
}

////////////////////////////////////////////////////////////////////////// Moving Define
void stopp() {
  analogWrite (10, LOW);
  digitalWrite (5, LOW);

  analogWrite (6, LOW);
  digitalWrite (9, LOW);
  Serial.println("stop done");
}

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);
}


void leftt() {

  analogWrite (10, LOW);
  analogWrite (5, 75);

  analogWrite (6, 85);
  analogWrite (9, LOW);
}

void rightt() {

  analogWrite (10, 85);
  analogWrite (5, LOW);

  analogWrite (6, LOW);
  analogWrite (9, 75);
}

best regards Stefan

Finally found out where the problem is
The problem was that I was assigned pin 10 as my RFID pin

#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
String tag;

But at below, im still using my old motor assign coding pin

void leftt() {

  analogWrite (10, LOW);
  analogWrite (5, 75);

  analogWrite (6, 85);
  analogWrite (9, LOW);
}

So when I scan my card 1, it stop the RFID from running, and this card 1 scanning action causes the system to error or crash.
After I reassigned all the pins everything was fine

Thanks a lot here, try to teach me and try to solve my problem
Thank you all! ! ! !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.