问题
我在Adafruit 羽毛 mo上有一个 Arduino 。我正在尝试将数据存储在adalogger上。这个想法很简单。我有一个电位器,我希望将该电位器中的数据写入 SD 卡。我可以创建、打开和关闭 CSV 文件,但无法将电位器中的数据写入 SD 卡上的 CSV 文件。
很少,我可以从传感器获取数据以写入 CSV 文件。但是,我不相信这是一个硬件问题,因为我可以看到 COM 中的所有数据,Serial.println()
就像我想要的电位器一样。它只是不会写入 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 上的类似问题无法回答我的问题。
- Arduino SD 文档
- Arduino不写入SD卡
- SD 库 API 文档
- 试过
String
和sprint
。我应该尝试flush
吗?但这与 SD 库的文档不匹配。令人沮丧。 - 更新:刚刚尝试使用
sensorData.flush()
afterprintln()
命令。没用。
String()
我已经通过使用和sprint
帮助定义数据更改了对 SD 卡的写入。没有任何工作。我认为这不是硬件问题,因为我可以打开和关闭文件,但不能写入任何数据。有人可以帮我吗?如果有人怀疑硬件,请告诉我原因。我真的很怀疑,因为为什么我可以在 SD 卡上创建一个文件并在 COM 中查看电位器值但不能存储它们?