Hello everyone, I wonder why do my file won't open after few try of retrieving text information from sd card here's the part of the code and here's also the screenshot of my problem.
And also I already used 71% of program storage space and 79% of dynamic memory that may can help to solve my problem thank you.
/*
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>
String toDisplay = "", findNext = "";
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(10)) {
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
if (digitalRead(4) == HIGH)
arrowDown();
if(digitalRead(7) == HIGH)
arrowUp();
}
void arrowDown() {
start:
if (!SD.begin(10)) {
Serial.println("initialization failed!");
//lcd.setCursor(0, 2);
//lcd.print(" ");
//lcd.setCursor(0, 2);
//cd.print("NULL SD CARD!");
}
toDisplay = "";
bool found = false;
bool next = false;
myFile = SD.open("test.txt");
if (myFile) {
while (myFile.available() && next == false) {
String list = myFile.readStringUntil('\n');
if (findNext == "") {
next = true;
toDisplay = list;
findNext = list;
}
if (findNext != "" && found == true) {
findNext = list;
toDisplay = list;
next = true;
}
if (findNext != "" && findNext == list)
found = true;
}
if (toDisplay == "") {
findNext ="";
goto start;
}
myFile.close();
} else
Serial.println("error occured");
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()) {
String list = myFile.readStringUntil('\n');
Serial.print(list);
Serial.print(" = ");
Serial.print(toDisplay);
Serial.print(" Status : ");
Serial.println(list == toDisplay);
}
// close the file:
myFile.close();
} else
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
//displayToLcd();
}
void arrowUp() {
start:
if (!SD.begin(10)) {
Serial.println("initialization failed!");
//lcd.setCursor(0, 2);
//lcd.print(" ");
//lcd.setCursor(0, 2);
//lcd.print("NULL SD CARD!");
}
myFile = SD.open("test.txt");
int count = 0;
String temp;
bool empty1 = false;
bool prev = false;
if (myFile && findNext == "") {
while (myFile.available()) {
String list = myFile.readStringUntil('\n');
toDisplay = list;
findNext = list;
}
myFile.close();
}
if (myFile && findNext != "") {
while (myFile.available() && prev == false) {
String list = myFile.readStringUntil('\n');
if (count == 0 && findNext == list) {
findNext = "";
goto start;
}
if (count == 0 && findNext != list && empty1 == false) {
temp = list;
empty1 = true;
}
if (count == 1 && findNext != list) {
temp = list;
Serial.print("\n\n");
Serial.print(temp);
Serial.print(" ");
Serial.print(list);
}
if (count == 1 && findNext == list) {
toDisplay = temp;
findNext = temp;
prev = true;
}
count++;
if (count == 2)
count = 1;
}
myFile.close();
}
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()) {
String list = myFile.readStringUntil('\n');
Serial.print(list);
Serial.print(" = ");
Serial.print(toDisplay);
Serial.print(" Status : ");
Serial.println(list == toDisplay);
}
// close the file:
myFile.close();
} else
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
//displayToLcd();
}