Could I save a data in micro SD card?

  arduino, arduino-ide, arduino-uno, c++

I want to check ultrasonic sensor data by micro SD card.

ultrasonic protocol is I2C.

but Serial monitor show "Couldn’t open log file" message me.

I don’t know why.

When I used another Lidar sensor by PWM, I can save a data.

But, Same sensor that use I2C Method cannot save a data.

Are there problem in code??

or Are Both saving data and using I2C Method impossible??

Please help me.

#include <SoftwareSerial.h>
#include <SD.h>

SoftwareSerial mySerial = SoftwareSerial(3,2);   // Tx(초록색) ,Rx(파란색)
unsigned char data[4]={};
float distance;

const int CS_PIN=10;

void setup() {
  Serial.begin(57600);
  mySerial.begin(9600);
  Serial.println("Initializing Card");
  pinMode(CS_PIN,OUTPUT);

  if(!SD.begin(CS_PIN))
  {
    Serial.println("Card Failure");
    return;
  }

  Serial.println("Card Ready");
}

void loop() {
  do{
    for(int i=0;i<4;i++)
    {
      data[i]=mySerial.read();
    }
  }while(mySerial.read()==0xff);
  
  mySerial.flush();
  long timeStamp=millis()/100.0;
  int timeStamp1=round(timeStamp);
  long timeStamp2 = timeStamp1/10.0;
  File dataFile=SD.open("log.csv",FILE_WRITE);
  
  if(data[0]==0xff)
  {
    int sum;
    sum=(data[0]+data[1]+data[2])&0x00FF;
    if(sum==data[3])
    {
      distance=(data[1]<<8)+data[2];
      if(distance>50)
      {
                  
        Serial.print("distance = ");
        Serial.print(distance/10);
        Serial.println("cm");
          
        if(dataFile)
        {
          int distance1 = round(distance/10);
          dataFile.print(timeStamp2);
          dataFile.print(",");
          dataFile.println(distance1);
          dataFile.close();
        }
        else
        {
          Serial.println("Couldn't open log file");
        }
      }
      else
      {
        Serial.println("Below the lower limit");
      }
    }
    else Serial.println("ERROR");
  }
  delay(100);
}

Source: Windows Questions C++

LEAVE A COMMENT