Do parallal tasks using threads and timer

Hi Arduino Geeks,

I'm new to Arduino programming, and currently, I'm doing a college project.
I'm creating an RFID-operated door using Uno, RC522 and Servo motor by help of sample projects in internet.
My goal is to continuously track RFID ID while the servo motor rotates 0 to 90 degrees and again rotates to 0 degrees. [Open door(servo motor 90 degree position) if RFID tag detects and close door(servo motor 0 degree position) with delay 2000 miliseconds, but within the duration 2000 miliseconds required to track RFID tags continuously and keep open door if RFID tag detects until end detection ].

As delay() freeze application i have use timer to revert Servo motor in the timer(example of BlinkWithoutDelay), but Servo motor function is not working fine for my expectation. Can you please explain what is the error in my code.

Thanks In Advance. (Power to servo will taken from external power-in)

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

#define RST_PIN  9        
#define SS_PIN  10       
#define SERVO_PIN 3

byte oldId[4];
int countIdentical;
unsigned long prevMillis;
const unsigned long delayInterval = 2000; // interval to avoid double registration

unsigned long previousMillis = 0; 
const long interval = 1000;  

Servo servo;
int angle = 0;

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

static struct pt pt1; // each protothread needs one of these

void setup() {
  Serial.begin(9600);   // Initialize serial communications with the PC1
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
  for(int i=1; i<4; i++) {
  prevMillis = millis();
  PT_INIT(&pt1);  // initialise the two
  servo.write(angle); // rotate servo motor to 0°
static int protothread1(struct pt *pt) {
     angle = 90;

void loop() {

  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
  countIdentical = 0;
  for ( uint8_t i = 0; i < 4; i++) { 
    if (mfrc522.uid.uidByte[i] == oldId[i]) {
  if (countIdentical != 4) {
      for ( uint8_t i = 0; i < 4; i++) { 
        if (mfrc522.uid.uidByte[i] < 0x10) {
        Serial.print(mfrc522.uid.uidByte[i], HEX);
    unsigned long currentMillis = millis();
  if ((currentMillis - prevMillis) > delayInterval) {
    if(angle == 90){
      angle =0;
        angle = 90;
      for ( uint8_t i = 0; i < 4; i++) {

Not a great start

They were all already zero, but did you deliberately forget one?

1 Like

Thank you very much for your time & reply.
As i said this code was taken form internet and amend according to my requirement. Yes i have noticed it but i didn't removed it, as RFID tag reading process is working fine.

Does it really matters working flow of the code ?

But my major concern is, i couldn't able to operate Servo motor(sg90) as my expectation

Could you explain that a bit more please.

1 Like

If using a Uno, Mega, and so forth: StateMachine - Arduino Reference

If using a ESP32 use the built in OS FreeRTOS API categories

1 Like

Sir, 5v Power will be given using Breadboard power supply.

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