arduino 不写入 SD 卡 [英] arduino not writing to sd card

查看:35
本文介绍了arduino 不写入 SD 卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Seeedstudio sd 卡屏蔽 v4.0 的 Arduino,上面有一个原型屏蔽,上面是一个 TMP36 温度传感器和一个红色和两个绿色 LED,红色表示它准备好"记录数据,第一个绿色表示当前正在记录数据",最后一个 LED 表示数据已保存"到 SD 卡,在文件开始时,它会发送,但是,它创建名为 TEST 的 txt 文件中的测试 1、2、3..."行.在同一个文件中应该有数据,但没有数据,它会在设置时写入卡,但不会在循环中写入.有人可以帮我吗?

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?

代码:

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

推荐答案

你有没有考虑过把数据写下来的时间问题?您可能会要求它在 Arduino 代码有时间处理之前写下数据.

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.

这篇关于arduino 不写入 SD 卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆