RFID based motor speed controller

Hey There,
I have been working on a project in which i am using Different RFID cards to change motor(DC) speed to a different value, basically varying from (0, 255).

I am having problem with coding. The RFID cards are being read in my code but the motor speed is not being changed.

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

//MOTOR 1

int enA = 6;
int in1 = 8;
int in2 = 7;

//MOTOR 2

int enB = 3;
int in3 = 5;
int in4 = 4;
 
void setup()
{
  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();

  //set all motor control pins to output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  analogWrite(enA, 250);
  analogWrite(enB, 250);

}
void loop()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  // 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) == "C9 B4 05 31") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SCHOOL AHEAD");
    Serial.println();
    analogWrite(enA, 100);
    analogWrite(enB, 100); 
    delay(2000);
  }
  if (content.substring(2) == "09 26 FA 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SAFE AHEAD");
    Serial.println();
    analogWrite(enA, 250);
    analogWrite(enB, 250);
    delay(2000);
  }
 if (content.substring(3) == "39 91 F6 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("U TURN");
    Serial.println();
    analogWrite(enA, 75);
    analogWrite(enB, 75);
    delay(2000);
  }
}

project1.ino (2.28 KB)

Thanks for adding the code. Too many OPs don't.
If You had followed the advice, rules, etc. You had supplied the code using "code tags", using the symbol up to the left in this window. It looks like </>. That creates a scrollable window showing the code for us. Some members just don't want to fill thier compers with OPs code, some uses tablets and can't read it.

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

//MOTOR 1

int enA = 6;
int in1 = 8;
int in2 = 7;

//MOTOR 2

int enB = 3;
int in3 = 5;
int in4 = 4;
 
void setup() 
{
  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();

  //set all motor control pins to output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  analogWrite(enA, 250);
  analogWrite(enB, 250);

}
void loop() 
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  // 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) == "C9 B4 05 31") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SCHOOL AHEAD");
    Serial.println();
    analogWrite(enA, 100);
    analogWrite(enB, 100);  
    delay(2000);
  }
  if (content.substring(2) == "09 26 FA 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SAFE AHEAD");
    Serial.println();
    analogWrite(enA, 250);
    analogWrite(enB, 250); 
    delay(2000);
  }
 if (content.substring(3) == "39 91 F6 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("U TURN");
    Serial.println();
    analogWrite(enA, 75);
    analogWrite(enB, 75); 
    delay(2000);
  }
}

You are detecting 3 different cards. For each card You can set a global speed variable that will be used as a delay timer for the code turning the motor.
Then only step the motor when timer has passed the waiting, set the time by adding the "speed variable" mS for next update of stepping.

I am not able to do that you mentioned.
Could you please code that for me ?
Do it for 1 card , then i'll do the remaining.
Thanks. :frowning:

Not being able? I hope You've got a keyboard and some Pc?
Ok, I'll make a try try. Look for "added by Rr" in the code. Be prepared to correct faults!

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

//MOTOR 1

int enA = 6;
int in1 = 8;
int in2 = 7;

//MOTOR 2

int enB = 3;
int in3 = 5;
int in4 = 4;
 
unsigned long step_delay;// added by Rr
unsigned long step_time; // added by Rr
boolean run_stepping = False;// added by Rr

void setup() 
{
  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();

  //set all motor control pins to output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  analogWrite(enA, 250);
  analogWrite(enB, 250);

}
void loop() 
{
  if( run_stepping ) //added by Rr
 {//added by Rr
    if( millis() - step_time > step_delay )//added by Rr
    { //added by Rr
       digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
     }
  }
  // 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) == "C9 B4 05 31") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SCHOOL AHEAD");
    Serial.println();
    analogWrite(enA, 100);
    analogWrite(enB, 100);  
    step_delay = 2000;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }
  if (content.substring(2) == "09 26 FA 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SAFE AHEAD");
    Serial.println();
    analogWrite(enA, 250);
    analogWrite(enB, 250); 
    step_delay = 2000;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }
 if (content.substring(3) == "39 91 F6 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("U TURN");
    Serial.println();
    analogWrite(enA, 75);
    analogWrite(enB, 75); 
    step_delay = 2000;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }
}

To be very true , I am new to Arduino. This assignment was given on short notice. I somehow managed to attach RFID, Motor Controller and did some research and coded this much.

I tested your code and tried it. It's working.
Could you please help me with other cards? I am finding it difficult.
Thanks :frowning:

Getting the RFID working on Your own is greate as You're a bit new to this.
Whow! You say it works? That happends very seldom.
If You want to use more cards just duplicate the code You have. Copy the code for the first card, change names and modify step_delay according to Your needs.

This piece:

  if (content.substring(1) == "C9 B4 05 31") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SCHOOL AHEAD");
    Serial.println();
    analogWrite(enA, 100);
    analogWrite(enB, 100);  
    step_delay = 2000;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }

I tried your way, the code compiled. But It now has different problems.

  1. only the first card is scanned and motor runs according to it, The remaining cards are scanned but the motor speed does not change.
  2. All the cards are scanned and the motor remains at rest.

I am attaching code here please have a look and tell me what is wrong with it.

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

//MOTOR 1

int enA = 6;
int in1 = 8;
int in2 = 7;

//MOTOR 2

int enB = 3;
int in3 = 5;
int in4 = 4;
 
unsigned long step_delay;// added by Rr
unsigned long step_time; // added by Rr
boolean run_stepping = false;// added by Rr



void setup()
{
  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();

  //set all motor control pins to output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  analogWrite(enA, 250);
  analogWrite(enB, 250);

}
void loop()
{
  if( run_stepping ) //added by Rr
 {//added by Rr
    if( millis() - step_time > step_delay )//added by Rr
    { //added by Rr
       digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
     }
  }
 
  // 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) == "C9 B4 05 31") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SCHOOL AHEAD");
    Serial.println();
    analogWrite(enA, 100);
    analogWrite(enB, 100); 
    step_delay = 500;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }
  if (content.substring(2) == "09 26 FA 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SAFE AHEAD");
    Serial.println();
    analogWrite(enA, 250);
    analogWrite(enB, 250);
    step_delay = 1000;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }
 if (content.substring(3) == "39 91 F6 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("U TURN");
    Serial.println();
    analogWrite(enA, 75);
    analogWrite(enB, 75);
    step_delay = 1500;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }
  if (content.substring(4) == "23 C8 D5 2E") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Safe1");
    Serial.println();
    analogWrite(enA, 75);
    analogWrite(enB, 75);
    step_delay = 2500;//added by Rr
    run_stepping = true;//added by Rr
//    delay(2000); // removed added by Rr
  }
}

That's good. First time code usually fails. What does the Serail monitor show from this piece of code when You try the first card, the second card etc?

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

I have attached foe both the cases.

Your pics show that different cards have been used, However the printout shows "SCHOOL AHEAD" regardles of what card is used. Hmmm. Strange.
I need to look closer. You do the same!

Your library is unknown to me. It looks like You fill up the 4 dimensionel Array

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

Then the for-loop tests first, second, third and forth chars in the Array.
I can't find out how to know what is the last card being red.

I feel that's were the issue is.

Try to run the sketch and don't use the fist card, "SCHOOL AHEAD", at all. Attache the files like You just did!

As you said I deleted the first card "SCHOOL AHEAD" code.

In the below code I only used two cards, now the RFID scans the card(both of them) but there is nothing input to the motors. They don't even start.

I am attaching both the Code and Screenshot for the Serial Monitor.

Also in this project I want to use 6 different card.

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

//MOTOR 1

int enA = 6;
int in1 = 8;
int in2 = 7;

//MOTOR 2

int enB = 3;
int in3 = 5;
int in4 = 4;
 
unsigned long step_delay;// added by Rr
unsigned long step_time; // added by Rr
boolean run_stepping = false;// added by Rr



void setup()
{
  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();

  //set all motor control pins to output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  

}
void loop()
{
  if( run_stepping ) //added by Rr
 {//added by Rr
    if( millis() - step_time > step_delay )//added by Rr
    { //added by Rr
       digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
     }
  }
 
  // 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(2) == "09 26 FA 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SAFE AHEAD");
    Serial.println();
    analogWrite(enA, 250);
    analogWrite(enB, 250);
    step_delay = 2000;//added by Rr
    run_stepping = true;//added by Rr

  }
 if (content.substring(3) == "39 91 F6 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("U TURN");
    Serial.println();
    analogWrite(enA, 75);
    analogWrite(enB, 75);
    step_delay = 1000;//added by Rr
    run_stepping = true;//added by Rr

  }
  
}

Why don't you put some Serial.prints in of content.substring(2) and content.substring(3) and see if they actually contain what your if statements are looking for?

Steve

Thanks for the suggestion but as you know I am new to Arduino.

Could you code that for me ?
I tried but Couldn't print my if loop , instead it kept printing the RFID Card Code.

I wonder if the code for the motor works. Is it a stepper motor, or what?

 if( millis() - step_time > step_delay )//added by Rr
    { //added by Rr
       digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
     }

For a stepper, this code can't work. The outputs are set and then they are the same. No turn of.....

Both the motor i used are DC motor

Add a this to the digitalWrite sequence in my previous post.

Serial.println("Running the motor");

Does the message show up in Serial Monitor?

I am using DC motors.
The code you helped me is for DC Motor Right ?

I copied your codeSerial.println("Running the motor"); it worked in the if loop for the SCHOOL AHEAD card and didn't printed that in the other two case.

I am attaching again the code and serial Port monitor result.

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

//MOTOR 1

int enA = 6;
int in1 = 8;
int in2 = 7;

//MOTOR 2

int enB = 3;
int in3 = 5;
int in4 = 4;
 
unsigned long step_delay;// added by Rr
unsigned long step_time; // added by Rr
boolean run_stepping = false;// added by Rr

void setup()
{
  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();

  //set all motor control pins to output
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  analogWrite(enA, 250);
  analogWrite(enB, 250);

}
void loop()
{
  if( run_stepping ) //added by Rr
 {//added by Rr
    if( millis() - step_time > step_delay )//added by Rr
    { //added by Rr
       digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
     }
  }
  // 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) == "C9 B4 05 31") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SCHOOL AHEAD");
    Serial.println("Running the motor");
    Serial.println();
    analogWrite(enA, 100);
    analogWrite(enB, 100); 
    step_delay = 2000;//added by Rr
    run_stepping = true;//added by Rr

  }
 else if (content.substring(2) == "09 26 FA 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("SAFE AHEAD");
    Serial.println("Running the motor");
    Serial.println();
    analogWrite(enA, 250);
    analogWrite(enB, 250);
    step_delay = 1500;//added by Rr
    run_stepping = true;//added by Rr

  }
 else if (content.substring(3) == "39 91 F6 30") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("U TURN");
    Serial.println("Running the motor");
    Serial.println();
    analogWrite(enA, 75);
    analogWrite(enB, 75);
    step_delay = 500;//added by Rr
    run_stepping = true;//added by Rr

  }
}