I can't write my data into SDcard

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

I can't write my data into SDcard

Postby U0421011 » Mon Jun 10, 2019 7:02 pm

Code: Select all

#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;
  }
  uint32_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
  }


  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);

  appendFile(SD, "data.txt", 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[i]);
      delay(10); 
    
    }
    key = 0;
    
  }
  delay(100); 

 
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
}
I am a beginner to use arduino IDE 1.8.9 to do ESP32 development.
This is my code. I want to let 8 pin state through Bluetooth transmission and recording with SD card.
I have been unable to write the data [val] into my SD card.
I connected the CS to pin 34 .
The program is compiled without problems, but I have been unable to write my data [val] to the SD card.
I want to know why and how to modify

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 55 guests