0

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();
}
Mike44449
  • 1
  • 2
  • 3
  • try taking all other shields off to see if its just conflicting information on the same pin... – MoralCode Feb 01 '16 at 01:24
  • I tried that and wrote a sample sketch that wrote "x = " and then the int x, rising up by 1 each time the loop repeated, it didnt write the text in the loop, i still had it print "test" to the SD in the setup and that worked, but the "x = " did not. – Mike44449 Feb 01 '16 at 01:45
  • were you ***setting*** the contents of the file to X= or ***appending*** it, theres a huge difference. – MoralCode Feb 01 '16 at 01:50
  • Ok, i dont mean to sound stupid, but what are those two things? – Mike44449 Feb 01 '16 at 01:52
  • setting is basically saying "x equals y" and erases all previous contents of X. appending is basically saying "tack z onto the end of x" (x now equals "yz"). This only works with strings. – MoralCode Feb 01 '16 at 01:55
  • So, how would i implement this, make all text into strings? – Mike44449 Feb 01 '16 at 01:56
  • string is the technical word for text. this text is a string. its the same thing – MoralCode Feb 01 '16 at 01:57
  • So how do i get the text to show up? – Mike44449 Feb 01 '16 at 02:01
  • Take of all shields from your arduino that are not the shield with the SD card slot and try making a new program with the code from https://www.arduino.cc/en/Tutorial/ReadWrite – MoralCode Feb 01 '16 at 02:04
  • I tried this and it worked, until i tried to write to the SD in the loop, it only writes whats in setup, and thats my problem – Mike44449 Feb 01 '16 at 02:24
  • try adding a delay after every time you try to write to the SD card – MoralCode Feb 01 '16 at 02:25
  • I added one before the file writing and then one right after the file closed, it didnt work – Mike44449 Feb 01 '16 at 02:28
  • are you sure the while loop is actually running? add some console log message in and see – MoralCode Feb 01 '16 at 02:29
  • 1
    ok, so in the couple lines of code where while the file is avaliable, it will save, and i put a Serial.print in there to see if the file was avaliable, and its now, im going to try and take it out of a while statement – Mike44449 Feb 01 '16 at 02:33
  • took it out of a while loop and now it works, the data is in the file! thanks. – Mike44449 Feb 01 '16 at 02:35
  • no problem! should I add that as an answer? – MoralCode Feb 01 '16 at 02:36

2 Answers2

0

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?

MoralCode
  • 1,954
  • 1
  • 20
  • 42
0

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574