Arduino SD module

Hello everyone, i'm sorry for the mistake, it won't happen again, next time i'll put my question in the right section. I've done some debugging and i've come to very strange behaviour of my code. I'll explain now. I have a variable strpom, in which i store the data, that i receive via UART. strpomForming is a function that throws out the endesireble bytes that come on UART and gives me the form of the string that i want. Now i convert my variable strpom in pomString, so i can use the function substring to decompose my UART message. In the if else structure i print the data on LCD display. I have variables AP,DS,DP,AS,GV which are also String type and i use them to store some parts of my main string. I also have the piece of code that write my variable pomString into the SD card. Now when i don't have the else part in my code the writing into the SD card works without any problem. But when i have the else part and all those String variables the writing doesn't work. The if-else and the if for writing in the SD card are in the loop. And the writing on the SD card is controled by the button. Could you help me to figure out why by adding one part of the code, the other doesn't work ?

Thank You

unsigned char strpom[65];

Serial.readBytes(strpom, 65);
strpomForming(strpom);

pomString = String((char *)strpom);

if((pomString.substring(0, 2) != "[B")){
  lcd.setCursor(0, 0);
  lcd.print(" ");
  lcd.setCursor(1, 0);
  lcd.print(lcdStrPage1a);
  lcd.setCursor(0, 1);
  lcd.print("                    ");
  lcd.setCursor(0, 2);
  lcd.print(" ");
  lcd.setCursor(1, 2);
  lcd.print(lcdStrPage2a);
  lcd.setCursor(0, 3);
  lcd.print("                    ");
}else{          
    
    tempStr = pomString.substring(pomString.indexOf("[B") + 2, pomString.indexOf("[C");
    voltage = float(tempStr.toInt());
    voltage = voltage / 1000;
    
    AP = pomString.substring(pomString.indexOf("[C") + 1, pomString.indexOf("[E") - 1);
    DP = pomString.substring(pomString.indexOf("[E") + 1, pomString.indexOf("[D") - 1);
    if(pomString.indexOf("[V") == -1){
        FW = pomString.substring(pomString.indexOf("[M") + 2, pomString.indexOf(']'));
    }else{
        FW = pomString.substring(pomString.indexOf("[V") + 2, pomString.indexOf(']'));
    }
    
    AS = pomString.substring(pomString.indexOf("[D") + 1, pomString.indexOf("[A") - 1);
    DS = pomString.substring(pomString.indexOf("[A") + 1, pomString.indexOf("[G") - 1);
    GV = pomString.substring(pomString.indexOf("[G") + 2, pomString.indexOf("[S") - 1);
    pomGValue = GV.toInt();
        
    indexAcc = pomString.indexOf("[X");
    tempStr2 = pomString.substring(indexAcc + 2, indexAcc + 6);
    accX = (tempStr2.toInt());

    indexAcc = pomString.indexOf("[Y");
    tempStr2 = pomString.substring(indexAcc + 2, indexAcc + 6);
    accY = (tempStr2.toInt());

    indexAcc = pomString.indexOf("[Z");
    tempStr2 = pomString.substring(indexAcc + 2, indexAcc + 6);
    accZ = (tempStr2.toInt());

    if (abs(1500-accX)>abs(1500-accY)){
        if (abs(1500-accX)>abs(1500-accZ)){
            tempStr3 = String(accX);
        }else{
            tempStr3 = String(accZ);
        }
    }else{
        if (abs(1500-accY)>abs(1500-accZ)){
            tempStr3 = String(accY);
        }else{
            tempStr3 = String(accZ);
        }
    }
    lcd.setCursor(0, 0);
    lcd.print(" ");
    lcd.setCursor(1, 0);
    lcd.print(lcdStrPage1a);
    lcd.setCursor(0, 1);
    lcd.print("  ");
    if(voltage < 10.00){
        lcd.setCursor(2, 1);
        lcd.print((String)voltage);
    }else{
        lcd.setCursor(1, 1);
        lcd.print((String)voltage);
    }
    lcd.setCursor(6, 1);
    lcd.print("  " + AP + " " + DP + "  " + FW + "  ");
    lcd.setCursor(0, 2);
    lcd.print(" ");
    lcd.setCursor(1, 2);
    lcd.print(lcdStrPage2a);
    lcd.setCursor(0, 3);
    lcd.print("  ");
    lcd.setCursor(2, 3);                                                                   
    lcd.print(tempStr3);                    
    lcd.setCursor(6, 3);
    if(GV.toInt() > 90){
        lcd.print("  " + DS + " " + AS + " " + GV + "  ");
    }else{
        lcd.print("  " + DS + " " + AS + "  " + GV + "  ");
    }
}
if(SD.begin(chipSelect)){
     myFile = SD.open("testbox.txt", FILE_WRITE);
     myFile.println(pomString);
     myFile.close();
     onceWritten = 1;
}

I moved your topic to an appropriate forum category.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
What was it about the uncategorized were you having trouble understanding?

DO NOT USE! Please select the appropriate category for your topic. The sole purpose of this category is to help the forum maintainers to identify topics that were not correctly categorized by the author. When creating topics, please carefully select the category that is most appropriate for the subject matter. There is an "About the _____ category" topic at the top of each category that explains its purpose. To learn more about creating a forum topic, see the "How to get the best out of this …

Hi @jovanovic98,

please read and follow @Grumpy_Mike 's post!

Unless you are interested in starting a contest (Who is the best at finding the cause of malfunctioning software without knowing even a line of code) it is really helpful to post the sketch you are talking about. :wink:

Please place the code between code tags, see the gray symbol in the menu bar of the editor window:

image

I posted the details and the part of the code that is important. I'll be so pleased to here the reply from you.

Thank you

Wild guess - with all the string use when you try to use the SD support the buffers that are dynamically allocated exceed the ram memory size. On the other hand if the unnamed processor has sufficient memory then I need more information.

Show the memory use messages at the end of the compile.

Avoid using String objects on AVR-based Arduinos (Uno R3, classic Nano, Mega, etc.) as they lead to unpredictable program crashes.

Always post ALL the code.

Hi @jovanovic98 ,

would you mind to post the full sketch?

Having only a part of it makes it impossible to identify the reason for your problems.

E.g. it is not clear what is done in setup and what in loop() and there is no code that is related to a button (" the writing on the SD card is controlled by the button.").

Observations so far:

  • SD.begin(chipSelect) should normally be called in setup() and not in loop() ...
  • The use of the String class is not recommended for UNO, Nano or the like unless you are using an ESP32 which has plenty of memory. What controller do you use?
  • There is no need to write lcd.print((String)f);
    Print handles float already: lcd.print(f);

Hi everyone,

the problem was the memory leaking, like one of you noticed in the comments. I was using so many variables declaired as String type and i was using these substring and indexOf function, that were making all the trouble in the memory. I have written those functions by myself and i am using unsigned char types now and i have no more problems. Thank you for your help

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