Integrating a Servo in my project!

I start working on a project and I got stuck when I tried to add a servo but I am not good at coding, any help?
This is the project : http://educ8s.tv/arduino-rfid-tutorial/
and on top of that I add the servo that should move when RFID reads the tag.

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


#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

boolean card;


MFRC522::MIFARE_Key key;

int code[] = {236, 52, 228, 140}; //This is the stored UID
int codeRead = 0;
String uidString;
void setup() {


  ////SERVO settings

  Servo myservo;
  myservo.attach(3);
  myservo.write(0);
  Serial.println();
  Serial.print("Message : ");
  Serial.println("Authorized access");
  Serial.println();
  delay(300);
  myservo.write(180);
  delay(900);
  myservo.write(0);
  myservo.write(180);
  
  
  Serial.begin(9600);
  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("RFID Lock");
  display.display();

}

void loop() {
  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)
  {
    Serial.println("\nI know this card!");
    printUnlockMessage();
  } 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);
  }
}

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:Alex ");
  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("RFID Lock");
  display.display();

  display.setTextColor(WHITE); // or BLACK);
  display.setTextSize(2);
  display.setCursor(10, 0);
  display.print("Unlocked");
  display.display();
 

  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("RFID Lock");
  display.display();
}

Move that to global scope.
As it is, it is only usable within the setup function.

Where exactly, I have 5 days working with this code. I do not know much so please be more specific. Thx for answering though.

Or can anyone correct my code and post it again, please. thx

This variable has global scope.

So does this one.

I suspect most of the Servo library examples you have seen will have the Servo object having global scope.

So I should move Servo myservo; above void setup() ?

That's it.

And if you still have problems it would help if you say exactly what the problems are.

Steve

Following the code above, the RFID reads and shows Unlocked on my OLED screen but the servo does not want to move, earlier it was moving right when plugged into a bank battery or laptop but not when I scan the tag. I think I have not added the proper code. I don't get any errors it's just not working.

Post your code as it looks now.

Servo moves right when I upload the code or the first time when it gets some power but not when I read the tag.

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


#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

boolean card;
Servo myservo;

MFRC522::MIFARE_Key key;

int code[] = {236, 52, 228, 140}; //This is the stored UID
int codeRead = 0;
String uidString;
void setup() {


  ////SERVO settings

  myservo.attach(3);
  myservo.write(0);
  Serial.println();
  Serial.print("Message : ");
  Serial.println("Authorized access");
  Serial.println();
  delay(300);
  myservo.write(180);
  delay(900);
  myservo.write(0);
  myservo.write(180);


  Serial.begin(9600);
  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("RFID Lock");
  display.display();

}

void loop() {
  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)
  {
    Serial.println("\nI know this card!");
    printUnlockMessage();
  } 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);
  }
}

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:Alex ");
  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("RFID Lock");
  display.display();

  display.setTextColor(WHITE); // or BLACK);
  display.setTextSize(2);
  display.setCursor(10, 0);
  display.print("Unlocked");
  display.display();


  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("RFID Lock");
  display.display();
}

ignore ```
Servo myservo;

The only code to move the servo is in the setup function.
The setup function is only called once, immediately after reset.

I cant figure it out. I think a should add myservo.write(180) somewhere to link it with RFID tag reading but thats just a presumption, like I said I`m clueless.

Thank you for trying to help me, guys. I will try more tomorrow, have a good day.

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