The sketch crashes

The program reads the data you send through the serial port and saves it in "Pregunta", and then reads all the "Titu.txt" files that are in numbered folders (001, 002, 003 ...), inside another a its time called "Manual", when I deactivate the part of reading the files, the part of reading the serial port works perfectly but when activating the other part nothing works:

#include <string.h>
#include <SD.h>

int ID, RAM, RAM2, c1 , c2, c3;
char Respuesta, Pregunta, cP, Directorio;
File Archivo;

void setup(){
  
  SD.begin(9);
  Serial.begin(9600);
  Serial.println("Listo");
  
}

void loop(){
  
  if (Serial.available()){

    RAM = 0;
    RAM2 = 0;
    Pregunta = Serial.read();

    while(Serial.available()){

      Pregunta = Pregunta + Serial.read();

    }

    Serial.print(Pregunta);

    while(1){

      RAM++;
      c1 = ((RAM - floor(RAM/10)*10) + 48);
      c2 = (((RAM - floor(RAM/100)*100)/10) + 48);
      c3 = (floor(RAM/100) + 48);
      char cP[3] = {c3, c2, c1};
      char Directorio[20] = "Manual/";
      strcat(Directorio, Directorio);
      strcat(Directorio, "/Titu.txt");
      Serial.print(Directorio);

      if (SD.exists(Directorio)){

        Archivo = SD.open(Directorio);
        Respuesta = Archivo.read();

        while(Archivo.available()){
          
            Respuesta = Respuesta + Archivo.read();

        }
        
        Serial.print(Respuesta);
        
      } else {

        break;

      }
      
    }
    
  }
  
}

Until

Serial.print (Pregunta);

is the first part and from to is the second

Pregunta is just ONE byte (a char) so here you are doing arithmetics, not concatenation (you are not building a string)

is that what you want?

can you explain this?

not only do you need to make sure there is enough room in Directorio for the end result, but why are you doing
strcat(Directorio, Directorio); again?
you have max 19 chars available for the directory, here you are overflowing

using strlcat() would keep you safe from overflow and you could test if you have been overflowing)

I think you want:

      char Directorio[20] = "Manual/";
      strcat(Directorio, "Titu.txt");

or

      char Directorio[20];
      strcpy(Directorio, "Manual/");
      strcat(Directorio, "Titu.txt");

or even
char Directorio[] = "Manual/Titu.txt";

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.