Hello all!
I am having a bit of a trouble with my project. I connected a SD module to a small circuit and combined two sketches to get the sketch I needed. The strange thing however is that the arduino only writes data to the SD card when the serial monitor is opened. If the serial monitor is not opened while the programm is running the SD card will remain empty. I will eventually have to disconnect the circuit from the computer and make it as small as possible so that is why this problem really buggs me.
I double checked everything and even got some help from more experienced people at my University but they too were not able to fix my problem. My guess is that there is some kind of bug in the standard library that is included in the sketch (SD.h) , I already tried an updated library (SDFat) but unfortunately that was a bit too complicated for me to comprehend because it used more advanced coding in the Arduino IDE.
The problem is either in something so simple that I somehow do not see it or, more likely, it is just a bug in the library.
I really hope that some of you can help me out! thanks for replying:)
Picture of my set up:
This is my code (the problem also occurs with the default SD sketch):
#include <SD.h>
const int chipSelect = 4;
int inPinSwitch = 3;
int outPinLED = 2;int stateLED = LOW;
int reading;
int previous = HIGH;long time = 0;
long debounce = 200;void setup()
{
pinMode(inPinSwitch, INPUT);
pinMode(outPinLED, OUTPUT);Serial.begin(9600);
while (!Serial) {
;
}Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");return;
}
Serial.println("card initialized.");}
void loop()
{
reading = digitalRead(inPinSwitch);if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (stateLED == HIGH)
{
stateLED = LOW;
}
elsestateLED = HIGH;
time = millis();
};if (stateLED == HIGH)
{String dataString = "";
for (int analogPin = 0; analogPin < 2; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 1) {
dataString += ",";
}
}File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}else {
Serial.println("error opening datalog.txt");
};delay(250);
}digitalWrite(outPinLED, stateLED);
previous = reading;
}