arduino不写sd卡

时间:2016-02-01 01:19:26

标签: sd-card arduino-uno write-error

我有一个带有Seeedstudio sd卡屏蔽v4.0的Arduino,上面有一个prototpye屏蔽,上面是一个TMP36温度传感器和一个红色和两个绿色LED,红色表示它是"准备"记录数据,第一个绿色显示它当前"记录数据"最后一个LED显示数据是"已保存"然而,它在文件的开始处对SD卡进行了处理,它创建了一条线"测试1,2,3 ......"在名为TEST的txt文件中。在同一个文件中应该有数据,但是没有数据,它会在设置中写入卡,但不会在循环中写入。任何人都可以帮助我吗?

代码:

#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 个答案:

答案 0 :(得分:0)

您可能需要检查以确保您的while循环实际上正在运行。既然您知道可以从void setup()写入SD卡,那么您知道while循环中的代码是否有效,但实际上是在运行while循环,还是正在评估false并被跳过?< / p>

答案 1 :(得分:0)

您是否考虑过将数据写下来所花费的时间?您可能会要求它在Arduino代码有时间处理之前写下数据。