On a 328 based Arduino, there went 1/4 of your memory.
}while(!(data[x-1]=='T'&&data[x-2]=='S')); //waits for response "REQUEST"
This will terminate if the serial data contains RUST or PEST or BEST or any number of other words that end in ST. Not a very robust test. The strncmp() function would allow you to specifically test for REQUEST at the end of the string.
Also, there is nothing to stop this code from writing to data, data, data, etc.
You just made a copy of the data in the array that could use up another 1/4 of the memory. Why is it necessary to copy the data? By the way, data is not the best choice for a variable name.
Serial.println("AT+CCAMS"); //starts the camera
Block until the Arduino is reset if the phone does not respond with OK. Is that really what you want?
I’d create a function that read and printed the phone reply, and returned true or false, based on whether the letters OK arrived one after the other. The function should time out after some reasonable (variable?) period of time.
Of course, to do this, the phone really needs to be connected to something other than the hardware serial port. SoftwareSerial could be used to read the phone data, so that the hardware serial port could be used to debug the program.
String file_stringa = String(name);
This calls the String constructor to make a String object containing the data in the name variable. Then, the copy constructor is called to copy that object’s data to another object (also constructed) named file_stringa.
That’s three constructor calls, when only one is necessary: Two destructor calls are also made.
One eighth of the memory on a 328.
I’m 99.999% certain this call is NOT needed, anywhere it is made. Throwing away the phone response is not a good idea (pre 1.0 behavior). Blocking until the pending serial data has all been send is not generally necessary (post 1.0 behavior).
20% of the 328’s memory.
With all the constant strings in the Serial.print() statements, you are almost certainly running out of memory.
The F() macro can help.
and your SRAM utilization drops.
You need to carefully consider the sizes of the arrays named data in the various functions. One global array, with a more reasonable size, is going to cut your memory utilization considerably. Dump the String class altogether (or, at least the chiamata instance). The first instance that copies the data in data just so you can use substring() to get id is hugely wasteful.