Why wont serial print my message after the loop is finished

Why wont serial print my message after the loop is finished


Yeah for some reason after the Arduino loops through the file in my sd card it just stops it doesn't continue the code after the while loop and it doesn't start the loop method, I suspect its getting an error but im not sure where I would see that if that was even the case.

Please help me!

/*
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/
#include <SPI.h>
#include <SD.h>
#include "PATH/vector" //import vector

//inialtizes variables
File myFile;
int recNum = 0; 

//set array as vector
std::vector< String > array;
 
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.");

  myFile = SD.open("PCB.txt");
  if (myFile) {
    Serial.println("PCB.txt:");

    while (myFile.available()){
      Serial.println("In loop");
      String list = myFile.readStringUntil('\n');
      array.push_back(list);
      recNum++; // Count the record
   }

  // close the file:
  myFile.close();

  }else {
    // if the file didn't open, print an error:
    Serial.println("error opening CB.schem");
  }
  Serial.println("This should print");
}

void loop() {
  Serial.println("hi");
  // nothing happens after setup
}

Terminal

Initializing SD card...initialization done.
PCB.txt:
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop
In loop

This:

Should be this:

Yes, why? Because readStringUntil() checks available() itself.

So it should be like this?

for(String list = myFile.readStringUntil('\n'); list;){
      Serial.println("In loop");
      array.push_back(list);
      recNum++; // Count the record
   }

The last clause is the "action" that occurs at the end of the loop. Usually an increment...

sorry for the confusion, I fixed it to make it more clear

If you can't be sure of a for loop, use simpler constructs first. Why do you reject the solution given in reply #2?

I rejected it cause I didn't see how it would work in an if statement cause it needs to be in a loop, just wanted to see if my solution would work which is why I posted it, still willing to go back to it just trying to come up with my own solution to get a better understanding.

Im a bit confused cause I made the for loop like this

for(String list = myFile.readStringUntil('\n'); list;){
      array.push_back(list);
      recNum++; // Count the record
      Serial.println(recNum);
   }

But after 18 its spitting out letters

Initializing SD card...initialization done.
PCB.txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
B
C
D
E
F
G
H
I
J

Which Arduino are you using?

Arduino UNO r3

You only have 2k RAM, already loose 512 bytes for the SD buffer,
I don't think String arrays are a good idea.

You probably just run out of memory.

So you ask for advice and then reject it because you don't understand it.

1 Like

I should get a more powerful Arduino or a different approach? I dont really see another way of approaching this besides an array of strings but if you have an idea please let me know

Whats with the hostility?

I would use an ESP32, it has plenty of memory, besides 1 or 2 cores, WiFi and Bluetooth.

can it be programmed as an arduino?

Yes.

Just one example of a board with SD card.

This has microPython installed from the factory, so maybe there are better picks.

The ESP32 CAM also has an SD interface built in, is programmed with Arduino, and is cheap.

None. I simply pointed out your behaviour.

What behavior? I was simply trying out your way of doing things first by only using readStringUntil because It made more sense to me? I don't understand why your being a jerk

You think this would work, also with my SD card module?