arduino not writing to sd card

2.5k views Asked by At

I have an Arduino with a Seeedstudio sd card shield v4.0 with a prototpye shield above that, and on that is a TMP36 temperature sensor and a red and two green LEDs, the red to show that it is "Ready" to log data, the first green to show that it is currently "logging data" and the last LED to show that the data was "Saved" to the SD card, which it dosent, at the beggining of the file, however, it creates the line "Testing 1, 2, 3..." in a txt file called TEST. in that same file there should be the data, but there is no data, it will write to the card in setup, but not in loop. Can anyone help me?

Code:

#include <toneAC.h>
#include <SPI.h>
#include <SD.h>
int readyLED = 2;
int startLED = 8;
int buzzer = 7;
int tempSensor = A0;
int readyButton = 5;
int sampleNo = 0;
int button_mode = 1;
int saveLED = 4;
File myFile;
void setup() {
  // put your setup code here, to run once:
 pinMode(readyLED, OUTPUT);
 digitalWrite(readyLED, HIGH);
 pinMode(saveLED, OUTPUT);
 digitalWrite(saveLED, LOW);
  pinMode(startLED, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(tempSensor, INPUT);
  pinMode(readyButton, INPUT);
  digitalWrite(readyLED, HIGH);
  digitalWrite(startLED, LOW);
  Serial.begin(9600);
  while (!Serial){}
    Serial.println("Initializing SD card...");
  if(!SD.begin(4)){
      Serial.println("Failed!");
      return;
    }
  Serial.println("Success!");
    myFile = SD.open("test.txt", FILE_WRITE);
     if (myFile) {
    Serial.println("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    delay(500);
    myFile.close();
    Serial.println("done.");
      } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  } 


void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(readyLED, HIGH);
   digitalWrite(startLED, LOW);
    delay(700);
    digitalWrite(startLED, HIGH);
    delay(650);
    int reading = analogRead(tempSensor);  
    float voltage = reading * 5.0;
    voltage /= 1024.0; 
    float temperatureC = (voltage - 0.5) * 100;
    float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
    Serial.print("Sample No. ");
    sampleNo = sampleNo + 1;
    Serial.print(sampleNo);
    Serial.print(" Temperature: ");
    Serial.print(temperatureF);
    Serial.println(" F");
    myFile = SD.open("test.txt", FILE_WRITE);
    if(myFile){
      Serial.println("Test.txt");
      }
      while(myFile.available()){
        myFile.print("Sample No. ");
        myFile.print(sampleNo);
        myFile.print(" Temperature: ");
        myFile.print(temperatureF);
        myFile.println(" F");       
      }
      delay(30);
      digitalWrite(saveLED, HIGH);
      delay(10);
      digitalWrite(saveLED, LOW);
      delay(10);
      myFile.close();
}
2

There are 2 answers

0
MoralCode On

You may want to check to make sure your while loop is actually being run. Since you know you can write to the SD card from void setup() you know your code inside the while loop works, but is the while loop actually being run, or is it evaluating to false and being skipped?

0
BugGuyBCS On

Have you considered the time it takes to write down data as an issue? You may be asking for it write down data before the Arduino code has time to process.