Arduino不写入SD卡?

Arduino doesn't write to SD card?

本文关键字:SD Arduino      更新时间:2023-10-16

>问题

我在Adafruit羽毛上有一个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();
}
}

引用

我试图通过咨询几种资源来解决问题。堆栈溢出上的类似问题并不能回答我的问题。

  • Arduino SD 文档
  • Arduino 未写入 SD 卡
  • SD 库 API 文档
  • 尝试了Stringsprint.我应该尝试flush吗?但这与 SD 库的文档不匹配。令人 沮丧。
  • 更新:刚刚尝试在println()命令后使用sensorData.flush()。没用。

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

尝试更改代码,以便仅在 WriteEnable 标志从 false 更改为 true 时发生打开调用。对关闭呼叫执行相反的操作。

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