c++ - Arduino 不写入 SD 卡?

标签 c++ arduino sd-card adafruit

问题

我在 Adafruit feather mo 上有一个 Arduino .我正在尝试将数据存储在 adalogger 上.这个想法很简单。我有一个电位计,我希望将数据从该电位计写入 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 上的类似问题没有回答我的问题。

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

最佳答案

尝试更改代码,以便只有在 WriteEnable 标志从 false 变为 true 时才会调用 open。为千钧一发做相反的事情。

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

关于c++ - Arduino 不写入 SD 卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106949/

相关文章:

c++ - 使用指向文件的指针从 vector 中保存对象

c++ - C++ 中的矩阵乘法给出运行时错误

c++ - 通过 C++ Serial 与 Arduino 对话

node.js - 在 Arduino Yun 上安装 Node

android - 如何在 Android 中扫描 SD 卡以查找文件的特定扩展名?

c++ - 如何添加到对象数组指针C++

c# - 通过C#从Arduino读取串行数据

sqlite - 从应用程序访问大量数据

android - 在android中检测Sdcard?

c++ - C++ 中的数组初始化(非字符串)