Hi,
Please have a look on the following code. It is a copy of example code but with FreeRTOS. This program fails to open the SD card file. However, it works when used as it is from example, also If the writing and reading a file from SD card code put into loop.
Please let me know if you have any idea to get it work. I have tried a lot.
I am using Arduino mega. The hardware setup us perfect as example code is working perfectly. Just my code is not able to open a file.
"myFile = SD.open("test.txt", FILE_WRITE);" this line has "0" as output always. I have not idea how to solve this.
//#include <stdio.h>
//#include <stdint.h>
//#include <ctype.h>
#include <Arduino_FreeRTOS.h>
#include <SPI.h>
#include <SD.h>
void TaskSDCard (void *pvParameters);
#ifndef TRUE
#define TRUE (1==1)
#define FALSE (!TRUE)
#endif
String dataString = "Prashant,Borhade";
const int chipSelect = 53;
File myFile;
TaskHandle_t xTaskSDCard;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println(F("Initializing tasks"));
xTaskCreate(
TaskSDCard
, (const portCHAR *) "SD"
, 256 // This stack size can be checked & adjusted by reading Highwater
, NULL
, 1 // priority
, xTaskSDCard );
Serial.println(F("Initializing Done"));
//pinMode(chipSelect, OUTPUT);
//pinMode(53, OUTPUT);
Serial.println(F("Initializing SD card"));
}
void loop() {
}
void TaskSDCard (void *pvParameters){
(void) pvParameters;
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("test.txt")){
SD.remove("test.txt");
Serial.println("Deleted Existing file.");
}
for(;;){
myFile = SD.open("test.txt", FILE_WRITE);
Serial.println(myFile);
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
myFile.close();
Serial.println("done.");
}
else {
myFile.close();
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
vTaskDelay(10000 / portTICK_PERIOD_MS);
}
}