arduino not writing to sd card

蓝咒 提交于 2019-12-12 04:19:55

问题


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();
}

回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/35121679/arduino-not-writing-to-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!