Problem with "snprintf"

Good evening everybody...

Being a Newbee, since 2 days (13 hours spread over 2 days), I was sitting in front of one coding line and still did not find a solution.
I have searched here and there on different forums, different languages, looked at examples but, nevertheless, I am still not able to find what causes my sketch to bug.

I have a working sketch which writes 3 random numbers on an SD card and a bit later reads them back to the serial monitor (IDE 2.3.2.).

It works fine...
But when I try to adapt the sketch into a much bigger sketch, (and replace 1 variable (the random number generator) in "snprintf"), it bugs, instead of writing 3 pre-calculated variables, it writes a set of 3 "0" (zeros).

Please find first my "working" sketch:

#include <SD.h>
void setup() {
  Serial.begin(115200);
  SD.begin(4);
  int A = 0;
  int AA = 0;
  int B = 0;
  int BB = -1;
  int C = 0;
  int CC = -2;
  
  char line[32];
  size_t line_length = 0;
  File file = SD.open("data.txt", FILE_WRITE | O_TRUNC);  // clean file on the SD card
 
      Serial.println("Reset SD card & write");
  if (file) {
    
    for (uint8_t i = 1; i <= 3; ++i) {
      line_length = snprintf(line, sizeof(line), "%5hi", (int16_t)random(32, 77));  //formatting the random numbers and saving them on the SD card
      Serial.print("Writing at position ");
      Serial.print(i);
      Serial.print(" : \"");
      Serial.print(line);
      Serial.println("\"");
      file.println(line);
    }
    file.close();      //close the SD card file
    line_length += 2;  // count characters "\r\n" added by println
  }
  Serial.println();  //blank line to separate save and read/distributing into variables
                     // Following reads all lines from file
  file = SD.open("data.txt");
  if (file) {
    while (file.available() >= line_length) {
      static uint8_t line_id = 0;
      file.seek(line_id * line_length);  // go to line

 //saving the 3 numbers in 3 different variables
      if (AA == 0) {
        A = file.parseInt();
        AA = AA + 1;
        BB = BB + 1;
        CC = CC + 1;
      }
      if (BB == 0) {
        B = file.parseInt();
        BB = BB + 1;
        CC = CC + 1;
      }
      if (CC == 0) {
        C = file.parseInt();
        CC = CC + 1;
      }
      file.read(line, line_length - 2);  // read line without "\r\n"
      line[line_length - 2] = '\0';      // make sure to add null character to make a valid C-string
    }
    file.close();  //close the SD card file

// show that there are 3 numbers in the 3 variables - corresponding to the random numbers at the beginning
    Serial.print("A = ");
    Serial.println(A);
    Serial.print("B = ");
    Serial.println(B);
    Serial.print("C = ");
    Serial.println(C);
  }
}
void loop() {
}

I know, it is not pretty...

Now, in line ...

  line_length = snprintf(line, sizeof(line), "%5hi", (int16_t)random(32, 77));  //formatting the random numbers and saving them on the SD card

... I'd like to replace " (int16_t)random(32, 77) " by "kilos[i - 1]", where "i" is ordered by "for (uint8_t i = 1; i <= 3; ++i)" and makes "kilos" go from 0 to 2. (= 3 variables)
Note that "kilos" is being declared as "double kilos[3];" in my bigger sketch... and is a 2 - 5 digit integer ... 15253 or 57231 or 53 or 125 or ...

Hence, the new line would look as follows:

 line_length = snprintf(line, sizeof(line), "%5hi", kilos[i - 1]);  //formatting the random numbers and saving them on the SD card

Somehow, when replacing "(int16_t)random(32, 77)" by "kilos[i - 1]" everything stops working.

I know, I miss something... but I do not seem to be able to find what...
So...
Please, if someone can spare 10 minutes for me, it would be great...

Thanks in advance,

Eric

Please post the complete code that is causing the problem, using code tags.

Somehow, when replacing "(int16_t)random(32, 77)" by "kilos[i - 1]" everything stops working.

Please describe what actually happens. "Stops working" doesn't give any useful information.

Ok, as requested...
but it is really not pretty to look at... (nothing is finished)

#include <SD.h>
const int buttonPin = 8;
int buttonState = 0;
int currentPins[3] = { 1, 2, 3 };          //Assign phase CT inputs to analog pins
double calib[3] = { 0.716, 0.87, 0.732 };  //{ L1, L2, L3 };
//double calib[3] = { 0.616, 0.77, 0.632 };
//double calib[3] = { 1.00, 1.2, 1.02};  //{ L1, L2, L3 };
double kilos[3];
double kJule[3];
double price[3];
float lowtariffprice = 3.2484;
float mediumtariffprice = 4.2218;
float hightariffprice = 4.4217;
unsigned long startMillis[3];
unsigned long endMillis[3];
double RMSCurrent[3];
float RMSPower[3];
float peakPower[3];
int kilostotal = 0;
int pay = 0;
int count = 370;
int X = 0;
int AA = 0;
int Y = 0;
int BB = -1;
int Z = 0;
int CC = -2;
int eleccut = 0;
float lowtariff = 0;
float mediumtariff = 0;
float hightariff = 0;
int low = 0;
int medium = 0;
int high = 0;
size_t line_length = 0;
char line[32];
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27, 20, 4);
byte three[8] = {
  B00000,
  B00000,
  B11000,
  B00100,
  B01000,
  B00100,
  B11000,
  B00000,
};
byte two[8] = {
  B00000,
  B00000,
  B11000,
  B00100,
  B01000,
  B10000,
  B11100,
  B00000,
};
byte one[8] = {
  B00000,
  B00000,
  B11000,
  B01000,
  B01000,
  B01000,
  B11100,
  B00000,
};
byte baht[8] = {
  B00100,
  B11110,
  B10101,
  B11110,
  B10101,
  B10101,
  B11110,
  B00100,
};
void setup() {
  Serial.begin(115200);
  SD.begin(4);
  lcd.begin(20, 4);  // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.init();        //initialize the lcd
  lcd.backlight();   //open the backlight  //
  lcd.createChar(1, one);
  lcd.createChar(2, two);
  lcd.createChar(3, three);
  lcd.createChar(4, baht);
  pinMode(8, INPUT_PULLUP);
  lcd.clear();
  lcd.setCursor(6, 1);  // set cursor to column 0, row 0 (the first row)
  lcd.print("3 Phase");
  lcd.setCursor(4, 2);
  lcd.print("Energy Meter");
  delay(2000);
  size_t line_length = 0;
  //File file = SD.open("DATA.TXT", FILE_WRITE | O_TRUNC);  // clean file on the SD card
}
void readPhase()  //Method to read information from CTs
{
  for (int i = 0; i <= 2; i++) {
    float current = 0;
    float maxCurrent = 0;
    float minCurrent = 1000;
    for (int j = 0; j <= 200; j++)  //Monitors and logs the current input for 200 cycles to determine max and min current
    {
      current = analogRead(currentPins[i]);  //Reads current input and records maximum and minimum current
      if (current >= maxCurrent)
        maxCurrent = current;
      else if (current <= minCurrent)
        minCurrent = current;
    }

    if (maxCurrent <= 515) {
      maxCurrent = 514;
    }
    RMSCurrent[i] = (((maxCurrent - 514) * 0.70710678118) / calib[i]);  //Calculates RMS current based on maximum value and scales according to calibration

    RMSPower[i] = 210 * RMSCurrent[i];  //Calculates RMS Power Assuming Voltage 220VAC, change to 110VAC accordingly
    if (RMSPower[i] > peakPower[i]) {
      peakPower[i] = RMSPower[i];
    }
    endMillis[i] = millis();
    unsigned long time = (endMillis[i] - startMillis[i]);
    //kilos[i] = kilos[i] + (float(RMSPower[i]) * float(time / 60 / 60 / 1000000));  //Calculate kilowatt hours use
    kilos[i] = kilos[i] + (float(RMSPower[i]) * (float(time / 3600000)));  //Calculate kilowatt hours use (float(time / 60 / 60 / 1000))

    kJule[i] = kJule[i] + (float(RMSPower[i]) * (float(time)) / 1000000);
    //Serial.println(time);
    //Serial.print("RMSPower[1] ");
    //Serial.println(RMSPower[1]);
    //Serial.print("RMSPower[2] ");
    //Serial.println(RMSPower[2]);
    //Serial.print("RMSPower[3] ");
    //Serial.println(RMSPower[3]);
    //Serial.print("kilos[1] ");
    //Serial.println(kilos[1]);
    //Serial.print("kilos[2] ");
    //Serial.println(kilos[2]);
    //Serial.print("kilos[3] ");
    //Serial.println(kilos[3]);
    //Serial.print("kJules[1] ");
    //Serial.println(kJule[1]);
    //Serial.print("kJules[2] ");
    //Serial.println(kJule[2]);
    //Serial.print("kJules[3] ");
    //Serial.println(kJule[3]);


    startMillis[i] = millis();
  }
}
void loop()  //Calls the methods to read values from CTs and changes display
{
  readPhase();
  displayKilowattHours();
  delay(1800);
  readPhase();
  displayKJules();
  delay(1800);
  //readPhase();
  //displayCurrent();
  //delay(1800);
  readPhase();
  displayRMSPower();
  delay(1800);
  readPhase();
  displayPeakPower();
  delay(1800);
  readPhase();
  displayTotals();
  delay(1800);
  readPhase();
  displaysavedata();
  delay(1800);
  readPhase();
  displayeleccut();
  delay(1700);

  //displaypressbutton();
  //delay(1700);
}
void displayKilowattHours()  //Displays all kilowatt hours data
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("****** Energy ******");
  lcd.setCursor(0, 1);
  lcd.print("L"
            "\1"
            " ");
  lcd.print(kilos[0]);
  lcd.print(" kW/h");
  lcd.setCursor(0, 2);
  lcd.print("L"
            "\2"
            " ");
  lcd.print(kilos[1]);
  lcd.print(" kW/h");
  lcd.setCursor(0, 3);
  lcd.print("L"
            "\3"
            " ");
  lcd.print(kilos[2]);
  lcd.print(" kW/h");
}
void displayKJules()  //Displays all kilowatt hours data
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("**** Kilojules *****");
  lcd.setCursor(0, 1);
  lcd.print("L"
            "\1"
            " ");
  lcd.print(kJule[0]);
  lcd.print(" kJ/h");
  lcd.setCursor(0, 2);
  lcd.print("L"
            "\2"
            " ");
  lcd.print(kJule[1]);
  lcd.print(" kJ/h");
  lcd.setCursor(0, 3);
  lcd.print("L"
            "\3"
            " ");
  lcd.print(kJule[2]);
  lcd.print(" kJ/h");
}
//void displayCurrent()  //Displays all current data
//{
//lcd.clear();
//lcd.setCursor(0, 0);
//lcd.print("**    Current");
//lcd.setCursor(0, 1);
//lcd.print("L"
//          "\1"
//           " ");
//lcd.print(RMSCurrent[0]);
// lcd.print(" A");
//lcd.setCursor(0, 2);
// lcd.print("L"
//          "\2"
//          " ");
// lcd.print(RMSCurrent[1]);
// lcd.print(" A");
// lcd.setCursor(0, 3);
// lcd.print("L"
//           "\3"
//          " ");
// lcd.print(RMSCurrent[2]);
// lcd.print(" A");
//}
void displayRMSPower()  //Displays all RMS power data
{
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("****** Power *******");
  lcd.setCursor(0, 1);
  lcd.print("L"
            "\1"
            " ");
  lcd.print(RMSPower[0]);
  lcd.print(" W");
  lcd.setCursor(0, 2);
  lcd.print("L"
            "\2"
            " ");
  lcd.print(RMSPower[1]);
  lcd.print(" W");
  lcd.setCursor(0, 3);
  lcd.print("L"
            "\3"
            " ");
  lcd.print(RMSPower[2]);
  lcd.print(" W");
}
void displayPeakPower()  //Displays all peak power data
{
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("**** Max  Power ****");
  lcd.setCursor(0, 1);
  lcd.print("L"
            "\1"
            " ");
  lcd.print(peakPower[0]);
  lcd.print(" W");
  lcd.setCursor(0, 2);
  lcd.print("L"
            "\2"
            " ");
  lcd.print(peakPower[1]);
  lcd.print(" W");
  lcd.setCursor(0, 3);
  lcd.print("L"
            "\3"
            " ");
  lcd.print(peakPower[2]);
  lcd.print(" W");
}
void displayTotals()  //Displays all peak power data
{
  lcd.clear();
  kilostotal = kilos[0] + kilos[1] + kilos[2];
  if (kilostotal < 150) {
    lowtariff = kilostotal;
    low = 1;
    medium = 0;
    high = 0;
  }
  if (kilostotal - 150 > 0) {
    lowtariff = 150;
    mediumtariff = kilostotal - 150;
    low = 1;
    medium = 1;
    high = 0;
  }
  if (kilostotal - 150 - 250 > 0) {
    lowtariff = 150;
    mediumtariff = 250;
    hightariff = kilostotal - 150 - 250;
    low = 1;
    medium = 1;
    high = 1;
  }
  pay = (lowtariff * 3.2484 * low) + (mediumtariff * 4.2218 * medium) + (hightariff * 4.4217 * high);
  if (pay < 0) {
    pay = 0;
  }
  lowtariff = 0;
  mediumtariff = 0;
  hightariff = 0;
  low = 0;
  medium = 0;
  high = 0;
  lcd.setCursor(0, 0);
  lcd.print(" Total consumption");
  lcd.setCursor(5, 1);
  lcd.print("L"
            "\1"
            "+ L"
            "\2"
            "+ L"
            "\3");
  lcd.setCursor(6, 2);
  lcd.print(kilostotal);
  lcd.print(" KW/h");
  lcd.setCursor(7, 3);
  lcd.print(pay);
  lcd.print(" "
            "\4");
}

void displaysavedata() {

  Serial.println(count);
  lcd.clear();

  lcd.setCursor(2, 0);
  lcd.print(" Automatically");
  lcd.setCursor(1, 1);
  lcd.print("Backing up Data in");
  lcd.setCursor(5, 2);
  lcd.print(376 - count);
  lcd.print(" cycles");

  if (count == 373) {
    count = 0;
    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("Saving Data");
    //File file = SD.open("DATA.TXT", FILE_WRITE);
    //if (file) {
    //  for (uint8_t i = 1; i <= 3; ++i) {
    //   line_length = snprintf(line, sizeof(line), "%5hi", kilos[i - 1]);  //formatting the numbers and saving them on the SD cardtxt
    //   SD.remove("DATA.TXT");
    //   File file = SD.open("DATA.TXT", FILE_WRITE);
    //   file.println(line);


    //File file = SD.open("DATA.TXT", FILE_WRITE | O_TRUNC);  // clean file on the SD card
    File file = SD.open("DATA.TXT", FILE_WRITE);
    file = SD.open("DATA.TXT");
    if (file) {

      for (uint8_t i = 1; i <= 3; ++i) {
        line_length = snprintf(line, sizeof(line), "%5hi", (int16_t)random(500, 600));  //formatting the random numbers and saving them on the SD card
        Serial.print("Writing at position ");
        Serial.print(i);
        Serial.print(" : \"");
        Serial.print(line);
        Serial.println("\"");
        file.println(line);
      }
      file.close();      //close the SD card file
      line_length += 2;  // count characters "\r\n" added by println



      //file.close();      //close the SD card file
      //line_length += 2;  // count characters "\r\n" added by println
      //lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" SAVED ALL DATA");
      //Serial.println("SAVED ALL DATA");  //blank line to separate save and read/distributing into variables
      // Following reads all lines from file
      delay(1000);
      // file = SD.open("DATA.TXT");
      //  if (file) {
      //  while (file.available() >= line_length) {
      //    static uint8_t line_id = 0;
      //   file.seek(line_id * line_length);  // go to line saving the 3 numbers in 3 different variables
      //  if (AA == 0) {
      //    A = file.parseInt();
      //    AA = AA + 1;
      //    BB = BB + 1;
      //    CC = CC + 1;
      //  }
      //  if (BB == 0) {
      //    B = file.parseInt();
      //     BB = BB + 1;
      //      CC = CC + 1;
      //     }
      //    if (CC == 0) {
      //     C = file.parseInt();
      //      CC = CC + 1;
      //    }
      //    file.read(line, line_length - 2);  // read line without "\r\n"
      //    line[line_length - 2] = '\0';      // make sure to add null character to make a valid C-string
      //    }
      //   file.close();  //close the SD card file show that there are 3 numbers in the 3 variables
      AA = 0;
      BB = -1;
      CC = -2;
    }
    file = SD.open("DATA.TXT", FILE_READ);
    Serial.print("A");
    //file = SD.open("DATA.TXT");
    if (file) {
      while (file.available() >= line_length) {
        static uint8_t line_id = 0;
        file.seek(line_id * line_length);  // go to line

        Serial.print("E");
        if (AA == 0) {
          Serial.print("F");
          X = file.parseInt();
          Serial.print("G");
          kilos[0] = X;


          AA = AA + 1;
          BB = BB + 1;
          CC = CC + 1;
        }
        if (BB == 0) {
          Serial.print("H");
          Y = file.parseInt();
          kilos[1] = Y;

          BB = BB + 1;
          CC = CC + 1;
        }
        if (CC == 0) {
          Serial.print("I");
          Z = file.parseInt();
          kilos[2] = Z;

          CC = CC + 1;
          Serial.print("J");
        }
      }
      Serial.print("K");

      Serial.print("L");
      file.close();
      Serial.print("M");
    }
  } else {
    Serial.print("N");
    count = count + 1;
  }
  Serial.print("O");
}




//void displaysavedata() {
//Serial.println(count);
// lcd.clear();

//lcd.setCursor(2, 0);
// lcd.print(" Automatically");
// lcd.setCursor(1, 1);
/// lcd.print("Backing up Data in");
/// lcd.setCursor(5, 2);
// lcd.print(376 - count);
// lcd.print(" cycles");


// if (count == 376) {
//   count = 0;
//   lcd.clear();

//   lcd.setCursor(0, 0);
//   lcd.print("Saving Data");
//   File file = SD.open("DATA.TXT", FILE_WRITE | O_TRUNC);  // clean file on the SD card

//   if (file) {

//    for (uint8_t i = 1; i <= 3; ++i) {
//       Serial.println(kilos[i - 1]);
//        line_length = snprintf(line, sizeof(line), "%5hi", kilos[i - 1]);  //formatting the random numbers and saving them on the SD card
//       file.println(line);
//       lcd.setCursor(0, i);
//        lcd.print("Data saved ");
//      lcd.print(kilos[i - 1]);
//       Serial.print("Writing ");
//      Serial.print(i);
//       Serial.print(": ");
//       Serial.println(kilos[i - 1]);
//     }
//      file.close();      //close the SD card file
//     line_length += 2;  // count characters "\r\n" added by println
//   }
///   //File file = SD.open("DATA.TXT");

//    file = SD.open("DATA.TXT");
//   if (file) {
//     //while (file.available() >= line_length) {
//     while (file.available() >= 3) {
//       static uint8_t line_id = 0;
//       file.seek(line_id * line_length);

//        if (AA == 0) {
//
//          A = file.parseInt();
//          kilos[0] = A;


//          AA = AA + 1;
//         BB = BB + 1;
//         CC = CC + 1;
//       }
//       if (BB == 0) {

//         B = file.parseInt();
//          kilos[1] = B;

//          BB = BB + 1;
//         CC = CC + 1;
//       }
//       if (CC == 0) {

//        C = file.parseInt();
//         kilos[2] = C;
//
//         CC = CC + 1;
//      }


//       file.close();

//     delay (1000);
//     lcd.clear();
//     //lcd.setCursor(3, 0);
//lcd.print("DATA retrieved");
//     lcd.setCursor(0, 1);
//     lcd.print("Saved @ 1 ");
//     lcd.print(kilos[0]);

//     lcd.setCursor(0, 2);
//    lcd.print("Saved @ 2 ");
//     lcd.print(kilos[1]);

//     lcd.setCursor(0, 3);
//     lcd.print("Saved @ 3 ");

//    lcd.print(kilos[2]);
//   }
// }

//  } else {
//    count = count + 1;
// }
//}

//lcd.clear();
//if (count == 373) {
//  count = 0;
//  File file = SD.open("DATA.TXT", FILE_WRITE);
//   if (file) {
//    for (uint8_t i = 1; i <= 3; ++i) {
//     line_length = snprintf(line, sizeof(line), "%5hi", kilos[i - 1]);  //formatting the numbers and saving them on the SD card
//     SD.remove("DATA.TXT");
//    File file = SD.open("DATA.TXT", FILE_WRITE);
//    file.println(line);
//  }
//  file.close();      //close the SD card file
//   line_length += 2;  // count characters "\r\n" added by println
//   lcd.clear();
//   lcd.setCursor(0, 0);
//   lcd.print(" SAVED ALL DATA");
//  //Serial.println("SAVED ALL DATA");  //blank line to separate save and read/distributing into variables
// Following reads all lines from file
//  delay(1000);
// file = SD.open("DATA.TXT");
//  if (file) {
//  while (file.available() >= line_length) {
//    static uint8_t line_id = 0;
//   file.seek(line_id * line_length);  // go to line saving the 3 numbers in 3 different variables
//  if (AA == 0) {
//    A = file.parseInt();
//    AA = AA + 1;
//    BB = BB + 1;
//    CC = CC + 1;
//  }
//  if (BB == 0) {
//    B = file.parseInt();
//     BB = BB + 1;
//      CC = CC + 1;
//     }
//    if (CC == 0) {
//     C = file.parseInt();
//      CC = CC + 1;
//    }
//    file.read(line, line_length - 2);  // read line without "\r\n"
//    line[line_length - 2] = '\0';      // make sure to add null character to make a valid C-string
//    }
//   file.close();  //close the SD card file show that there are 3 numbers in the 3 variables
//}
//  }
// } else {
//   count = count + 1;
// }
//}

void displayeleccut() {
  if (eleccut < 2) {  //if there was a power cut, wait for power to stabilise (10 cycles), recall data on SD and then start normally
    eleccut = eleccut + 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("**** Power  Cut ****");
    lcd.setCursor(0, 1);
    lcd.print("     Please  Wait");
    lcd.setCursor(0, 2);
    lcd.print("     for a minute");
    lcd.setCursor(3, 3);
    lcd.print("   loop ");
    lcd.print(eleccut);
    lcd.print(" / 10");
    delay(1000);
  }
  if (eleccut == 2) {  //if there was a power cut, wait for power to stabilise (10 cycles), recall data on SD and then start normally
    eleccut = 3;
    AA = 0;
    BB = -1;
    CC = -2;
    File file = SD.open("DATA.TXT", FILE_READ);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" Power Cut");
    lcd.setCursor(0, 1);
    lcd.print("  RETRIEVING DATA");
    lcd.setCursor(0, 2);
    lcd.print(" FROM MICRO SD CARD");

    file = SD.open("DATA.TXT");
    if (!file) {
      //while (file.available() >= line_length) {
      while (file.available() >= 3) {

        static uint8_t line_id = 0;
        file.seek(line_id * line_length);
                file.read(line, line_length - 2); 

        if (AA == 0) {

          X = file.parseInt();
          kilos[0] = X;


          AA = AA + 1;
          BB = BB + 1;
          CC = CC + 1;
        }
        if (BB == 0) {

          Y = file.parseInt();
          kilos[1] = Y;

          BB = BB + 1;
          CC = CC + 1;
        }
        if (CC == 0) {

          Z = file.parseInt();
          kilos[2] = Z;

          CC = CC + 1;
        }
        //file.read(line, line_length - 2);  // read line without "\r\n"
        //line[line_length - 2] = '\0';      // make sure to add null character to make a valid C-string
      }
      file.close();  //close the SD card file
    

  
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("DATA retrieved");
    lcd.setCursor(0, 1);
    lcd.print("L"
              "\1"
              " ");
    lcd.print(kilos[0]);
    lcd.print(" KW/h");
    lcd.setCursor(0, 2);
    lcd.print("L"
              "\2"
              " ");
    ;
    lcd.print(kilos[1]);
    lcd.print(" KW/h");
    lcd.setCursor(0, 3);
    lcd.print("L"
              "\3"
              " ");
    lcd.print(kilos[2]);
    lcd.print(" KW/h");
    delay(1800);
  }
}
}

void displaypressbutton() {
  if (eleccut > 5) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("5"
              ")"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff");
    lcd.setCursor(0, 1);
    lcd.print("\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              " RESET ? "
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff");
    lcd.setCursor(0, 2);
    lcd.print("\xff"
              " Press Button NOW "
              "\xff");
    lcd.setCursor(0, 3);
    lcd.print("\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff"
              "\xff");



    buttonState = digitalRead(buttonPin);
    if (!buttonState == HIGH) {  //create blank data file
      lcd.clear();
      Serial.println("Reset SD card");
      delay(1000);
      File file = SD.open("DATA.TXT", FILE_WRITE | O_TRUNC);  // clean file on the SD card
      Serial.println("file erased");
      file.close();
      file = SD.open("DATA.TXT", FILE_WRITE);
      if (file) {
        for (uint8_t i = 1; i <= 3; ++i) {
          line_length = snprintf(line, sizeof(line), "%5hi", kilos[i - 1]);  //formatting the numbers and saving them on the SD card
          SD.remove("DATA.TXT");
          File file = SD.open("DATA.TXT", FILE_WRITE);
          file.println(line);
        }
        file.close();
        line_length += 2;
        lcd.setCursor(0, 0);
        lcd.print(" SD card reset OK");

        kilos[0] = 0;
        kilos[1] = 0;
        kilos[2] = 0;
        peakPower[0] = 0;
        peakPower[1] = 0;
        peakPower[2] = 0;
        file = SD.open("DATA.TXT");
        if (file) {
          while (file.available() >= line_length) {
            static uint8_t line_id = 0;
            file.seek(line_id * line_length);
            if (AA == 0) {
              X = file.parseInt();
              AA = AA + 1;
              BB = BB + 1;
              CC = CC + 1;
            }
            if (BB == 0) {
              Y = file.parseInt();
              BB = BB + 1;
              CC = CC + 1;
            }
            if (CC == 0) {
              Z = file.parseInt();
              CC = CC + 1;
            }
            file.read(line, line_length - 2);
            line[line_length - 2] = '\0';
          }
          //file.close();
        }
      }
    }
  }
}


double is a double-precision floating point number, not an integer.

Which Arduino board are you using? Most do not support floating point numbers with snprintf().

Try casting the float to (int16_t).

I have a NANO...
But it seems to eat the "double kilos"

Further,
note that I updated the sketch (above)...

Thanks again for your time to help me solve my problem

double kilos[3];

-> int16_t

Something like this...?

line_length = snprintf(line, sizeof(line), "%5hi", int16_t (kilos[i - 1]));

PLS NOTE We have a thunderstorm now...
I close down my stuff and come back later...

No.

If by "NANO" you mean a classic Arduino Nano, then snprintf() does not support float or double. Use dtostrf() instead to format those types.

This should work for printing the number as an integer, but you will lose any fractional part of the number. If you want to actually print the decimal places, then see the post by @jremington about dtostrf().

          line_length = snprintf(line, sizeof(line), "%5hi", (int16_t)kilos[i - 1]);  //formatting the numbers and saving them on the SD card

My first thought was about random numbers, a topic you better go into deep. It isn't your real problem (other are replying to you already) but I suggest you to start creating a better random numbers generation.

I'm saying this because I see you're here using "random()" function without a "seed": do you know how (pseudo) random numbers generators work?

You firstly need to always use "randomSeed()" function with an integer value as "seed", used by random generator algorythm. Starting with the same seed will result in exactly the same (pseudo) random numbers sequence.

Common solution is to use as seed the value of "analogRead()" from any unused and not connected analog pin, because unconnected pin is "floating", its value can be almost random due to electromagnetic interferences and fluctuations.

But that solution can give not more than 1024 possible pseudo-random sequences, not more (in realty, less that a few hundreds), making it not really a good "randomic" method.

On some projects I used a minimal "mixed" approach, combining at least two analogRead() values like this:

  unsigned long seed0 = analogRead(A0);
  delay(10);
  randomSeed(seed0 + analogRead(A0) * 1024UL);

But for better results, I used a loop to get more "randomic" seeds, check this test/example code out:

void setup()
{
  Serial.begin(9600);
  unsigned long vMin = 1000000;
  unsigned long vMax = 0;
  for(int n=1; n<= 100; ++n) {
    unsigned long seed = 0;
    for(int i=1; i<=5; ++i) {
      unsigned long seed1 = analogRead(A0);
      delay(10);
      unsigned long seed2 = analogRead(A0);
      delay(10);
      seed = seed1 + seed2*128*i;      
    }
    if (seed < vMin) vMin = seed;
    if (seed > vMax) vMax = seed;
    Serial.print("seed(");Serial.print(n);Serial.print(") = ");Serial.println(seed);
    randomSeed(seed);
  }
  Serial.println();
  Serial.print("min=");Serial.print(vMin);
  Serial.print(" max=");Serial.println(vMax);
}
 
void loop()
{

}

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