I would like to restart a program once it is finished

Hi everyone, I am doing the project for my exam which consists of a safety lock with rfid, everything goes as it should go apart when the solenoid lock is unlocked, which once opened closes and from there on the module rfid no longer works.
this is the code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 
#define LED_R 4
#define RELAY 3 
#define BUZZER 2
#define ACCESS_DELAY 8000
#define DENIED_DELAY 1000

MFRC522 mfrc522(SS_PIN, RST_PIN); 
LiquidCrystal_I2C lcd(0x27,20,4);

void setup() 
{
  Serial.begin(9600);  
  SPI.begin();          
  mfrc522.PCD_Init();   
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  digitalWrite(RELAY, LOW);
  Serial.println("Put your card to the reader...");
  Serial.println();
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Prego appoggi pure  la sua card");

}

void loop() 
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  lcd.clear();
  Serial.print("Numero : ");
  lcd.setCursor(0, 0);
  lcd.print("Numero:");
  lcd.setCursor(0, 1);
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Permesso? ");
  lcd.clear();
  content.toUpperCase();
  if (content.substring(1) == "B3 5D 89 91") 
  {
    Serial.println("codice giusto entrare");
    Serial.println();
    lcd.setCursor(0, 0);
    lcd.print("accesso autorizzato,puo' entrare");
    delay(500);
    digitalWrite(RELAY, HIGH);
    digitalWrite(LED_G, HIGH);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, LOW);
    digitalWrite(LED_G, LOW);
  }
 
 else   {
    Serial.println("codice errato riprovi");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("accesso negato,     riprovi");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}
  void reset_state()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Prego appoggi pure  la sua card");
    digitalWrite( LED_G , LOW);
    digitalWrite( LED_R , LOW);
}

What should I do in your opinion?

Your topic was MOVED to its current forum category as it is more suitable than the original

you should call mfrc522.PICC_HaltA(); once the card is read.

check how it's done in this example

Those

return;

inside loop() aren’t doing you any favors.

run this version of the code and analyse the serial debug-output

// 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 <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5
#define LED_R 4
#define RELAY 3
#define BUZZER 2
#define ACCESS_DELAY 8000
#define DENIED_DELAY 1000

MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()
{
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  digitalWrite(RELAY, LOW);
  Serial.println("Put your card to the reader...");
  Serial.println();
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Prego appoggi pure  la sua card");

}

void loop()
{
  dbgi("top of loop", mfrc522.PICC_IsNewCardPresent(), 1000);
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    dbgi("return cause ! mfrc522.PICC_IsNewCardPresent", 0, 1000);
    return;
  }
  dbgi("2:", mfrc522.PICC_ReadCardSerial(), 1000);
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    dbgi("return cause ! mfrc522.PICC_ReadCardSerial()", 0, 1000);
    return;
  }

  dbgi("went below returns", 0, 1000);

  lcd.clear();
  Serial.print("Numero : ");
  lcd.setCursor(0, 0);
  lcd.print("Numero:");
  lcd.setCursor(0, 1);
  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Permesso? ");
  lcd.clear();
  content.toUpperCase();
  if (content.substring(1) == "B3 5D 89 91")
  {
    Serial.println("codice giusto entrare");
    Serial.println();
    lcd.setCursor(0, 0);
    lcd.print("accesso autorizzato,puo' entrare");
    delay(500);
    digitalWrite(RELAY, HIGH);
    digitalWrite(LED_G, HIGH);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, LOW);
    digitalWrite(LED_G, LOW);
  }

  else   {
    Serial.println("codice errato riprovi");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("accesso negato,     riprovi");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

void reset_state()
{
  dbgi("entering reset_state()",0,1000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Prego appoggi pure  la sua card");
  digitalWrite( LED_G , LOW);
  digitalWrite( LED_R , LOW);
  dbgi("leaving reset_state()",0,1000);
}

best regards Stefan

I just did what he proposed to me and this is what is written on the serial port

top of loop" mfrc522.PICC_IsNewCardPresent()=0
"2:" mfrc522.PICC_ReadCardSerial()=0
"return cause ! mfrc522.PICC_ReadCardSerial()" 0=0
"went below returns" 0=0
Numero :  93 BA 81 A7
Permesso? codice errato riprovi
"top of loop" mfrc522.PICC_IsNewCardPresent()=0
"return cause ! mfrc522.PICC_IsNewCardPresent" 0=0
"top of loop" mfrc522.PICC_IsNewCardPresent()=0
"return cause ! mfrc522.PICC_IsNewCardPresent" 0=0
"top of loop" mfrc522.PICC_IsNewCardPresent()=0
"return cause ! mfrc522.PICC_IsNewCardPresent" 0=0
"top of loop" mfrc522.PICC_IsNewCardPresent()=0
"return cause ! mfrc522.PICC_IsNewCardPresent" 0=0
"2:" mfrc522.PICC_ReadCardSerial()=1
"return cause ! mfrc522.PICC_ReadCardSerial()" 0=0
"went below returns" 0=0
Numero :  B3 5D 89 91
Permesso? codice giusto entrare

"top of loop" mfrc522.PICC_IsNewCardPresent()=0
"return cause ! mfrc522.PICC_IsNewCardPresent" 0=0
"top of loop" mfrc522.PICC_IsNewCardPresent()=0
"return cause ! mfrc522.PICC_IsNewCardPresent" 0=0

have you tried calling

at the end of the loop?

yes but it gives me error

what error? (why do I need to ask?)

this is the error

Arduino:1.8.20 Hourly Build 2022/04/25 09:33 (Windows 10), Scheda:"Arduino Uno"





















C:\Users\ASUS\Documents\Arduino\rfid\test\test.ino: In function 'void loop()':

test:52:33: error: 'rfid' was not declared in this scope

   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

                                 ^~~~

C:\Users\ASUS\Documents\Arduino\rfid\test\test.ino:52:33: note: suggested alternative: 'rand'

   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

                                 ^~~~

                                 rand

exit status 1

'rfid' was not declared in this scope

where is that code coming from? it's not what you had in post #1....

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 
#define LED_R 4
#define RELAY 3 
#define BUZZER 2
#define ACCESS_DELAY 8000
#define DENIED_DELAY 1000

MFRC522 mfrc522(SS_PIN, RST_PIN); 
LiquidCrystal_I2C lcd(0x27,20,4);

void setup() 
{
  Serial.begin(9600);  
  SPI.begin();          
  mfrc522.PCD_Init();  
   
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
   
  digitalWrite(RELAY, LOW);
  Serial.println("Put your card to the reader...");
  Serial.println();
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Prego appoggi pure  la sua card");

}

void loop() 
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  lcd.clear();
  Serial.print("Numero : ");
  lcd.setCursor(0, 0);
  lcd.print("Numero:");
  lcd.setCursor(0, 1);
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Permesso? ");
  lcd.clear();
  content.toUpperCase();
  if (content.substring(1) == "B3 5D 89 91") 
  {
    Serial.println("codice giusto entrare");
    Serial.println();
    lcd.setCursor(0, 0);
    lcd.print("accesso autorizzato,puo' entrare");
    delay(500);
    digitalWrite(RELAY, HIGH);
    digitalWrite(LED_G, HIGH);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, LOW);
    digitalWrite(LED_G, LOW);
  }
 
 else   {
    Serial.println("codice errato riprovi");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("accesso negato,     riprovi");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
    rfid.PICC_HaltA();

}
  
}
  void reset_state()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Prego appoggi pure  la sua card");
    digitalWrite( LED_G , LOW);
    digitalWrite( LED_R , LOW);
}

so where is the faulty line

   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

in that code ???

sorry this is the error

test:92:5: error: 'rfid' was not declared in this scope

     rfid.PICC_HaltA();

     ^~~~

C:\Users\ASUS\Documents\Arduino\rfid\test\test.ino:92:5: note: suggested alternative: 'rand'

     rfid.PICC_HaltA();

     ^~~~

     rand

Uso la libreria Wire alla versione 1.0 nella cartella: C:\Users\ASUS\Desktop\FEDE\arduino-nightly\hardware\arduino\avr\libraries\Wire 

Uso la libreria LiquidCrystal_I2C-1.1.2 alla versione 1.1.2 nella cartella: C:\Users\ASUS\Documents\Arduino\libraries\LiquidCrystal_I2C-1.1.2 

Uso la libreria SPI alla versione 1.0 nella cartella: C:\Users\ASUS\Desktop\FEDE\arduino-nightly\hardware\arduino\avr\libraries\SPI 

Uso la libreria MFRC522 alla versione 1.4.10 nella cartella: C:\Users\ASUS\Documents\Arduino\libraries\MFRC522 

exit status 1

'rfid' was not declared in this scope

I suggested

ah i'm so sorry

now the error no longer occurs but the module does not restart anyway

does this compile ?
typed here based on your code, fully untested

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

const byte SS_PIN   = 10;
const byte RST_PIN  =  9;
const byte LED_G    =  5;
const byte LED_R    =  4;
const byte RELAY    =  3;
const byte BUZZER   =  2;

const unsigned long ACCESS_DELAY = 8000UL;
const unsigned long DENIED_DELAY = 1000UL;

MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 20, 4);

const byte correctCard[] = {0xB3 , 0x5D , 0x89 , 0x91};
const byte correctSize = sizeof correctCard;

void reset_state()
{
  lcd.clear();
  lcd.print("Prego appoggi pure  la sua card");
  digitalWrite( LED_G , LOW);
  digitalWrite( LED_R , LOW);
}

void setup() {
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  digitalWrite(RELAY, LOW);

  lcd.init();
  lcd.backlight();
  reset_state();
}

void loop() {
  if ( ! mfrc522.PICC_IsNewCardPresent())return;
  if ( ! mfrc522.PICC_ReadCardSerial()) return;

  lcd.clear();
  lcd.print(F("CODE:"));
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? F(" 0") : F(" "));
    lcd.print(mfrc522.uid.uidByte[i], HEX);
  }

  lcd.setCursor(0, 1);
  if ((mfrc522.uid.size == correctSize) && ! memcmp(mfrc522.uid.uidByte, correctCard, correctSize)) { // see https://cplusplus.com/reference/cstring/memcmp/
    lcd.print("accesso autorizzato,puo' entrare");
    tone(BUZZER, 3000, 300);
    digitalWrite(RELAY, HIGH);
    digitalWrite(LED_G, HIGH);
    delay(ACCESS_DELAY);
    digitalWrite(RELAY, LOW);
    digitalWrite(LED_G, LOW);
  } else {
    lcd.print("accesso negato,     riprovi");
    tone(BUZZER, 300, 300);
    digitalWrite(LED_R, HIGH);
    delay(DENIED_DELAY);
    digitalWrite(LED_R, LOW);
  }
  mfrc522.PICC_HaltA();
  reset_state();
}

The sympathiy-factor of such postings is

-100

always

post your

complete sketch

using this method that adds the code-tags automatically

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

the sympathy-factor of posting always your complete sketch is

+ 100

you got it?

best regards Stefan

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