<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] arduino save int variable value on SD]]></title><description><![CDATA[<p dir="auto">Hi, how can I save an int variable value on m5stack? (like eeprom)<br />
is it possible to save it on sd card?</p>
<p dir="auto">tnks</p>
]]></description><link>https://community.m5stack.com/topic/436/solved-arduino-save-int-variable-value-on-sd</link><generator>RSS for Node</generator><lastBuildDate>Thu, 30 Apr 2026 17:41:02 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/436.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 08 Dec 2018 00:05:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Wed, 26 Dec 2018 10:01:32 GMT]]></title><description><![CDATA[<p dir="auto">the  <a href="https://forum.arduino.cc/index.php?topic=587002.0" target="_blank" rel="noopener noreferrer nofollow ugc">solution</a>:</p>
<p dir="auto"><code>file.parseInt();</code></p>
<p dir="auto">thanks a lot !!!</p>
]]></description><link>https://community.m5stack.com/post/2240</link><guid isPermaLink="true">https://community.m5stack.com/post/2240</guid><dc:creator><![CDATA[cepics]]></dc:creator><pubDate>Wed, 26 Dec 2018 10:01:32 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Wed, 26 Dec 2018 05:06:00 GMT]]></title><description><![CDATA[<p dir="auto">Hello,if you want use eeprom, suggest try this example in arduino ide:</p>
<pre><code>file-&gt;Examples-&gt;Preferences
</code></pre>
<p dir="auto"><a href="https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino</a></p>
]]></description><link>https://community.m5stack.com/post/2239</link><guid isPermaLink="true">https://community.m5stack.com/post/2239</guid><dc:creator><![CDATA[heybin]]></dc:creator><pubDate>Wed, 26 Dec 2018 05:06:00 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Tue, 25 Dec 2018 11:04:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ajb2k3" aria-label="Profile: ajb2k3">@<bdi>ajb2k3</bdi></a> said in <a href="/post/2235">arduino save int variable value</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/cepics" aria-label="Profile: cepics">@<bdi>cepics</bdi></a><br />
Have you tried playing with the sd read and write examples?</p>
</blockquote>
<p dir="auto">yes this one</p>
<pre><code>/*
  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 &lt;SPI.h&gt;
#include &lt;SD.h&gt;

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
}


</code></pre>
<p dir="auto">it compile but the serial monitor say:<br />
<code> error opening test.txt</code></p>
<p dir="auto">right now, with an example from <a href="http://forum.m5stack.com/topic/184/problems-writing-to-sd-card" target="_blank" rel="noopener noreferrer nofollow ugc">here</a>, I can write an int variable in a file on sd</p>
<pre><code>
#include &lt;M5Stack.h&gt;
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:

}
</code></pre>
<p dir="auto">and I can read the file with</p>
<pre><code>
#include &lt;M5Stack.h&gt;
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:

}
</code></pre>
<p dir="auto">but I can't use the "ch" variable ..</p>
<p dir="auto">if I add, on the "read" sketch</p>
<pre><code>int y = ch + 2000;
Serial.print(y);
</code></pre>
<p dir="auto">in the serial monitor I have:</p>
<pre><code>M5Stack initializing...O�������
Read from file: 2000
2048
</code></pre>
<p dir="auto">2048 instead of 4000</p>
<p dir="auto">now I'm searching how to convert ASCII to binary</p>
<p dir="auto">thanks for interest..</p>
]]></description><link>https://community.m5stack.com/post/2236</link><guid isPermaLink="true">https://community.m5stack.com/post/2236</guid><dc:creator><![CDATA[cepics]]></dc:creator><pubDate>Tue, 25 Dec 2018 11:04:40 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Tue, 25 Dec 2018 10:02:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/cepics" aria-label="Profile: cepics">@<bdi>cepics</bdi></a><br />
Have you tried playing with the sd read and write examples?</p>
]]></description><link>https://community.m5stack.com/post/2235</link><guid isPermaLink="true">https://community.m5stack.com/post/2235</guid><dc:creator><![CDATA[ajb2k3]]></dc:creator><pubDate>Tue, 25 Dec 2018 10:02:46 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Tue, 25 Dec 2018 08:20:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/yellowelise" aria-label="Profile: yellowelise">@<bdi>yellowelise</bdi></a> said in <a href="/post/2207">arduino save int variable value</a>:</p>
<blockquote>
<p dir="auto">How many cycle before dead of flash partition?</p>
</blockquote>
<p dir="auto">I'm interested too</p>
]]></description><link>https://community.m5stack.com/post/2234</link><guid isPermaLink="true">https://community.m5stack.com/post/2234</guid><dc:creator><![CDATA[cepics]]></dc:creator><pubDate>Tue, 25 Dec 2018 08:20:20 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Mon, 24 Dec 2018 20:32:28 GMT]]></title><description><![CDATA[<p dir="auto">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)</p>
<p dir="auto">I'm trying this sketch:</p>
<pre><code>// http://forum.m5stack.com/topic/184/problems-writing-to-sd-card
#include &lt;M5Stack.h&gt;

//Micro SD / TF Card Test


String cal;
String lum;
String unit;

bool brght = 0;
bool ftmt = 1;

int x = 1680;

void listDir(fs::FS &amp;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 &amp;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 &amp;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();
}
</code></pre>
<p dir="auto">but I can't understand how to read from sd and use the variables in the sketch..<br />
I need to read and write two boolean state and one int variables ..<br />
I'm a newbi and all the sketch I found on line that use sd.h library doesn't work on M5Stack</p>
<p dir="auto">if I upload read/write arduino sd example, this is the serial monitor output:</p>
<p dir="auto">error opening test.txt</p>
<p dir="auto">please help</p>
<p dir="auto">thanks a lot</p>
]]></description><link>https://community.m5stack.com/post/2228</link><guid isPermaLink="true">https://community.m5stack.com/post/2228</guid><dc:creator><![CDATA[cepics]]></dc:creator><pubDate>Mon, 24 Dec 2018 20:32:28 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Tue, 18 Dec 2018 08:03:37 GMT]]></title><description><![CDATA[<p dir="auto">How many cycle before dead of flash partition?</p>
]]></description><link>https://community.m5stack.com/post/2207</link><guid isPermaLink="true">https://community.m5stack.com/post/2207</guid><dc:creator><![CDATA[yellowelise]]></dc:creator><pubDate>Tue, 18 Dec 2018 08:03:37 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Sun, 16 Dec 2018 07:37:36 GMT]]></title><description><![CDATA[<p dir="auto">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).</p>
<p dir="auto">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 <a href="https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino" target="_blank" rel="noopener noreferrer nofollow ugc">here</a> for an example that stores a value (the number of times the ESP was booted) in flash.</p>
]]></description><link>https://community.m5stack.com/post/2198</link><guid isPermaLink="true">https://community.m5stack.com/post/2198</guid><dc:creator><![CDATA[Rop]]></dc:creator><pubDate>Sun, 16 Dec 2018 07:37:36 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Tue, 25 Dec 2018 08:13:07 GMT]]></title><description><![CDATA[<p dir="auto">on esp8266, to save/read a value in to eeprom, I do:</p>
<pre><code>#include &lt;EEPROM.h&gt;
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);
</code></pre>
<p dir="auto">how can I do to save/read x variable on sd with m5?</p>
<p dir="auto">tnks a lot</p>
]]></description><link>https://community.m5stack.com/post/2191</link><guid isPermaLink="true">https://community.m5stack.com/post/2191</guid><dc:creator><![CDATA[cepics]]></dc:creator><pubDate>Tue, 25 Dec 2018 08:13:07 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] arduino save int variable value on SD on Tue, 11 Dec 2018 01:35:09 GMT]]></title><description><![CDATA[<p dir="auto">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?</p>
]]></description><link>https://community.m5stack.com/post/2162</link><guid isPermaLink="true">https://community.m5stack.com/post/2162</guid><dc:creator><![CDATA[lukasmaximus]]></dc:creator><pubDate>Tue, 11 Dec 2018 01:35:09 GMT</pubDate></item></channel></rss>