SD卡無法寫入資料

U0421011
Posts: 2
Joined: Mon May 27, 2019 5:53 pm

SD卡無法寫入資料

Postby U0421011 » Mon May 27, 2019 6:07 pm

#include <SD.h>
#include <SPI.h>
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
int key;
#define SD_CS 34



void setup()
{
Serial.begin(115200);
SerialBT.begin("ESP32");


SD.begin(SD_CS);
if(!SD.begin(SD_CS)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.println("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}

// If the data.txt file doesn't exist
// Create a file on the SD card and write the data labels
File file = SD.open("data.txt");
if(!file) {
Serial.println("File doens't exist");
Serial.println("Creating file...");
writeFile(SD, "data.txt", "data \r\n");
}
else {
Serial.println("File already exists");
}
file.close();




}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{

int value =0;

int D0 = digitalRead(2);
int D1 = digitalRead(15);
int D2 = digitalRead(4);
int D3 = digitalRead(16);
int D4 = digitalRead(17);
int D5 = digitalRead(5);
int D6 = digitalRead(18);
int D7 = digitalRead(19);
char val[256];
snprintf(val , sizeof(val) , "%d %d %d %d %d %d %d %d",
D0,D1,D2,D3,D4,D5,D6,D7);

Serial.println(val);




byte Date[8];
Date[0] = D0;
Date[1] = D1;
Date[2] = D2;
Date[3] = D3;
Date[4] = D4;
Date[5] = D5;
Date[6] = D6;
Date[7] = D7;

readFromAPP();
if(key == 65)
{
SerialBT.write('B');
for(int i=0;i<8;i++)
{
SerialBT.write(Date);
delay(10);

}
key = 0;

}
delay(100);
appendFile(SD, "data.txt", val);
}



void readFromAPP()
{
while (SerialBT.available() > 0)
{
key = SerialBT.read();
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);

File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}

void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);

File file = fs.open(path, FILE_WRITE);
if(!file) {
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}

我是位初學者利用arduino IDE 1.8.9做ESP32的開發
以上是我的程式碼我在製作將8個腳位的狀態透過藍芽傳輸並用SD卡紀錄
我一直無法將資料【val】寫入我的SD卡中
我將模組CS腳位連接到開發版34腳內
程式編譯沒問題但我一直沒辦法將我的資料【val】寫入SD卡內
我想知道為什麼及如何修改

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: SD卡無法寫入資料

Postby ESP_Gargamel » Wed May 29, 2019 10:14 am

hi,arduino 的问题,您能提交到 viewforum.php?f=19 吗?需要英文提交,中文板块这边没有对应的工程师。

Who is online

Users browsing this forum: No registered users and 94 guests