Arduino Uno, RFID and GSM project

Hi guys,

I have some problems with a project i work at the moment, i want to use a series of functions to power modules (sensors, rfid module and gsm module) separately because the gsm module needs more power and the board can't deliver to all at once. The problem is with the rfid function, i want it to run until a new card is detected and it's uid is read, but i can't make it to work, the function looks like this at the moment :

void fRFID()
{
 int control = 0;
 digitalWrite(SENS1_Power, LOW);
 digitalWrite(SENS2_Power, LOW);
 digitalWrite(RFID_Power, HIGH);
 Serial.println("Wait 5s for RFID to boot");
 delay(5000);
 Serial.println("DONE!");
 // Look for new cards
 do
 {
   delay(50);
   if ( mfrc522.PICC_IsNewCardPresent())//If a new PICC placed to RFID reader continue
   {
     delay(50);
     if ( mfrc522.PICC_ReadCardSerial()) //Since a PICC placed get Serial and continue
     {
       Serial.println("New card detected");
       control = 1;
     }
   }
 }
 while (control == 0);
 Serial.print ("RFID TagID is: ");
 dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
 mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
 mfrc522.PICC_HaltA(); // Stop reading
 Serial.println("Done reading!");
 delay(500);
 fGSM();
}

Any help is really welcome!

but i can't make it to work, the function looks like this at the moment :

It would look better in code tags, with the rest of the code.

It does something. You failed to explain what it actually does, or how that differs from what you want.

Hi. Sorry if i didn't explained what i want to do with the code... this function should read the uid of an rfid tag and print it to serial, and when it's done the next function it's called. The setup i have works with the examples from the mfrc522 library but when i run this code, the tags are not detected. Here is all the code i used, maybe you can see something i've missed.

#include <SoftwareSerial.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
MFRC522::MIFARE_Key key;

SoftwareSerial GSM_Serial(15, 16);
int GSM_Power = 14;
int SENS1_Power = 2;
int SENS2_Power = 19;
int RFID_Power = 8;
char msg;
char call;
String number;
int SENS1 = 0;
int SENS2 = 0;
byte readCard[100] ;
char tag;

void setup()
{

  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  SPI.begin();        // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522
  pinMode(GSM_Power, OUTPUT);
  pinMode(SENS1_Power, OUTPUT);
  pinMode(SENS2_Power, OUTPUT);
  pinMode(RFID_Power, OUTPUT);
  GSM_Serial.begin(9600);   // Setting the baud rate of GSM Module
  Serial.println("GSM SIM900A BEGIN");
  Serial.println("Enter character for control option:");
  Serial.println("h : to disconnect a call");
  Serial.println("i : to receive a call");
  Serial.println("s : to send message");
  Serial.println("r : to receive message");
  Serial.println("c : to make a call");
  Serial.println("e : to redial");
  Serial.println("f : to start main program");
  Serial.println();
}

void loop()
{
  if (Serial.available() > 0)
    switch (Serial.read())
    {
      case 's':
        SendMessage();
        break;
      case 'r':
        ReceiveMessage();
        break;
      case 'c':
        MakeCall();
        break;
      case 'h':
        HangupCall();
        break;
      case 'e':
        RedialCall();
        break;
      case 'i':
        ReceiveCall();
        break;
      case 'f':
        fSENS1();
        break;
    }
}

void fSENS1()
{
  digitalWrite(SENS1_Power, HIGH);
  delay(10);
  SENS1 = analogRead(A3);
  delay(5);
  if (SENS1 < 100)
  {
    Serial.println(SENS1);
    fSENS2();
  }
  else
    fSENS1();
}

void fSENS2()
{
  digitalWrite(SENS2_Power, HIGH);
  delay(10);
  SENS2 = (analogRead(A4));
  delay(5);
  if (SENS2 < 100)
  {
    Serial.println(SENS2);
    fRFID();
  }
  else
    fSENS2();
}

int fRFID()
{
  int control = 0;
  digitalWrite(SENS1_Power, LOW);
  digitalWrite(SENS2_Power, LOW);
  digitalWrite(RFID_Power, HIGH);
  Serial.println("Wait 1s for RFID to boot");
  delay(1000);
  Serial.println("DONE!");
  // Look for new cards
  do
  {
    if ( mfrc522.PICC_IsNewCardPresent())//If a new PICC placed to RFID reader continue
    {
      if ( mfrc522.PICC_ReadCardSerial()) //Since a PICC placed get Serial and continue
      {
        Serial.println("New card detected");
        control = 1;
      }
    }
  }
  while (control == 0);
  Serial.print ("RFID TagID is: ");
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  mfrc522.PICC_HaltA(); // Stop reading
  Serial.println("Done reading!");
  delay(500);
  fGSM();
}

void fGSM()
{
  digitalWrite(RFID_Power, LOW);
  digitalWrite(GSM_Power, HIGH);
  delay(10000);
  fGSM();
}

void SendMessage()
{
  GSM_Serial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  GSM_Serial.println("AT+CMGS=\"+40727919188\"\r"); // Replace x with mobile number
  delay(1000);
  GSM_Serial.println("");// The SMS text you want to send
  delay(100);
  GSM_Serial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

void ReceiveMessage()
{
  GSM_Serial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
  delay(1000);
  if (GSM_Serial.available() > 0)
  {
    msg = GSM_Serial.read();
    Serial.print(msg);
  }
}

void MakeCall()
{
  Serial.println("Enter Phone Number: ");
  while (Serial.available())
  {
  }
  delay(3000);
  number = Serial.readString();
  number = "ATD" + number + ";";
  GSM_Serial.println(number); // ATDxxxxxxxxxx; -- watch out here for semicolon at the end!!
  Serial.println("Calling " + number); // print response over serial port
  delay(1000);
}


void HangupCall()
{
  GSM_Serial.println("ATH");
  Serial.println("Hangup Call");
  delay(1000);
}

void ReceiveCall()
{
  GSM_Serial.println("ATA");
  delay(1000);
  {
    call = GSM_Serial.read();
    Serial.print(call);
  }
}

void RedialCall()
{
  GSM_Serial.println("ATDL");
  Serial.println("Redialing");
  delay(1000);
}

int getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
    return 0;
  }
  // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  //Serial.println("Scanned PICC's UID:");
  for (int i = 0; i < mfrc522.uid.size; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
    tag = tag + " " + (readCard[i], HEX);
  }
  Serial.print(tag);
  Serial.println("");
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}

void dump_byte_array(byte * buffer, byte bufferSize)
{
  for (byte i = 0; i < bufferSize; i++)
  {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
  Serial.println();
}

this function should read the uid of an rfid tag and print it to serial

Well, that would have to be done by loop() or something called by loop().

In your loop() function, NOTHING happens if there is no serial data to be read. Why is that?

fSENS1() calls fSENS1(). That is REALLY a bad idea.

fSENS2() calls fSENS2(). That is REALLY a bad idea.