<?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[save some variables into sd card]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I would like to save three variables value on the sd.<br />
boolean brgt<br />
boolean unit<br />
int x<br />
and, on setup() read that values...<br />
I'm trying with String, and I can save a string of my value in a file on sd..<br />
but I don't know how to read the separate value to use it in the sketch</p>
<p dir="auto">// <a href="http://forum.m5stack.com/topic/184/problems-writing-to-sd-card" target="_blank" rel="noopener noreferrer nofollow ugc">http://forum.m5stack.com/topic/184/problems-writing-to-sd-card</a><br />
#include &lt;M5Stack.h&gt;</p>
<p dir="auto">//Micro SD / TF Card Test</p>
<p dir="auto">void listDir(fs::FS &amp;fs, const char * dirname, uint8_t levels) {</p>
<p dir="auto">// Print blank line on screen<br />
M5.Lcd.printf(" \n  ");<br />
M5.Lcd.printf(" \n  ");</p>
<p dir="auto">Serial.printf("Listing directory: %s\n", dirname);<br />
M5.Lcd.printf("Listing directory: %s\n", dirname);</p>
<p dir="auto">File root = fs.open(dirname);<br />
if (!root) {<br />
Serial.println("Failed to open directory");<br />
M5.Lcd.println("Failed to open directory");<br />
return;<br />
}<br />
if (!root.isDirectory()) {<br />
Serial.println("Not a directory");<br />
M5.Lcd.println("Not a directory");<br />
return;<br />
}</p>
<p dir="auto">File file = root.openNextFile();<br />
while (file) {<br />
if (file.isDirectory()) {<br />
Serial.print("  DIR : ");<br />
M5.Lcd.print("  DIR : ");<br />
Serial.println(<a href="http://file.name" target="_blank" rel="noopener noreferrer nofollow ugc">file.name</a>());<br />
M5.Lcd.println(<a href="http://file.name" target="_blank" rel="noopener noreferrer nofollow ugc">file.name</a>());<br />
if (levels) {<br />
listDir(fs, <a href="http://file.name" target="_blank" rel="noopener noreferrer nofollow ugc">file.name</a>(), levels - 1);<br />
}<br />
} else {<br />
Serial.print("  FILE: ");<br />
M5.Lcd.print("  FILE: ");<br />
Serial.print(<a href="http://file.name" target="_blank" rel="noopener noreferrer nofollow ugc">file.name</a>());<br />
M5.Lcd.print(<a href="http://file.name" target="_blank" rel="noopener noreferrer nofollow ugc">file.name</a>());<br />
Serial.print("  SIZE: ");<br />
M5.Lcd.print("  SIZE: ");<br />
Serial.println(file.size());<br />
M5.Lcd.println(file.size());<br />
}<br />
file = root.openNextFile();<br />
}<br />
}</p>
<p dir="auto">void readFile(fs::FS &amp;fs, const char * path) {<br />
Serial.printf("Reading file: %s\n", path);<br />
M5.Lcd.printf("Reading file: %s\n", path);</p>
<p dir="auto">File file = fs.open(path);<br />
if (!file) {<br />
Serial.println("Failed to open file for reading");<br />
M5.Lcd.println("Failed to open file for reading");<br />
return;<br />
}</p>
<p dir="auto">Serial.print("Read from file: ");<br />
M5.Lcd.print("Read from file: ");<br />
while (file.available()) {<br />
int ch = file.read();<br />
Serial.write(ch);<br />
M5.Lcd.write(ch);<br />
}<br />
}</p>
<p dir="auto">void writeFile(fs::FS &amp;fs, const char * path, const char * message) {<br />
Serial.printf("Writing file: %s\n", path);<br />
M5.Lcd.printf("Writing file: %s\n", path);</p>
<p dir="auto">File file = fs.open(path, FILE_WRITE);<br />
if (!file) {<br />
Serial.println("Failed to open file for writing");<br />
M5.Lcd.println("Failed to open file for writing");<br />
return;<br />
}<br />
if (file.print(message)) {<br />
Serial.println("File written");<br />
M5.Lcd.println("File written");<br />
} else {<br />
Serial.println("Write failed");<br />
M5.Lcd.println("Write failed");<br />
}<br />
}</p>
<p dir="auto">//////////////////////////////////////////////////////////////////////////////////////<br />
bool suca = 1;<br />
bool assai = 0;<br />
int x = 1680;<br />
//////////////////////////////////////////////////////////////////////////////////////</p>
<p dir="auto">// the setup routine runs once when M5Stack starts up</p>
<p dir="auto">void setup() {</p>
<p dir="auto">// initialize the M5Stack object<br />
M5.begin();</p>
<p dir="auto">//    M5.startupLogo();<br />
Wire.begin();</p>
<p dir="auto">// Lcd display<br />
M5.Lcd.setBrightness(100);<br />
M5.Lcd.fillScreen(BLACK);<br />
M5.Lcd.setCursor(0, 10);<br />
M5.Lcd.setTextColor(WHITE);<br />
M5.Lcd.setTextSize(2);</p>
<p dir="auto">// Page Header<br />
M5.Lcd.fillScreen(BLACK);<br />
M5.Lcd.setCursor(0, 05);<br />
M5.Lcd.printf("Testing SD Card Functions:\r\n");</p>
<p dir="auto">//////////////////////////////////////////////////////////////////////////////////////</p>
<p dir="auto">store = String(brgt) + "," + String(unit) + "," + String(x) + "\r\n";</p>
<p dir="auto">//////////////////////////////////////////////////////////////////////////////////////<br />
//   writeFile(SD, "/hello.txt", "Hello world from M5Stack !!");<br />
writeFile(SD, "/hello.txt", store.c_str()); // questa funziona!!!!</p>
<p dir="auto">M5.Lcd.printf("");</p>
<p dir="auto">// Print blank line on screen<br />
M5.Lcd.printf(" \n  ");</p>
<p dir="auto">// Print blank line on screen<br />
M5.Lcd.printf(" \n  ");</p>
<p dir="auto">readFile(SD, "/hello.txt");<br />
//   store2[3] = (readFile(SD, "/hello.txt"));<br />
}</p>
<p dir="auto">void loop() {</p>
<p dir="auto">// put your main code here, to run repeatedly:</p>
<p dir="auto">M5.update();<br />
}</p>
<p dir="auto">is it correct to use String?<br />
is it the best way to save/read my variables?</p>
<p dir="auto">in the past, with esp8266,  I saved my three variables on EEPROM but since M5Stack have sd card I would like to have infinite cycle of read/write</p>
]]></description><link>https://community.m5stack.com/topic/447/save-some-variables-into-sd-card</link><generator>RSS for Node</generator><lastBuildDate>Sun, 07 Jun 2026 14:50:41 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/447.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Dec 2018 17:09:52 GMT</pubDate><ttl>60</ttl></channel></rss>