gpt4 book ai didi

c++ - 在 Arduino 中使用 SD.remove() 和 SD 库

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:00 29 4
gpt4 key购买 nike

我正在构建一个程序,该程序对 pin0 上的模拟电压进行 10 次测量,并将其打印到日志文件中。当我尝试确保文件为空时,我遇到了这个问题。我正在使用 SD.remove() 来删除以前的日志文件。当我这样做时,实际上从未写入新的日志文件。当我删除对 SD.remove() 的调用时,程序可以正常工作。这是 SD 库中的一些已知错误还是有一些偷偷摸摸的方法来解决这个问题?

代码如下。

#include <SD.h>
#define OUTPUT_PIN 9 //Using SparkFun MP3 shield
#define DEFAULT_OUTPUT 10
#define VOLTAGE_REF (5)

//Reads a voltage on pin0. by default, the reference voltage is set to 5 V, but
//it can be changed by changing VOLTAGE_REF.

void setup() {
Serial.begin(9600);
Serial.println("Program Initialized");

pinMode(DEFAULT_OUTPUT ,OUTPUT); //Needs to be on to use the library
pinMode(0, INPUT);

if (!SD.begin(OUTPUT_PIN)) {
//init error
Serial.println("Error initializing SD card. Reset the Arduino and try again");
return;
}

Serial.println("Card sucessfully initialized");

if (SD.exists("LOGFILE.LOG") {
SD.remove("LOGFILE.LOG"); //We don't want to use the same file <<THIS IS THE BUG?
}

delay(10); //Make sure changes are applied

File logFile = SD.open("ANALOG.LOG", FILE_WRITE); //Create a new one every time
if (!SD.exists("LOGFILE.LOG")) {
Serial.println("There was some error making a new instance of the logfile?");
delay(1000);
SD.open("ANALOG.LOG", FILE_WRITE);
}
int i;

if (logFile) {
for(i=0;i<10;i++) {
int j = 0;
char str[64];

Serial.print("Reading analog sensor value");
for(j=0;j<=i;j++) {
Serial.print(".");
}
Serial.println();

logFile.print("Read #");
logFile.print(i+1);
logFile.print(" : ");
logFile.print(doVoltageRead(0));
unsigned char l = logFile.println(" V");
if (!l)
Serial.println("No data written");
delay(500);
}
Serial.println("Done.");
logFile.close(); //Close the logfile
Serial.println("Data sucessfully written");
}
else {
//Couldn't create file
Serial.println("There was an error creating the logfile");
}
}

void loop() {
//We don't really need to do anything here
}

float doVoltageRead(int pin) {
int voltageRead = analogRead(pin);
double divisor = (voltageRead * 0.00097752);
float finalVoltage =(VOLTAGE_REF * divisor);
Serial.println(finalVoltage);
return finalVoltage;
}

最佳答案

所以.. 首先你看看LOGFILE.LOG是否存在,如果存在你删除ANALOG.LOG。然后创建 ANALOG.LOG。之后检查 LOGFILE.LOG 是否存在。如果没有,则打印错误并打开 ANALOG.LOG。

LOGFILE.LOG 的具体用途是什么?您不应该将 LOGFILE 更改为 ANALOG 吗?

关于c++ - 在 Arduino 中使用 SD.remove() 和 SD 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13113113/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com