I am nearing the end of my dissertation project but still have a few things to finish...I am using the Arduino R4 Wi-Fi with a HC-SR04 Ultrasonic Distance Sensor and RC522 RFID Reader hooked up to the board. The board has been coded so when an object is outside the 30cm threshold of the sensor, the RFID scanner will scan for cards.
Attached is a screenshot of my code. I am trying to get "Airstairs Stowed Correctly" after scanning a specific UID and "Airstairs Stowed Incorrectly" if it reads a different UID. It currently displays "Airstairs Stowed Correctly" regardless of what card was scanned.
I'm not sure if this is the right way of doing this.
As I am still fairly new to Arduino thought it would be easier to get more experienced opinions!
Please do not post pictures of code. Post a complete sketch using code tags when you do
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
// establish and initalise libraries for for the RFID module
#include "rfid1.h" //library reproduced from https://docs.sunfounder.com/projects/elite-explorer-kit/en/latest/basic_projects/08_basic_mfrc522.html#code
RFID1 rfid1; // this creates an instance of the RFID1 class
// Define Trig and Echo pins for the range finder module:
#define trigPin 9
#define echoPin 10
#define buzzPin 11
// Distance threshold in centimeters
#define STOP_DISTANCE 5
// Unique Identification of Card
#define uid
// Serial Number
//#define serNum
// Define variables for the ranger finder:
long duration;
int distanceThreshold = 30; // Define your desired distance threshold in centimeters
int minBeepInterval = 750; // Minimum interval between beeps in milliseconds
int maxBeepInterval = 200; // Maximum interval between beeps in milliseconds
void setup() {
// Define inputs and outputs for the ranger finder and buzzer:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzPin, OUTPUT);
Serial.begin(9600); // Begin Serial communication at a baudrate of 9600
// Define PINS for the RFID module
rfid1.begin(7, 5, 4, 3, 6, 2); // IRQ_PIN, SCK_PIN, MOSI_PIN, MISO_PIN, SDA_PIN, RST_PIN
delay(200); // Delay for 100 milliseconds
rfid1.init(); // Initialize the RFID module
}
void loop() {
uchar status; // Status variable to hold the state of RFID functions
uchar str[MAX_LEN]; // Create an array to hold the RFID card data; maximum length is 16
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // Changed 5 to 2 as it's more commonly used
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Changed 5 to 10 as it's more commonly used
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
int distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(50); //
// Check if distance is less than or equal to the stop distance threshold
if (distance <= STOP_DISTANCE) {
Serial.println("STOP"); // Display "STOP" in the Serial Monitor
}
// Buzzer and RFID controls
if (distance < distanceThreshold) {
// If an object is within the defined threshold distance, start the buzzer
int beepInterval = map(distance, 3, distanceThreshold, maxBeepInterval, minBeepInterval);
// Beep the buzzer with the calculated interval
digitalWrite(buzzPin, HIGH);
delay(beepInterval);
digitalWrite(buzzPin, LOW);
delay(beepInterval);
} else {
// If no object or object is beyond threshold distance, turn off the buzzer
digitalWrite(buzzPin, LOW);
// If not in close proximity, start to search for an RFID card and get its type
status = rfid1.request(PICC_REQIDL, str);
if (status != MI_OK) {
return; // If no card is found, return to the beginning of loop()
}
Serial.print("Card type: ");
Serial.println(rfid1.readCardType(str)); // Print the card type to the serial monitor
// Prevent RFID card collision and get the 4-byte serial number of the card
status = rfid1.anticoll(str);
if (status == MI_OK) {
Serial.print("The card's number is: ");
int IDlen = 4; // Length of the card's serial number
for (int i = 0; i < IDlen; i++) {
Serial.print(str[i], HEX); // Print each byte of the card's serial number
}
Serial.println();
Serial.println(); // Print an empty line for better readability
}
Serial.print("UID Tag: ");
Serial.println(uid);
if (uid "1460EE6A") {
Serial.println("Airstairs Stowed Correctly");
} else {
if (uid "19BD147A")
Serial.println("Airstairs Stowed Incorrectly");
}
}
}
Careful with this bit. Have a close look at the curly braces. What happens if you add one more line of code below the Serial.println above? Under which conditions will it be executed?
I was hoping that after scanning a code, it would display the statement in the serial monitor, however I want one card to be "correct" and the other "incorrect". For some reason, regardless of what card is scanned, they are both "correct". I am not sure how to resolve this issue.
I was hoping that after the RC522 scans an RFID tag, it would display the statement in the serial monitor, however I want one card to be "correct" and the other "incorrect". For some reason, regardless of what card is scanned, they are both "correct". So for example
RFID scans X, it reads the Card Number, if UID is 12345, display Y, if UID is 67890, display Z
Not sure if I need to get it to read a string as it is letters and numbers etc.