Help with speed control on servo

Hi there!

I've been trying to add in speed control to my code that controls servo motion activated with an RFID tag. Right now, the servo moves much to fast because it's trying to get where I'm telling it to go as fast as possible. I tried adjusting the delay, but that isn't working. I researched some topics on the varspeedservo library but can't seem to work out how this would fit into my code.

Any guidance would be greatly appreciated.

Here is the code I am currently using

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

int flag = 1;
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();          // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Put your card to the reader...");
  Serial.println();

  servo.attach(3);
  servo.write(50);

}
void loop() 
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  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) == "9A 13 CE 58") //change here the UID of the card/cards that you want to give access
  {
    if(flag%2 == 1)
    {
      servo.write(130);
      flag++;
      delay(1000);

    }
 
  else   
  {
      servo.write(15);
      flag++;
      delay(1000);
    }
  }

  else
  {
    delay(1000);
    servo.write(130);
  }
}

VarSpeedServo has an addition argument in the write() command that controls the speed. I.e. write(angle, speed). The way it's used is documented at GitHub - netlabtoolkit/VarSpeedServo: Arduino library for servos that extends the standard servo.h library with the ability to set speed, and wait for position to complete including a sample program. Give it a try.

Steve

I’ll try this. Thank you!

So, I realized what I was doing wrong. I downloaded the library from a very old post which had the WProgram.h instead of the updated Arduino.h. I deleted that old library and used the updated on from GitHub link (which I should have used in the first place). Here is my updated code to close things out in case anyone else has the same problem.

#include <SPI.h>
#include <MFRC522.h>
#include <VarSpeedServo.h>
VarSpeedServo servo;

int flag = 1;
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();          // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Put your card to the reader...");
  Serial.println();

  servo.attach(3);
  servo.write(50);

}
void loop() 
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  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) == "9A 13 CE 58") //change here the UID of the card/cards that you want to give access
  {
    if(flag%2 == 1)
    {
      servo.write(130,100);
      flag++;
      delay(1000);

    }
 
  else   
  {
      servo.write(15,100);
      flag++;
      delay(1000);
    }
  }

  else
  {
    delay(1000);
    servo.write(130,100);
  }
}