如何使用Arduino创建变量文件名并在sd卡上存储变量
Arduino IDE中的示例“SD_测试”非常有效。现在我想扩展它 首先:我想使用变量文件名。我在互联网和stackoverlow中找到了一些例子,但没有任何效果(仍在搜索一个最小的例子) 我想要如何使用Arduino创建变量文件名并在sd卡上存储变量,arduino,android-sdcard,Arduino,Android Sdcard,Arduino IDE中的示例“SD_测试”非常有效。现在我想扩展它 首先:我想使用变量文件名。我在互联网和stackoverlow中找到了一些例子,但没有任何效果(仍在搜索一个最小的例子) 我想要 writeFile(SD, filename, "Hello "); 其中filename是一个处理类似“file.txt”的变量 Second:我想对要保存在此文件中的内容执行相同的操作。所以不是 writeFile(SD, "/hello.txt", "Hello "); 我想要 write
writeFile(SD, filename, "Hello ");
其中filename是一个处理类似“file.txt”的变量
Second:我想对要保存在此文件中的内容执行相同的操作。所以不是
writeFile(SD, "/hello.txt", "Hello ");
我想要
writeFile(SD, "/hello.txt", datas);
举个例子:我可以把这个打印出来
printf("%04x", datas);
现在,我想在文件中用4个十六进制来保存这个变量“datas”,它在串行监视器中的外观正好如此。您可以使用以下代码在SD卡上写入。 您需要下载两个库文件,因为代码中提到了此名称。 记住先将SD卡格式化为FAT32 希望这对你有帮助
/* Author: Ashish Kumar
Org: INVOOTECH */
#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
File myFile;
char *b="hellow.txt";
char p;
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
if (!card.init(SPI_HALF_SPEED, chipSelect)) //error statement
{
Serial.println("initialization failed. Things to check:");
return;
}
if (!volume.init(card)) //error statement
{
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
}
void loop() {
myFile = SD.open(b);
if (myFile) {
p=myFile.read();
Serial.write(p);
}
// close the file:
myFile.close();
}
/*作者:Ashish Kumar
组织:INVOOTECH*/
#包括
#包括
Sd2Card卡;
SDV体积;
文件myFile;
char*b=“hellow.txt”;
charp;
常数int chipSelect=4;
无效设置(){
Serial.begin(9600);
if(!card.init(SPI_半_速度,chipSelect))//错误语句
{
Serial.println(“初始化失败。要检查的内容:”);
返回;
}
if(!volume.init(card))//错误语句
{
Serial.println(“找不到FAT16/FAT32分区。\n请确保已格式化卡”);
返回;
}
如果(!SD.begin(4)){
Serial.println(“初始化失败!”);
返回;
}
}
void循环(){
myFile=SD.open(b);
如果(我的文件){
p=myFile.read();
串行写入(p);
}
//关闭文件:
myFile.close();
}
好的。所以我所做的就是在循环中创建一个文件
#include <SPI.h>
#include <SD.h>
File myFile;
int chipSelectNumber = 10;
String prefix = "test";
String extension = ".txt";
String filename;
int fileNumber=0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
initSDCard();
}
void loop() {
createFileAndEnterData();
}
//This Function will create file and Enter modbus data in the file
void createFileAndEnterData(){
String temp;
temp = prefix;
temp.concat(fileNumber);
temp.concat(extension);
filename = temp;
Serial.println(filename);
fileNumber++;
boolean statusFileCreate = createFile(filename);
if(statusFileCreate){
Serial.println("File Creation Successful");
}
else{
Serial.println("File Creation Unsuccessful");
}
delay(1000);
}
boolean createFile(String fileName){
myFile = SD.open(fileName, FILE_WRITE);
if(myFile){
Serial.print("Creating file : ");
Serial.print(fileName);
Serial.print(" ");
myFile.println("");
myFile.close();
return true;
}
else{
return false;
}
}
void initSDCard(){
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelectNumber)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void openFile(String fileName){
myFile = SD.open(fileName, FILE_WRITE);
if (myFile) {
Serial.println("Opened File");
} else {
Serial.println("error opening test.txt");
}
}
void deleteFile(String filename){
}
void writeFile(String fileName){
// re-open the file for reading:
myFile = SD.open(fileName);
if (myFile) {
Serial.println(fileName);
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
希望这能解决你的问题。
谢谢。错误:Sd2Card卡未命名类型/SdVolume卷未命名类型您能否与arduino uno共享sd卡模块的连接,以便我可以更好地帮助您。
#include <SPI.h>
#include <SD.h>
File myFile;
int chipSelectNumber = 10;
String prefix = "test";
String extension = ".txt";
String filename;
int fileNumber=0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
initSDCard();
}
void loop() {
createFileAndEnterData();
}
//This Function will create file and Enter modbus data in the file
void createFileAndEnterData(){
String temp;
temp = prefix;
temp.concat(fileNumber);
temp.concat(extension);
filename = temp;
Serial.println(filename);
fileNumber++;
boolean statusFileCreate = createFile(filename);
if(statusFileCreate){
Serial.println("File Creation Successful");
}
else{
Serial.println("File Creation Unsuccessful");
}
delay(1000);
}
boolean createFile(String fileName){
myFile = SD.open(fileName, FILE_WRITE);
if(myFile){
Serial.print("Creating file : ");
Serial.print(fileName);
Serial.print(" ");
myFile.println("");
myFile.close();
return true;
}
else{
return false;
}
}
void initSDCard(){
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelectNumber)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void openFile(String fileName){
myFile = SD.open(fileName, FILE_WRITE);
if (myFile) {
Serial.println("Opened File");
} else {
Serial.println("error opening test.txt");
}
}
void deleteFile(String filename){
}
void writeFile(String fileName){
// re-open the file for reading:
myFile = SD.open(fileName);
if (myFile) {
Serial.println(fileName);
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void createFileAndEnterData(){
String temp;
temp = prefix;
temp.concat(fileNumber);
temp.concat(extension);
filename = temp;
Serial.println(filename);
fileNumber++;
boolean statusFileCreate = createFile(filename);
if(statusFileCreate){
Serial.println("File Creation Successful");
}
else{
Serial.println("File Creation Unsuccessful");
}
delay(1000);
}