0

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

  }

}
  • The "secret" is in the use of the millis() function which allows you to take a snapshot of the current time and compare it to a later time . This is how JamesG referenced 'Blink Without Delay' achieves its result.Note that any copies of the value of millis() must be stored in an "unsigned long" variable and use of eg "long" as in the previousMillis() save in the 'Blink Without Delay' example will cause errors in some cases. – Russell McMahon Jun 3 '15 at 4:07
4

What you want to implement is described in Blink Without Delay. Basically, instead of using the delay function to just wait until your time has elapsed, you use variables to measure how much time has elapsed and only execute your operation when it has. In the mean time, you can check on other things and do those in the mean time.

  • 2
    Good reference. Comment only: Note that any copies of the value of millis() must be stored in an "unsigned long" variable and use of eg "long" as in the previousMillis() save in the 'Blink Without Delay' example will cause errors in some cases. – Russell McMahon Jun 3 '15 at 4:09
0

have a look my example program below for you reference.

/* 
 * Author BALASAIDULU.N 
 * function -1 : Fading led with pwm pin 9
 * fucntion -2 : Blinking LED on pin 13
 * connect an LED on pin 9 with a 330 ohms resistor   
 */
     int leda=9;
     int ledd=13;
int i,g,s1,s2;
int switchPin=8;

void setup() {
pinMode(ledd,OUTPUT);
pinMode(switchPin,INPUT);
}

void fading()
{
  st:
  if(s1==0){
if(s1==1){goto hy;}
analogWrite(leda,i);
delay(30);
i+=5;
if(i>255){s1=1;}
goto st1;
  }
hy:

if(i<0){s1=0;goto st;}
i-=5;
analogWrite(leda,i);
delay(30);
 st1:
delay(10);
}


void blinking()
{ 
digitalWrite(ledd,HIGH);
delay(30);
digitalWrite(ledd,LOW);
delay(30);

}
void loop() {

int g;
g=digitalRead(switchPin);
if(g==1) {delay(50);}
if(g==0){
blinking();
fading();}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.