Problem when control servo motor with RFID

:confused:

Hello every body I'm working on small project I want to control servo Motor 9g with RFID

I put the cables in right way and the rfid working good and scanning the cards but the servo not working

I just want when I scan the right card the servo should move = 90 degree

Here is my code :

#include <SPI.h>

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

Servo microservo9g;

#define SS_PIN 10
#define RST_PIN 9
// definitions pine modulo RC522
MFRC522 mfrc522(SS_PIN, RST_PIN);

// Leds indicators access released or denied
int led_green = 5;
int led_red = 6;

int pos = 0;

void setup()
{

pinMode(led_green, OUTPUT);
pinMode(led_red, OUTPUT);

// Define that the servo this connected a port digital 3
microservo9g.attach(3);
// Move o servo TO a position early (cancels closed)
microservo9g.write(pos);
// starts a serial
Serial.begin(9600);
// starts SPI bus
SPI.begin();
// Starts MFRC522
mfrc522.PCD_Init();
// posts early no serial monitor
Serial.println("Scan your card");
Serial.println();

}

void loop()
{
// Waits for the approximation of the Card
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
// display UID on serial
Serial.print("UID da tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte < 0x10 ? " " : " ");
_ Serial.print(mfrc522.uid.uidByte*, HEX);_
_ content.concat(String(mfrc522.uid.uidByte < 0x10 ? " " : " "));
content.concat(String(mfrc522.uid.uidByte, HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();*_

* // head are the Cardboard1 It was shore*
* if (content.substring(1) == "E3 44 19 13")*
* {*
* // lifts a cancel and lights the led green*

* microservo9g.write(pos);*

* digitalWrite(led_green, HIGH);
_ Serial.println("Cardboard1 - released Access !");
Serial.println();
delay(150);
microservo9g.write(pos);_

digitalWrite(led_green, LOW);
_ }*_

* // head if the read was Cardboard2*
* if (content.substring(1) == "54 74 1B 96")*
* {*
* Serial.println("Cardboard2 - Access Denied !!!");*
* Serial.println();*
* // Flashes the red LED*
* for (int i= 1; i<5 ; i++)*
* {*
* digitalWrite(led_red, HIGH);
_ delay(100);_
digitalWrite(led_red, LOW);
_ delay(100);
}
}
delay(500);
}
image.jpg*_

You are not updating the "pos" variable when the correct card is read. You can either update pos or just manually write the degrees.

  if (content.substring(1) == "E3 44 19 13")
  {
    // lifts a cancel and lights the led green
   
    microservo9g.write(90);
 
    digitalWrite(led_green, HIGH);
    Serial.println("Cardboard1 - released Access !");
    Serial.println();
    delay(150);
    microservo9g.write(0);

    digitalWrite(led_green, LOW);
    }

This should move the servo to 90 when the correct card is read, delay(150) and then move the servo back to 0.

Thanks a lot dear I will try it later
And I will tell what happenes to me again okay thank you so much

I update the code but nothing the servo not moving

I would try removing microservo9g.write(0); after delay(150), this is telling the servo to go back to 0 before if ever gets a chance to get to 90, either that or change to delay to a longer time (like maybe 3000 to start with).

  if (content.substring(1) == "E3 44 19 13")
  {
    // lifts a cancel and lights the led green
   
    microservo9g.write(90);
 
    digitalWrite(led_green, HIGH);
    Serial.println("Cardboard1 - released Access !");
    Serial.println();
    delay(3000);
    microservo9g.write(0);

    digitalWrite(led_green, LOW);
    }

Thanks a lot working 100%

The Servo library does not (and cannot) wait for the servo to move, because there
is no way of sensing whether its moved or how fast. So the latest call to write() or
writeMicroseconds() is always applied immediately without delay and control returns.

Also a hobby servo is not normally called a servo motor, its a servo mechanism, and
normally just called a servo. Servo motors are motors with high resolution encoder,
full servo controller capable of continuous rotation and speed, position, torque control
and are expensive industrial items usually (unless you cobble together your own control
loops)