Arduino 不写入 SD 卡?

Arduino doesn't write to SD card?

问题

我在 Adafruit feather mo. I am attempting to store data on an adalogger 上有一个 Arduino。这个想法很简单。我有一个电位计,我希望将数据从该电位计写入 SD 卡。我可以创建、打开和关闭CSV文件,但是我不能将电位器的数据写入SD卡上的CSV文件。

在极少数情况下,我可以从传感器获取数据并写入 CSV 文件。然而,我不相信这是一个硬件问题,因为我可以使用 Serial.println() 查看 COM 中的所有数据,就像我想从电位器中看到的那样。它只是不会写入SD卡。有人可以指出我可能出错的地方吗?

代码

此代码有效:

**Initalize Stuff** 
//Project Dependencies 
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
#include "RTClib.h"

// the setup function runs once when you press reset or power the board
//Global variables, usually associated with physical assets on the board
Sd2Card card;
SdVolume volume;
SdFile root;
char filename[13] = "";
File logfile;
int potPin = 2; //select the input pin for the potentiometer 
const int chipSelect = 10;//chip select for SPID 
File dataFile;
File sensorData;
bool fileOpen = false;

void setup() {
  //Step 1: Initialize Serial
  //================================================================
  Serial.begin(57600);
  //Step 1: Initialize the SD Card
  //================================================================
  //Step 1: Initialize the SD Card
  //================================================================
  Serial.println("Initializing SD card...");
  while (!SD.begin(chipSelect)) {
    // see if the card is present and can be initialized:
    Serial.println("...Card failed, or not present");
    delay(2000);
  }
  Serial.println("...Success.");
  if (card.init(SPI_HALF_SPEED, chipSelect))
    Serial.println("...Card initialized.");
  //// print the type of card
    Serial.print("\n...Card type: ");
    switch (card.type()) {
      case SD_CARD_TYPE_SD1:
        Serial.println("SD1");
        break;
      case SD_CARD_TYPE_SD2:
        Serial.println("SD2");
        break;
      case SD_CARD_TYPE_SDHC:
        Serial.println("SDHC");
        break;
      default:
        Serial.println("Unknown");
    }
    sprintf(filename, "%02d%02d%02d%02d.csv", now.month(), now.day(), now.hour(), now.minute());
    Serial.print("Initializing date file: ");
    Serial.println(filename);
    //Step 2: Create and open new logfile
    if (SD.exists(filename)) {
      Serial.print("...file ");
      Serial.print(filename);
      Serial.println(" already created before...Success.");
    } else {
      //File not found, so create it.
      dataFile = SD.open(filename, FILE_WRITE);
      if (!dataFile) {
        Serial.print("...Couldn't create ");
        Serial.println(filename);
      } else {
        Serial.print("...Success opening ");
        Serial.println(filename);
        Serial.print("...Starting size: ");
        Serial.print(dataFile.size());
        Serial.print(" bytes");
      }
      dataFile.close();
    }
  }

循环

此代码无效:

// the loop function runs over and over again until power down or reset
void loop() {
  int WriteEnabled = digitalRead(5); //This is when I turn a switch on to record, this works
  if (WriteEnabled) {
    sensorData = SD.open(filename, FILE_WRITE);
    if (sensorData) {
      uint32_t value = analogRead(potPin);
      //char line[27] = ""; //Define my input line
      //sprintf(line, "%04d/%02d/%02d, %02d:%02d:%02d, %04d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second(), value); //Doesn't work either, why?
      String dataString = String(value);
      sensorData.println(dataString);//This doesn't work
      Serial.println(dataString);
    }
  }
  if (!WriteEnabled && sensorData) {
    sensorData.close();
  }
}

参考资料

我已尝试通过咨询多种资源来解决问题。 Stack Overflow 上的类似问题没有回答我的问题。

我更改了写入 SD 卡的方式,使用 String()sprint 来帮助定义数据。什么都不起作用。我不认为这是硬件问题,因为我可以打开和关闭文件,但不能写入任何数据。有人可以帮帮我吗?如果有人怀疑硬件,请告诉我原因。我真的很怀疑,因为为什么我可以在 SD 卡上创建一个文件并在 COM 中看到电位器值但不存储它们?

尝试更改代码,使打开调用仅在 WriteEnable 标志从 false 变为 true 时发生。对千钧一发做相反的事情。

为了更直接地匹配 Arduino 参考,将关闭调用移动到写入之后。