Help

Welcome!

This community is for professionals and enthusiasts of our products and services.
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

0

Arduino writing/reading struct object to SDCARD

Avatar
9H5BM Jason

I came across an article where majenko explains how to read/write to sd a struct.


I tried to read and write to sd but was faced with some issues. 

Is there a Github source for this demo? 



2 Comments
Avatar
Discard
Avatar
Matt Jenkins
-

No, there isn't. What's the problem you've been having?

Avatar
9H5BM Jason
-
Hi Majenko

Issue with that code in the website is that it compiles ok. but when i left it running and then tried to read the sd card , there was no binary raw data saved. I have also replied in the forum page and pasted the code . Maybe I am missing something here.  If this works s expected then I can maybe make a class and maybe add a seek feature. 

regards 
Jason 

On Tue, Dec 28, 2021 at 8:39 PM Matt Jenkins <matt@majenko.co.uk> wrote:

No, there isn't. What's the problem you've been having?

--
Matt Jenkins

Majenko Technologies

Sent by Majenko Technologies using Odoo.

1 Answer
0
Avatar
9H5BM Jason
Best Answer

Hi, 


Thanks for replying, 

Although the code compiles,  nothing is saved on SD card, executing the reading part , nothing is returned 


Please find code example here , maybe i am missing something 

// pin 9 used for sc cs  and pin 10 for ethernet shield  cs 

//write to file in binary 
#include <SPI.h>

#include <SD.h>

const int chipSelect = 9;

File dataFile;

struct datastore {

  uint16_t adc1;

  uint16_t adc2;

  float voltage;

  float current;

};


void setup() {

  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  pinMode(chipSelect, OUTPUT);

  digitalWrite(9, LOW);

  pinMode(10, OUTPUT);

  digitalWrite(10, HIGH);


  if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    return;

  }


  Serial.println("card initialized.");

  dataFile = SD.open("datalog.dat", FILE_WRITE);

}

void loop() {

  struct datastore myData;

  myData.adc1 = 255; // analogRead(A0);

  myData.adc2 = 123; // analogRead(A1);

  myData.voltage = myData.adc1 / 1023.0 * 5.0;

  myData.current = myData.adc2 / 10000.0 * 23.4429;

  dataFile.write((const uint8_t *)&myData, sizeof(myData));

  delay(50);

  Serial.println(myData.adc1);

  Serial.println(myData.adc2);

  Serial.println(myData.voltage);

  Serial.println(myData.current);

}


.// read back binary saved data 

#include <SPI.h>

#include <SD.h>


const int chipSelect = 9;

File dataFile;


struct datastore {

  uint16_t adc1;

  uint16_t adc2;

  float voltage;

  float current;

};


void setup() {

  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  pinMode(10, OUTPUT);

  digitalWrite(9, LOW);

  pinMode(10, OUTPUT);

  digitalWrite(10, HIGH);


  if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    return;

  }


  Serial.println("card initialized.");

  dataFile = SD.open("datalog.dat", FILE_READ);

  

}


void loop() {

//read the data

  if (dataFile.available()) {

    struct datastore myData;

    dataFile.read((uint8_t *)&myData, sizeof(myData));

  //  analogWrite(5, map(myData.adc1, 0, 1023, 0, 255));

  //  analogWrite(6, map(myData.adc2, 0, 1023, 0, 255));

   Serial.println(myData.adc1, DEC);

  Serial.println(myData.adc2, DEC);

  Serial.println(myData.voltage, DEC);

  Serial.println(myData.current, DEC);

  //  Serial.print(myData.voltage, 4);

 //   Serial.print(" ");

 //   Serial.println(myData.current, 4);

    delay(50);

  }

}


2 Comments
Avatar
Discard
Avatar
Matt Jenkins
-

How long do you wait when doing the writing before resetting or turning off the Arduino? You have no `.close()` or `.flush()` so the data will never be written to the card unless you write enough data that the internal buffer overflows and has to be written to the card to make room.

Avatar
9H5BM Jason
-
Hi Matt 

First of all, I wish you all the best for the year 2022! & thanks for replying to my mail.

Ok, thanks for the clarification. so once I fill the struct with some data, I will save it to sd, then flush it and fill it again - and so on ... 

can you update the code on your page so it's more clear? 

regards
 

On Fri, Dec 31, 2021 at 7:40 PM Matt Jenkins <matt@majenko.co.uk> wrote:

How long do you wait when doing the writing before resetting or turning off the Arduino? You have no `.close()` or `.flush()` so the data will never be written to the card unless you write enough data that the internal buffer overflows and has to be written to the card to make room.

--
Matt Jenkins

Majenko Technologies

Sent by Majenko Technologies using Odoo.