I'm quite lost.
This code that is calling testSensor function every 5 secs it crashes the arduino.
The function itself seems correct, what is it the problem? The code compiled is close to 14 K
#include <MemoryFree.h>
#include <SD.h>
#include <TimeAlarms.h>
#include <Time.h>
#include <SPI.h>
String repeatString = "x";
String dataString;
int sensorPin = A0;
String filenameLog = "";
const int chipSelect = 4;
File myFile;
void setup() {
Serial.begin(9600);
// From http://arduino.cc/en/Tutorial/ReadWrite
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
/*
pinMode(10, OUTPUT);
// root.close();
// file.close();
digitalWrite(10,HIGH);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// 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("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
*/
syncDateTime();
Alarm.timerRepeat(1, printTimer); // timer for every 5 seconds
Alarm.timerRepeat(5, testSensor); // timer for every 5 seconds
// Alarm.timerRepeat(2, setActuators); // timer for every 2 seconds
// Alarm.timerRepeat(2, readSensors); // timer for every 5 seconds
// Alarm.timerRepeat(20, sendDataHub); // timer for every 20 seconds
// Alarm.alarmRepeat(8,30,0, syncDateTime); // 8:30am every day
// Alarm.alarmRepeat(20,30,0, syncDateTime); // 8:30am every day
}
void testSensor() {
String valueSD;
String sensorValue;
sensorValue = "";
valueSD = "";
// sensorValue = (String) analogRead(sensorPin);
sensorValue = "ciaooo";
valueSD += "123456"; // DateTime
// valueSD += setString(getTime(), 6); // DateTime
valueSD += "1234567890"; // id sensor
valueSD += sensorValue; // Value sensor
valueSD += "1234567890"; // Future usage
}
void readSensors() {
String valueSD = "";
String sensorValue = "";
// sensorValue = (String) analogRead(sensorPin);
sensorValue = "ciaooo";
// valueSD += "123456"; // DateTime
// valueSD += setString(getTime(), 6); // DateTime
valueSD += "1234567890"; // id sensor
valueSD += sensorValue; // Value sensor
valueSD += "1234567890"; // Future usage
// valueSD += setString("100 ", 14); // id sensor
// valueSD += setString(sensorValue, 20); // Value sensor
// valueSD += setString("100 ", 10); // Future usage
// writeSD((String) valueSD);
}
void dumpFile (String filename) {
}
void writeSD(String dataString) {
Serial.println(dataString);
dumpFile("D.TXT");
}
void loop() {
Alarm.delay(100);
}
void lsMemoryCard() {
}
void formatMemoryCard() {
}
String getFilename() {
String tmpValue = "";
tmpValue = getDigits(month());
tmpValue += getDigits(day());
tmpValue += getDigits(hour());
tmpValue += getDigits(minute());
tmpValue += ".";
if (second() < 30) {
tmpValue += "000";
} else {
tmpValue += "001";
}
return tmpValue;
}
void printTimer() {
String valueSD = "";
Serial.println(getTime());
Serial.print("freeMemory()=");
Serial.println(freeMemory());
// valueSD += setString(getTime(), 6); // DateTime
// valueSD += setString(getTime(), 6); // id sensor
valueSD += getTime(); // id sensor
// valueSD += repeat("H", 10);
// valueSD += "aaa"; // Value sensor
// valueSD += "1234567890"; // Future usage
Serial.println(valueSD); // DateTime
}
void setActuators() {
}
void sendDataHub() {
}
String readFile(String fileSend) {
String tmpString = "";
return tmpString;
}
void deleteFile(String fileDelete) {
// Delete the file
}
boolean sendData(String valueRecords){
// Should be sent the data and the CRC of the data
}
void sendRecords(String fileSend) {
String dataFile = readFile(fileSend); // // Read the file content
if (sendData(dataFile) == true) {
deleteFile(fileSend);
}
}
String listFileSend() {
// Get the file that shold be read and send to the hub
}
String setString(String Value, int length) {
String tmpValue = "";
tmpValue = Value + repeat(repeatString, length - Value.length() );
return tmpValue;
}
String repeat(String value, int nValues) {
String tmpValue = "";
int iCount = 0;
for (iCount = 0; iCount < nValues; iCount++) {
tmpValue += value;
}
return tmpValue;
}
String getTime() {
String tmpValue = "";
tmpValue = getDigits(hour());
tmpValue += getDigits(minute());
tmpValue += getDigits(second());
return tmpValue;
}
String getDigits(int digits)
{
if(digits < 10) {
return "0" + ( (String) digits);
} else {
return (String) digits;
}
}
void syncDateTime() {
setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
}
void printDirectory() {
}