Boolean not changing

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)

"=" for assign, "==" for compare.

Hint also: if a Boolean isn't true, there's no need to do another "if", an "else" will suffice.

I just changed that because I forgot to do that, but its still printing:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

PICC type: MIFARE 1KB
Scanned PICC's UID:
103 216 57 99Opening...
Closing...

I know this card!
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

PICC type: MIFARE 1KB
Scanned PICC's UID:
103 216 57 99Opening...
Closing...

I know this card!
0
0
0
0
0
0
0
0
0
0
0
0

PICC type: MIFARE 1KB
Scanned PICC's UID:
103 216 57 99Opening...
Closing...

I know this card!
0
0
0
0
0
0
0
0
0

PICC type: MIFARE 1KB
Scanned PICC's UID:
103 216 57 99Opening...
Closing...

I know this card!
0
0
0
0
0
0
0
0

Code is now:

//VIRAL SCIENCE
//www.youtube.com/c/viralscience
#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}; //Enter your UID
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 == code*))*
* {*
* 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 < 0x10 ? " 0" : " ");
Serial.print(buffer, 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();*
* }*

Please remember to use code tags when posting code

Look at this pice of code:

if (match)
  {
    if (checkOpen = false) {
      Serial.println("Opening...");
      motor.write(0);

      checkOpen = true;
    }
    if (checkOpen = true) {

      motor.write(90);
      Serial.println("Closing...");
      checkOpen = false;
    }

When "match" is true and checkOpen is false You print out "Opening" and sets "checkOpen" to true.
What do You think happends in the next line of the code?
Your Serial monitor should show "Opening" and directly "Closing". Add a printout of millis()! Then You can see when things happends.

Here is the code. This is the way You should prefere to use.

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

[Fixed]

I just changed it to an else if, totally forgot about that.... oops

You could have reduced if (checkOpen = true) to just else....
Working better now?

Railroader:
You could have reduced

if (checkOpen = true)

to just

else

....
Working better now?

You probably mean:

if (checkOpen == true)

Yes, the opposite… First test if for false and then no need for an if …… true.

You probably meanif (checkOpen)

Good evening,

@Agent_scout:

Can you please share the final code you use to get this working?

Thanks in advance for your reply

Rex

Hello guys,

Like the OP, I'm trying to use the same card to move a servo to 180 (starting at 30) and when the card is swapped again it will go back to 30 but after cleaning the code posted by agent_scout as I don't use any of the Adrafruit stuff I still can't make this to work; last message from him was "Fixed I just changed it to an else if, totally forgot about that.... oops" and message from Railroader was not clear to me (sorry about that)

If someone can take a look at what I have and help me out I will be really grateful

Thanks in advance for your reply

#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>

Servo motor;


#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key key;

int code[] = {24, 16, 27, 39};
int codeRead = 0;
String uidString;

boolean checkOpen = false;

void setup() {

 Serial.begin(9600);
 motor.attach(3);
motor.write(30);
 SPI.begin(); // Init SPI bus
 rfid.PCD_Init(); // Init MFRC522



}

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

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


 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(180);

     checkOpen == true;
   }
   if (checkOpen = true) {

     motor.write(0);
     Serial.println("Closing...");
     checkOpen = false;
   }

   Serial.println("\nI know this card!");

 } else
 {
   Serial.println("\nUnknown Card");
  
 }


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

Do we 2 OPs here, agent_scout and rexray16?

You have used = here where == is required:

    if (checkOpen = false)

Same thing a few lines later

wildbill:
You have used = here where == is required:

    if (checkOpen = false)

Same thing a few lines later

A usual mistake, just typing too fast.

wildbill:
You have used = here where == is required:

    if (checkOpen = false)

Same thing a few lines later

Thank you so much Wildbill, I will make the modification and report back

Best regards,

Railroader:
A usual mistake, just typing too fast.

Thank you Railroader for your clarification, I will work on this tomorrow and hopefully it will work when I replace those two lines

I appreciate your time and help

Best regards,

Good afternoon guys,

I did the changes as Wildbill ans Railroader pointed out:

int i = 0;
  boolean match = true;
  while (i<rfid.uid.size)
  {
    if (!(rfid.uid.uidByte[i] == code[i]))
    {
      match = false;
    }
    i++;
  }

  if (match)
  {
    if ([b]checkOpen == false[/b]) {
      Serial.println("Opening...");
      motor.write(0);

      checkOpen = true;
    }
    if ([b]checkOpen == true[/b]) {

      motor.write(180);
      Serial.println("Closing...");
      checkOpen = false;
    }

    Serial.println("\nI know this card!");

  } else
  {
    Serial.println("\nUnknown Card");

but the code servo still misbehaving as when I compile and send the code, the servo goes to the 90 position until I scan the card, then it goes to 180 and no matter how many times I swap the card, the servo doesn't go to 30 (one weird thing I noticed is that I open the serial monitor the servo goes to 30 ??????)

I really, really appreciate if you guys can help me with this

Thanks in advance

When You open Serial Monitor the controller is reset by the system.