Hey, this program is supposed to run a motor when i scan a rfid card and say whether the motor is opening or closing the lock. i have the boolean set to start at false(which is closed). once the card is scanned, it runs the motor, sends a message, and changes the boolean to true(which is opened). It didn't work when I ran it, so I decided to put a println in the loop that shows the value of checkOpen(the name of the boolean), it isn't changing to true ever, it stays at 0 the whole time.
Here's the code:
#include <MFRC522.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Servo.h>
Servo motor;
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
int code[] = {103,216,57,99};
int codeRead = 0;
String uidString;
boolean checkOpen = false;
void setup() {
Serial.begin(9600);
motor.attach(7);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// Clear the buffer.
display.clearDisplay();
display.display();
display.setTextColor(WHITE); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("SCAN RFID");
display.display();
}
void loop() {
Serial.println(checkOpen);
if( rfid.PICC_IsNewCardPresent())
{
readRFID();
}
delay(100);
}
void readRFID()
{
rfid.PICC_ReadCardSerial();
Serial.print(F("\nPICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
clearUID();
Serial.println("Scanned PICC's UID:");
printDec(rfid.uid.uidByte, rfid.uid.size);
uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);
printUID();
int i = 0;
boolean match = true;
while(i<rfid.uid.size)
{
if(!(rfid.uid.uidByte[i] == code[i]))
{
match = false;
}
i++;
}
if(match)
{
if (checkOpen = false){
Serial.println("Opening...");
motor.write(0);
checkOpen = true;
}
if(checkOpen = true){
motor.write(90);
Serial.println("Closing...");
checkOpen = false;
}
Serial.println("\nI know this card!");
printUnlockMessage();
}else
{
Serial.println("\nUnknown Card");
printDeniedMessage();
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
void clearUID()
{
display.setTextColor(BLACK); // or BLACK);
display.setTextSize(1);
display.setCursor(30,20);
display.print(uidString);
display.display();
}
void printUID()
{
display.setTextColor(WHITE); // or BLACK);
display.setTextSize(1);
display.setCursor(0,20);
display.print("UID: ");
display.setCursor(30,20);
display.print(uidString);
display.display();
}
void printUnlockMessage()
{
display.display();
display.setTextColor(BLACK); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("SCAN RFID");
display.display();
display.setTextColor(WHITE); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("UnLocked");
display.display();
delay(2000);
display.setTextColor(BLACK); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("UnLocked");
display.setTextColor(WHITE); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("SCAN RFID");
display.display();
}
void printDeniedMessage()
{
display.display();
display.setTextColor(BLACK); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("SCAN RFID");
display.display();
display.setTextColor(WHITE); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("Denied!!");
display.display();
delay(2000);
display.setTextColor(BLACK); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("Denied!!");
display.setTextColor(WHITE); // or BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("SCAN RFID");
display.display();
}
RFID.ino (4.66 KB)