Problem get back to reset state when ultrasonic sensor finish reading counter.

Help needed! Basically this project about when valid RFID card is granted it activate the ultrasonic sensor to do counter (if distance < 20) which reduce from 5 to 0 then reset back initial state until new RFID card is granted to do same operation as above.

My problem is when valid RFID card is present and the Tag ID is match, it activate the ultrasonic sensor after 2 second and the counter reduce from 5 to 0. It never back to reset state when counter reach 0 and when another same RFID card is present, it never start new operation.

I just need when RFID if granted, after 2 second ultrasonic sensor activate and reduce counter from 5 to 0(if distance<20) and once complete back to reset state.

Here is my code :frowning:

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

#define SS_PIN 10 
#define RST_PIN 9 

//Define pins ultrasonic(trig,echo)
Ultrasonic ultrasonic(6,7);

//Variables
byte readCard[4];
String MasterTag = "6F181B";
String MasterTag1 = "89ED52D3";

String tagID = "";

int count = 5;
int distance;

LiquidCrystal_I2C lcd(0x27,16,2);
MFRC522 mfrc522(SS_PIN, RST_PIN); // Instance of the class

void setup() 
{  
   lcd.init();
   lcd.backlight();
   lcd.setCursor(0,0);
   lcd.print("     Welcome");
   lcd.setCursor(0,1);
   lcd.print(" Scan your card ");
   
   Serial.begin(9600); 
   SPI.begin();       // Init SPI bus
   mfrc522.PCD_Init(); // Init MFRC522 
}

void loop() 
{ 
  while (getID())
  {   
    if (tagID == MasterTag) 
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" Access Granted!");
      lcd.setCursor(5, 1);
      lcd.print(tagID);
      delay(2000);
      lcd.clear();
     }

    else
    {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" Access Denied!");
      lcd.setCursor(0, 1);
      lcd.print(tagID);
      delay(1000);
      reset_state(); 
    }  
    
  }

if ((tagID == MasterTag)) 
{
    distance = ultrasonic.Ranging(CM);
    delay (500);  

    if (distance <= 10)
       { 
           count = count - 1;
           Serial.print(count);
           lcd.setCursor(0,0);
           lcd.print(count);

         if(count<=0)
           {
             reset_state();
           }
       }
}
       

}

boolean getID() 
{
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  { //If a new PICC placed to RFID reader continue
  return false;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  { //Since a PICC placed get Serial and continue
  return false;
  }
  tagID = "";
  for ( uint8_t i = 0; i < 4 ; i++) 
  { // The MIFARE PICCs that we use have 4 byte UID
  readCard[i] = mfrc522.uid.uidByte[i];
  //tagID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
  }
  tagID.toUpperCase();
  mfrc522.PICC_HaltA(); // Stop reading
  return true;
}


void reset_state()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("     Welcome");
    lcd.setCursor(0,1);
    lcd.print("Scan your card "); 
}

I expect something wrong with my coding in this part. Much appreciated if someone help to review my coding and give solutions.Thanks

if ((tagID == MasterTag)) 
{
    distance = ultrasonic.Ranging(CM);
    delay (500);  

    if (distance <= 10)
       { 
           count = count - 1;
           Serial.print(count);
           lcd.setCursor(0,0);
           lcd.print(count);

         if(count<=0)
           {
             reset_state();
           }
       }
}

You have a decent amount of output to Serial. When you run the sketch with Serial Monitor open, what does it tell you?

In Serial monitor it print ‘4 3 2 1 0 - 1 - 2 -3 continuously if place any object at distance <20cm ’ in LCD display it shows until 0 and go to ‘Welcome, Scan your Card’ screen.

When i tap again the valid RFID card, LCD display still keep on showing ‘Welcome, scan your card without going to counter screen. (I suspect when counter reach <= 0 it keeps on reading ‘Welcome’ screen). I need to end loop once it reach counter = 0.

Suppose once reach counter 0, it should be return to initial state to wait for next card to reduce count from 5.

So the problem is that your reset_state() function doesn't actually reset the state. You need to set the value of count back to 5:

count = 5;

When i set count = 5; instead of reset_state, the counter print until 0 and return to 4. Continously printing 4 to 0.

I attached the serial monitor screenshot.

I think because the coding below in loop function, it keeps on read based on condition set (distance<20) from begining again and again whenever condition set is met.

I just want it reads one time from 4 to 0 and then back initial state without repeating again 4 to 0.
I tried alot of ways but still not success.

When again i tap RFID card, the counter should start from 4 reduce to 0 and end the operation.

if ((tagID == MasterTag)) 
{
    distance = ultrasonic.Ranging(CM);
    delay (500);  

    if (distance <= 10)
       { 
           count = count - 1;
           Serial.print(count);
           lcd.setCursor(0,0);
           lcd.print(count);

         if(count<=0)
           {
             count = 5;
           }
       }
}

Are you saying that ultrasonic.Ranging(CM) is returning an incorrect value, that there is no object closer than 10 cm to the sensor?

No, there is no issues with ultrasonic.Ranging(CM). It just used to convert distance into centimeter since i use ultrasonic.h library.

When I tap RFID card and TagID is matched after 2 seconds it should activate Ultrasonic sensor and do counter reducing from 4 to 0 whenever if any object present at distance less than 10cm. If no object present at distance less than 10, the counter won’t reduce till something present at distance <10cm.

The problem now is when it reduce from 4 to 0. It never end the loop. It still being return to 4 again to reduce to 0 continously. I just want one time it reduce from 4 to 0 and stop everything.