If Statement and Variable Not Doing What I ask of it

I am trying to write a program for an RFID doorlock and so I created a variable for the servo’s position and rotate the servo to that variable given a circumstance (for this program 0 is unlocked and 100 is locked). My problem is that I think my variable is stuck on 100 even if I start by defining it as 0 so it only executes the if statement where it’s equal to 100, I’ve tried everything to fix using global and local variables but nothing works. Can anyone help? Thanks.

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

 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

Servo servo_6;
int pos = 0; // Postion for servo
 
void setup() 
{
  servo_6.attach(6, 544, 2400);
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();

}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  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("Message : ");
  content.toUpperCase();

  if (content.substring(1) == "C5 49 50 2D") //UID for card access
  {
    if (pos = 100) { // Door is lokced and needs to be unlocked
      Serial.println("Authorized access");
      Serial.println();
      // Serial.println("your here 1");
      // Serial.println(pos);
      delay(1000);

      pos = pos - 100;
      // Serial.println(pos);
      servo_6.write(pos);
      // Serial.println(pos);
    }
    
    else if (pos = 0) { // Door is unlokced and needs to be locked
      Serial.println("Authorized access");
      Serial.println();
      // Serial.println("your here 2");
      // Serial.println(pos);
      delay(1000);
    
      pos = pos + 100;
      // Serial.println(pos);
      servo_6.write(pos);
      // Serial.println(pos);
    }
  }

 else { // Wrong UID card
    Serial.println(" Access denied");
    delay(1000);
  }
}

That is an assignment statement.

try to change = to ==

if (pos == 100) { // Door is lokced and needs to be unlocked
Serial.println(“Authorized access”);
Serial.println();
// Serial.println(“your here 1”);
// Serial.println(pos);
delay(1000);

  pos = pos - 100;
  // Serial.println(pos);
  servo_6.write(pos);
  // Serial.println(pos);
}

else if (pos == 0) { // Door is unlokced and needs to be locked
  Serial.println("Authorized access");
  Serial.println();
  // Serial.println("your here 2");
  // Serial.println(pos);
  delay(1000);

Yeah, it works now, thanks, sometimes the smallest mistakes make the biggest problems

1 Like

Yeah it works now, I forgot about the differences between = and ==, thanks so much.

1 Like

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