🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    [SOLVED] arduino save int variable value on SD

    Arduino
    6
    11
    28.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      cepics
      last edited by cepics

      Hi, how can I save an int variable value on m5stack? (like eeprom)
      is it possible to save it on sd card?

      tnks

      1 Reply Last reply Reply Quote 0
      • lukasmaximusL
        lukasmaximus
        last edited by

        I'm not sure I quite understand your question. Do you want to log the output of a variable to a text file on the SD?

        1 Reply Last reply Reply Quote 0
        • C
          cepics
          last edited by cepics

          on esp8266, to save/read a value in to eeprom, I do:

          #include <EEPROM.h>
          int address = 0;
          
           EEPROM.begin(512);
            
          byte x = sonar.convert_cm(echoTime);
            delay(1000);
            EEPROM.write(address, x);
            delay(1000);
            EEPROM.commit();
            delay(1000);
          
            x = EEPROM.read(address);
            delay (500);
          

          how can I do to save/read x variable on sd with m5?

          tnks a lot

          1 Reply Last reply Reply Quote 0
          • RopR
            Rop
            last edited by

            M5Stack has the same "EEPROM" (Which is really a simulation that simply uses a small partition in the flash memory.) If I am not mistaken the code you quote should work still on the ESP32 (and thus on the M5Stack).

            ESP32's have something called NVM, which stores key-value pairs in another partition of the same flash. In the Arduino world, "Preferences.h" provides a wrapper to use these functions. Have a look here for an example that stores a value (the number of times the ESP was booted) in flash.

            1 Reply Last reply Reply Quote 0
            • yelloweliseY
              yellowelise
              last edited by

              How many cycle before dead of flash partition?

              C 1 Reply Last reply Reply Quote 0
              • C
                cepics
                last edited by cepics

                I would like to save some variables, like I did with EEPROM on esp8266, but on M5Stack sd.. (to kill sd card instead of esp32 memory)

                I'm trying this sketch:

                // http://forum.m5stack.com/topic/184/problems-writing-to-sd-card
                #include <M5Stack.h>
                
                //Micro SD / TF Card Test
                
                
                String cal;
                String lum;
                String unit;
                
                bool brght = 0;
                bool ftmt = 1;
                
                int x = 1680;
                
                void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
                
                  // Print blank line on screen
                  M5.Lcd.printf(" \n  ");
                  M5.Lcd.printf(" \n  ");
                
                  Serial.printf("Listing directory: %s\n", dirname);
                  M5.Lcd.printf("Listing directory: %s\n", dirname);
                
                  File root = fs.open(dirname);
                  if (!root) {
                    Serial.println("Failed to open directory");
                    M5.Lcd.println("Failed to open directory");
                    return;
                  }
                  if (!root.isDirectory()) {
                    Serial.println("Not a directory");
                    M5.Lcd.println("Not a directory");
                    return;
                  }
                
                  File file = root.openNextFile();
                  while (file) {
                    if (file.isDirectory()) {
                      Serial.print("  DIR : ");
                      M5.Lcd.print("  DIR : ");
                      Serial.println(file.name());
                      M5.Lcd.println(file.name());
                      if (levels) {
                        listDir(fs, file.name(), levels - 1);
                      }
                    } else {
                      Serial.print("  FILE: ");
                      M5.Lcd.print("  FILE: ");
                      Serial.print(file.name());
                      M5.Lcd.print(file.name());
                      Serial.print("  SIZE: ");
                      M5.Lcd.print("  SIZE: ");
                      Serial.println(file.size());
                      M5.Lcd.println(file.size());
                    }
                    file = root.openNextFile();
                  }
                }
                
                void readFile(fs::FS &fs, const char * path) {
                  Serial.printf("Reading file: %s\n", path);
                  M5.Lcd.printf("Reading file: %s\n", path);
                
                  File file = fs.open(path);
                  if (!file) {
                    Serial.println("Failed to open file for reading");
                    M5.Lcd.println("Failed to open file for reading");
                    return;
                  }
                
                  Serial.print("Read from file: ");
                  M5.Lcd.print("Read from file: ");
                  while (file.available()) {
                    int ch = file.read();
                    Serial.write(ch);
                    M5.Lcd.write(ch);
                  }
                }
                
                void writeFile(fs::FS &fs, const char * path, const char * message) {
                  Serial.printf("Writing file: %s\n", path);
                  M5.Lcd.printf("Writing file: %s\n", path);
                
                  File file = fs.open(path, FILE_WRITE);
                  if (!file) {
                    Serial.println("Failed to open file for writing");
                    M5.Lcd.println("Failed to open file for writing");
                    return;
                  }
                  if (file.print(message)) {
                    Serial.println("File written");
                    M5.Lcd.println("File written");
                  } else {
                    Serial.println("Write failed");
                    M5.Lcd.println("Write failed");
                  }
                }
                
                void setup() {
                
                  M5.begin();
                  Wire.begin();
                
                  Serial.begin(115200);
                
                  // Lcd display
                  M5.Lcd.setBrightness(100);
                  M5.Lcd.fillScreen(BLACK);
                  M5.Lcd.setCursor(0, 10);
                  M5.Lcd.setTextColor(WHITE);
                  M5.Lcd.setTextSize(2);
                
                  // Page Header
                  M5.Lcd.fillScreen(BLACK);
                  M5.Lcd.setCursor(0, 05);
                  M5.Lcd.printf("Testing SD Card Functions:\r\n");
                
                  /////////////////////////////////////////////////////////////////////////
                  cal = String("cal = ") + String(x) + "\r\n";
                  lum = String("lum = ") + String(brght) + "\r\n";
                  unit = String("unit = ") + String(ftmt) + "\r\n";
                
                  writeFile(SD, "/cal.txt", cal.c_str()); // questa funziona!!!!
                  writeFile(SD, "/lum.txt", lum.c_str()); // questa funziona!!!!
                  writeFile(SD, "/unit.txt", unit.c_str()); // questa funziona!!!!
                  /////////////////////////////////////////////////////////////////////////
                
                  M5.Lcd.printf("");
                  // Print blank line on screen
                  M5.Lcd.printf(" \n  ");
                  // Print blank line on screen
                  M5.Lcd.printf(" \n  ");
                
                  readFile(SD, "/cal.txt");
                  readFile(SD, "/lum.txt");
                  readFile(SD, "/unit.txt");
                
                }
                
                void loop() {
                
                  // put your main code here, to run repeatedly:
                
                  M5.update();
                }
                

                but I can't understand how to read from sd and use the variables in the sketch..
                I need to read and write two boolean state and one int variables ..
                I'm a newbi and all the sketch I found on line that use sd.h library doesn't work on M5Stack

                if I upload read/write arduino sd example, this is the serial monitor output:

                error opening test.txt

                please help

                thanks a lot

                ajb2k3A 1 Reply Last reply Reply Quote 0
                • C
                  cepics @yellowelise
                  last edited by

                  @yellowelise said in arduino save int variable value:

                  How many cycle before dead of flash partition?

                  I'm interested too

                  1 Reply Last reply Reply Quote 0
                  • ajb2k3A
                    ajb2k3 @cepics
                    last edited by

                    @cepics
                    Have you tried playing with the sd read and write examples?

                    UIFlow, so easy an adult can learn it!
                    If I don't know it, be patient!
                    I've ether not learned it or am too drunk to remember it!
                    Author of the WIP UIFlow Handbook!
                    M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

                    C 1 Reply Last reply Reply Quote 0
                    • C
                      cepics @ajb2k3
                      last edited by cepics

                      @ajb2k3 said in arduino save int variable value:

                      @cepics
                      Have you tried playing with the sd read and write examples?

                      yes this one

                      /*
                        SD card read/write
                      
                       This example shows how to read and write data to and from an SD card file
                       The circuit:
                       * SD card attached to SPI bus as follows:
                       ** MOSI - pin 11
                       ** MISO - pin 12
                       ** CLK - pin 13
                       ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
                      
                       created   Nov 2010
                       by David A. Mellis
                       modified 9 Apr 2012
                       by Tom Igoe
                      
                       This example code is in the public domain.
                      
                       */
                      
                      #include <SPI.h>
                      #include <SD.h>
                      
                      File myFile;
                      
                      void setup() {
                        // Open serial communications and wait for port to open:
                        Serial.begin(9600);
                        while (!Serial) {
                          ; // wait for serial port to connect. Needed for native USB port only
                        }
                      
                      
                        Serial.print("Initializing SD card...");
                      
                        if (!SD.begin(4)) {
                          Serial.println("initialization failed!");
                          while (1);
                        }
                        Serial.println("initialization done.");
                      
                        // open the file. note that only one file can be open at a time,
                        // so you have to close this one before opening another.
                        myFile = SD.open("test.txt", FILE_WRITE);
                      
                        // if the file opened okay, write to it:
                        if (myFile) {
                          Serial.print("Writing to test.txt...");
                          myFile.println("testing 1, 2, 3.");
                          // close the file:
                          myFile.close();
                          Serial.println("done.");
                        } else {
                          // if the file didn't open, print an error:
                          Serial.println("error opening test.txt");
                        }
                      
                        // re-open the file for reading:
                        myFile = SD.open("test.txt");
                        if (myFile) {
                          Serial.println("test.txt:");
                      
                          // read from the file until there's nothing else in it:
                          while (myFile.available()) {
                            Serial.write(myFile.read());
                          }
                          // close the file:
                          myFile.close();
                        } else {
                          // if the file didn't open, print an error:
                          Serial.println("error opening test.txt");
                        }
                      }
                      
                      void loop() {
                        // nothing happens after setup
                      }
                      
                      
                      

                      it compile but the serial monitor say:
                      error opening test.txt

                      right now, with an example from here, I can write an int variable in a file on sd

                      
                      #include <M5Stack.h>
                      int cal = 2000;
                      
                      
                      void setup() {
                      
                        M5.begin();
                        Wire.begin();
                      
                        Serial.begin(115200);
                      
                        File file = SD.open("/cal.txt", FILE_WRITE);
                        if (!file) {
                          Serial.println("Failed to open file for writing");
                          return;
                        }
                        if (file.print(cal)) {
                          Serial.println("File written");
                        } else {
                          Serial.println("Write failed");
                        }
                      
                      }
                      
                      void loop() {
                        // put your main code here, to run repeatedly:
                      
                      }
                      

                      and I can read the file with

                      
                      #include <M5Stack.h>
                      int ch;
                      
                      void setup() {
                      
                        M5.begin();
                        Wire.begin();
                      
                        Serial.begin(115200);
                      
                        File file = SD.open("/cal.txt");
                        if (!file) {
                          Serial.println("Failed to open file for reading");
                          return;
                        }
                      
                        Serial.print("Read from file: ");
                        M5.Lcd.print("Read from file: ");
                        while (file.available()) {
                          ch = file.read();
                          Serial.write(ch);
                          M5.Lcd.write(ch);
                        }
                      }
                      
                      
                      void loop() {
                        // put your main code here, to run repeatedly:
                      
                      }
                      

                      but I can't use the "ch" variable ..

                      if I add, on the "read" sketch

                      int y = ch + 2000;
                      Serial.print(y);
                      

                      in the serial monitor I have:

                      M5Stack initializing...O�������
                      Read from file: 2000
                      2048
                      

                      2048 instead of 4000

                      now I'm searching how to convert ASCII to binary

                      thanks for interest..

                      1 Reply Last reply Reply Quote 0
                      • H
                        heybin
                        last edited by

                        Hello,if you want use eeprom, suggest try this example in arduino ide:

                        file->Examples->Preferences
                        

                        https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino

                        1 Reply Last reply Reply Quote 0
                        • C
                          cepics
                          last edited by

                          the solution:

                          file.parseInt();

                          thanks a lot !!!

                          1 Reply Last reply Reply Quote 1
                          • First post
                            Last post