How do I run two loop at same time to scan RFID and GPS Coordinates

I have build a real time vehicle tracking system in which I want to scan continuously gps location using sim808 shield module which send data to mysql database where I will fetch for futher operation and if any driver scan his RFID card at RC522 Module I need to pause gps tracking and tcp connection of data sending and give highest priority to rfid scanner but when card is not present it start gps coordinate and send over the server if I place a card to scan it will not scan the card till tcp connection shut that may lead error to scan card and send to card detail to server.

How do I solve this issue with arduino Nano + RC522 +GSM/GPS sim 808 module. I want to give highest priority to rfid scanner as well data logging in background when card is not present but if card reaches the sensor immideatley hold the tcp and gps operation while card process is going on.

Please help I am new in arduino.

Start here

It is the topmost thread in this section of the forum, with a title in bold type; it's remarkable that you didn't see it. Is there any way we could make it more obvious?

Sorry Sir I am unable to understand your reply, Project working fine but there is huge delay to scan RFID card while controller busy in other loop to send data over tcp connection as soon as it completed ,RFID scanned,What I want scanning at highest priority as card present to RC522 module it should stop all operation and scan card first then starts its normal routing.

Please help me to solve this issue.

Project working fine but there is huge delay

Clearly your definition of “working fine” differs from mine.


You didn’t post your code, and we’re not psychic. (It’s kind-of embarrassing that I have to post such an obvious thing, but again, hey! Whatever)

ahbhoyar: Sorry Sir I am unable to understand your reply,

Have you studied the code in the link in Reply #1 ?


please find the code below;
As I am New in arduino so coding is not in professional manner.


ahbhoyar: please find the code below;

Please tell us in detail what the program actually does when you run it and what you want it to do that is different. That way we won't waste time studying parts that don't need changing.


Project have two feature first scan the rfid card and send to web server for identification ,second get gps coordinate continuous for real time tracking of his vehicle and send lat lng to same web server.

Driver can scan his card any time while vehicle running or stop thats mean I want rfid scanner active all time and once the card scanned that detail send to web server database through tcp connection but problem occurs of gps scanner because its continuously running and data sending to tcp web server mode Now when driver scan his card it will not scanned till gps and tcp complete his loop once they complete the card get scanned its very long process to hold the card at reader.

What I want, RFID scanning and gps location sender functions run in parallel way as rfid reader alway active even gps sending data in background mode live service. I have two priority for this project sending gps coordinate continuously as well as scan rfid any moment that card is placed.

all problem occur due to tcp connection once gps is in the function of tcp it take time and not scan RFID.

Can I run both rfid scan and gps acquisition process simultaneously rather tcp connection work in background.

It is probably sufficient to check the GPS position once every 30 seconds rather than every time loop() repeats.

And you have some long delay()s in the gps_scan() function which block the Arduino from doing other things and will mean that it takes a lot longer for the function to complete than is necessary. If you really do need intervals use millis() to make them non-blocking as illustrated in the demo Several Things at a Time

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

I see lots of other delay()s also. They should all be replaced by millis() to let the Arduino do useful stuff.

Your code should be organized so that loop() repeats at least 100 times per second.

And use millis() to create the interval between successive reads of the GPS also.


Thank you so much , I will implement mill() in the code and avoid unnecessary delay from the code But I have one issue that is when controller goes into TCP connection after reading rfid or gps it consume lots of time and struct into the loop so It couldn’t come into main loop for rfid scanner and gps scan.
How do I deal with this TCP connection function.
as two function at main loop like rfid and gps and after reading date third function tcp (webconnection).

I will correct my code as per your guidance and followup you soon.

it consume lots of time and struct into the loop so It couldn't come into main loop for rfid scanner and gps scan.

"lots of time" isn't very helpful.

So How do I deal with TCP webconnection functio, Do I use milli() for same?

  1. rfid scan - which I need in real time (any time driver can scan his card).
  2. gps scan - which I need in real time (scan real time vehicle location).
  3. tcp connection - which I need work in background (when any data came from rfid or gps it has to post on Url).

How to deal with Tcp In background without disturb rfid n gps scanners?

ahbhoyar: So How do I deal with TCP webconnection functio, Do I use milli() for same?

If you can then yes.

Is there any reason to think that millis() can't be used? Data over the internet arrives one byte at a time just like serial data.