数据未保存在Arduino SD中
我创建了一个代码,该代码获取温度测量数据并将其保存在变量中。现在,我想将数据保存在连接到Arduino的SD卡中的.txt文件中。 SD的初始化有效,但是当它设法访问文件时,会给我错误,给我写一组奇怪的符号,而不是文件名,并且不会在文件中写入。我尝试了一个非常相似的代码来在SD内部编写,并且效果很好。可能是什么问题?我将写作代码的一部分封闭在SD上,并将照片与输出一起。 初始化部分:
Serial.println(F("Intializing SD card"));
if(!SD.begin(4))
{
Serial.print(F("Initialization failed"));
while(!SD.begin(4))
{
Serial.print(F("."));
delay(1000);
}
}
else Serial.println(F("Initialization done"));
写作部分:
sdFile = SD.open("records.txt",FILE_WRITE);
Serial.print("Writing to ");
Serial.println(sdFile.name());
sdFile.print(" Temperature: ");
sdFile.print(tempC);
sdFile.print(" taken at: ");
sdFile.print(hour());
sdFile.print(":");
sdFile.print(minute());
sdFile.print(":");
sdFile.println(second());
sdFile.close();
I have created a code that takes temperature measurement data and saves it in a variable. Now I would like to save the data in a .txt file inside an SD card connected to Arduino. The initialization of the SD works, but when it manages to access the file it gives me errors, writing me a set of strange symbols instead of the file name and it doesn't write inside the file. I tried a very similar code to write inside the SD and it works fine. What problem can it be? I enclose the part of the writing code on the SD and the photo with the output.
Initialization part:
Serial.println(F("Intializing SD card"));
if(!SD.begin(4))
{
Serial.print(F("Initialization failed"));
while(!SD.begin(4))
{
Serial.print(F("."));
delay(1000);
}
}
else Serial.println(F("Initialization done"));
Writing part:
sdFile = SD.open("records.txt",FILE_WRITE);
Serial.print("Writing to ");
Serial.println(sdFile.name());
sdFile.print(" Temperature: ");
sdFile.print(tempC);
sdFile.print(" taken at: ");
sdFile.print(hour());
sdFile.print(":");
sdFile.print(minute());
sdFile.print(":");
sdFile.println(second());
sdFile.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您是否正在打开SD卡,然后才有机会初始化,解决此问题,在开始时实现检查:
这是否可以解决您的问题?另外,请注意尺寸限制,以了解Arduino可以读取SD卡的大小。对于全尺寸卡来说,这通常是2GB,而“微型”卡的16GB。
可以在Arduino网站上找到其他文档,以及示例在这里。
I suspect you are opening the SD card before it has a chance to initialise, to fix this, implement a check at the start like so:
Does this fix your issue? Also, watch out for size limitations on how large the Arduino can read SD cards. This is usually 2GB for full-size cards, and 16GB for "micro" cards.
Additional documentation can be found on the arduino website, as well as examples here.