Problem with SD Card in UNO+GSM SHIELD 900+PIR+SD CARD

Hello,

I use an Arduino UNO with a GSM Shield SIM900 and attached a PIR sensor and an SD CARD to store the data.
I have 3 problems:

  1. All works OK but I can’t store in SD card. I get the “ERROR can’t open file” that is in the code.
  2. Also, I would like to know if there is a command to get directly from GSM SHIELD the date/time.
  3. I am using a part of code in order to prevent sending SMS too soon (I use a 60sec timer and a message “Too soon”). If PIR is triggered too soon I get multiple messages “Too soon” like being triggered many times for about 2 seconds continuously. How can I make it show/trigger only once (only when the PIR sensor is triggered).

I have this code:

/*-------------SD CARD---------------------*/
#include <SPI.h>
#include <SD.h>
/*-------------GSM---------------------*/
#include <GSM.h>

//Define file for SD CARD
File myFile;

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;

// char array of the telephone number to send SMS
// change the number 1-212-555-1212 to a number
// you have access to
char remoteNumber[11]= "1-212-555-1212";  

// char array of the message
char txtMsg[100]="Love You *ARDUINO*";

/*-------------PIR ---------------------*/
int sensor = 6;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected

/*------------- TIMER for minimum time ---------------------*/
long lastMessage = -60 *1000l;

void setup() {
  // initialize serial communications
  Serial.begin(9600);

  /*-------------SD CARD SETUP---------------------*/
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  /*-------------GSM SETUP---------------------*/
  Serial.println("SMS Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin("0000")==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(500);
    }
  }
  Serial.println("GSM initialized");
  /*-------------PIR SETUP ---------------------*/
  
  pinMode(sensor, INPUT);    // initialize sensor as an input
}


void loop() {
  long now = millis(); // Get time passed
  
  if (digitalRead(sensor) == HIGH) {          // check if the sensor is HIGH
    if(now >(lastMessage + 60 *1000l)){ // If not too soon
      lastMessage = now; //set new time

      // Write to SD CARD

      // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
      myFile = SD.open("pirRecords.txt", FILE_WRITE);

      // if the file opened okay, write to it:
      if (myFile) {
        Serial.print("Writing to file...");
        myFile.println(now);
        // close the file:
        myFile.close();
        Serial.println("Writing done.");
      } else {
        // if the file didn't open, print an error:
        Serial.println("ERROR opening text file");
      }
  
      if (state == LOW) {
        Serial.println("Motion detected!");
        state = HIGH;       // update variable state to HIGH
        delay(20);
        sendSMS();          /*------------- SEND THE SMS --------------*/
        Serial.println("Send SMS");
      }
    }else{
      Serial.println("Too soon...");
    }
  }
  else {
    if (state == HIGH) {
      Serial.println("Motion stopped!");
      state = LOW;       // update variable state to LOW
    }
  }
}

void sendSMS(){

  Serial.print("Message to mobile number: ");
  Serial.println(remoteNumber);

  // sms text
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");  
}

I hope comments are helpful.
All code I met so far for SD Card is all written in setup() section. But I need to call it from loop(). Could that be the problem? If yes how can I solve this?

Please give some info.

Somewhere I read that because there is no much space for variables left (I think 89% occupied) that could cause a problem. Is that true?

Is that true?

It is true that running out of memory can cause problems. You can check whether that is the case FOR YOU: http://playground.arduino.cc/Code/AvailableMemory

char remoteNumber[11]= "1-212-555-1212";

I can't believe the compiler allows you to try to stuff 14 characters plus a terminating NULL in an 11 element array.

      myFile = SD.open("pirRecords.txt", FILE_WRITE);

The SD class supports 8.3 format names. You REALLY need to learn to count.

PaulS: char remoteNumber[11]= "1-212-555-1212";

I can't believe the compiler allows you to try to stuff 14 characters plus a terminating NULL in an 11 element array.

:-) My number has 10 chars.

PaulS:       myFile = SD.open("pirRecords.txt", FILE_WRITE);

The SD class supports 8.3 format names. You REALLY need to learn to count.

Didn't know that. Even if I had read that, I wouldn't understand what is 8.3 format names. Maybe that is the reason. Very helpful, thanks!