So I am doing a project where a bunch of servos are moving from 90 - 120 degrees and slowly speed up over time until they reach their max speed and so stay at that speed. This is being done by editing the delay under the servo sweep for loop. However though this works it doesn't work with the next component.
WHat I want to happen next is an MFRC522 RFID reader to always be scanning and at any time it detects a card and verifies it the servos from whatever speed they are now at start to slow down. The problem is I can't get the two functions to run simultaneously. IF the servo is running its sweep function it must finish before the RFID tag can scan. This makes the whole device look very lagy. Is there a way I can get the RFID scanner to be reading all the time even when the servo sweep for loop is working?
Here is the code: Please note that I have put printins everywhere to see how often the RFID is scanning etc just while I test the code out. I essentially have two functions one called nocardservo and yescardservo each one activating if there is a card there or not. Any help would be fantastic!!
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define RST_PIN 9 //
#define SS_PIN 10 //
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
Servo myservo;
int pos = 0;
int y = 15;
int z = y;
void setup() {
myservo.attach(6);
myservo.writeMicroseconds(1518);
Serial.begin(9600); // Initialize serial communications with the PC
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
//ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
//Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
nocardservo();
return;
}
delay(20);
Serial.println("carddetected");
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
}
Serial.println("correctcard");
yescardservo();
delay(20);
return;
}
//***If card not detected this function runs**//
void nocardservo() {
for (pos = 90; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 90; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
}
}
//***If card is detected this function runs**//
void yescardservo() {
for (pos = 90; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 90; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50);
}
}