Storing read data from Sd card (value up to a 16 bit int) in a variable

Hello Friends,

Good day. I am currently working on a project that is highly dependent on the values that are generated during operations, earing in mind that variables are stored in the RAM of the Arduino, which means they would be wiped out during an outage, I looked to using an SD card, I successfully wrote the data to the SD card, and now, I want to read it, but instead of printing over serial monitor, which worked with…

myStorage = SD.open("totRsec.txt");

  if (myStorage)
  {
    while (myStorage.available())
    {
      Serial.write(myStorage.read());
    }
    myStorage.close();
  }

…I want to store the read value to a variable and print on an LCD,

    lcd.setCursor(0, 1);
    lcd.print("T.R_Hr:" + String(_runhour));

so whenever there is a power outage (I have a power outage sensing circuit already), and power is restored, the last value before the outage is what is displayed on the LCD.

Below is the full code.

#include <SD.h>
#include <SPI.h>
#include <Adafruit_ADS1015.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

File myStorage;

Adafruit_ADS1115 ads;
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;
uEEPROMLib eeprom(0x57);
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};


bool recordState = false;
float amps;
//byte inpin = 3;
//byte instate = 0;
// declare a type for the various states of our machine
enum t_weldingState : uint8_t {WELDING_IDLE, WELDING_IN_USE};
//enum t_PowerState : uint8_t {POWER_ON, POWER_OFF};

// define a variable to keep track of current state and initialize as IDLE
t_weldingState weldingState  = WELDING_IDLE;
//t_PowerState PowerState = POWER_OFF;

// variables to keep track of start and end time + duration
uint32_t weldingStartTime, weldingEndTime;
uint32_t weldingDuration;

uint32_t _totalrunhour;
uint32_t _runhour;
uint32_t TotalRunHour;
uint32_t RunHour;

unsigned long startRecord;
unsigned long stopRecord;
const uint64_t recordTime = 3600000;

const int chipSelect = 4;

int powerInput = A1;
int powerState = 0;

//float amps;

//...

bool isNewWeldingTimeReady()
{
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print(now.timestamp(DateTime::TIMESTAMP_TIME));
  lcd.setCursor(9, 0);
  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);

  bool durationIsReady = false;
  int16_t results;

  results = ads.readADC_Differential_0_1();

  float amps = ((float)results * 256.0) / 32768.0;
  amps = amps * 6.6667;

  switch (weldingState) {
    case WELDING_IDLE: // we are waiting for the start of the process

      if (amps >= 50.00) {
        // welding started, record time and make note of state change
        weldingStartTime = now.unixtime();
        Serial.println(weldingStartTime);
        weldingState = WELDING_IN_USE;
      }
      break;

    case WELDING_IN_USE: // we are waiting for the end of the process
      int16_t results;

      results = ads.readADC_Differential_0_1();

      float amps = ((float)results * 256.0) / 32768.0;
      amps = amps * 6.6667;

      if (amps <= 10.00) {
        // welding ended, record time and make note of state change
        weldingEndTime = now.unixtime();
        Serial.println(weldingEndTime);
        weldingDuration = weldingEndTime - weldingStartTime;
        durationIsReady = true;
        weldingState = WELDING_IDLE;
      }
      break;
  }
  return durationIsReady;
  return amps;
}

int readSDdata()
{
  myStorage = SD.open("totRsec.txt");

  if (myStorage)
  {
    while (myStorage.available())
    {
      Serial.write(myStorage.read());
    }
    myStorage.close();
  }
  //  delay(20);

  //  myStorage = SD.open("Rsec.txt", FILE_WRITE);

  //  if (myStorage)
  //  {
  //    while (myStorage.available())
  //    {
  //      Serial.write(myStorage.read());
  //    }
  //    myStorage.close();
  //  }
  ////  return TotalRunHour;
  //  return RunHour;
}

void writeHourData ()
{
  myStorage = SD.open("totRhr.txt", FILE_WRITE);

  if (myStorage)
  {
    myStorage.println(_totalrunhour);
    Serial.println("Hour data written to SD");
    myStorage.close();
  }
  delay(20);
  myStorage = SD.open("R_hour.txt", FILE_WRITE);

  if (myStorage)
  {
    myStorage.println(_runhour);
    Serial.println("Hour data written to SD");
    myStorage.close();
  }
  delay(20);
}

void writeSecData()
{
  myStorage = SD.open("totRsec.txt", FILE_WRITE);

  if (myStorage)
  {
    myStorage.println(TotalRunHour);
    myStorage.close();
  }
  //  delay(20);

  myStorage = SD.open("Rsec.txt", FILE_WRITE);
  if (myStorage)
  {
    myStorage.println(RunHour);
    myStorage.close();
  }
}
void setup()
{
  Serial.begin(57600);
  ads.setGain(GAIN_SIXTEEN);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  pinMode(chipSelect, OUTPUT);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  //  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

  //  SD.begin(chipSelect);


  if (!SD.begin(4))
  {
    Serial.println("initialization failed!");
    while (1);
  } else {

    Serial.println("initialization done.");
  }

  myStorage = SD.open("totRsec.txt");

  if (myStorage)
  {
    while (myStorage.available())
    {
      Serial.write(myStorage.read());
    }
    myStorage.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

//  Serial.print(TotalRunHour);

  pinMode(powerInput, INPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("OEE meter");
  lcd.setCursor(0, 1);
  lcd.print("Version 1.0.0.2020.");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Designed by");
  lcd.setCursor(0, 1);
  lcd.print("");
  delay(2000);
  lcd.clear();
}

void loop()
{
  powerState = digitalRead(powerInput);
  startRecord = millis();

  // check the welding machine state on a regular basis to keep track of usage
  if (isNewWeldingTimeReady()) {


    if (RunHour / 3600 >= 8)
    {
      RunHour = 0;
    }
    RunHour += weldingDuration;
    TotalRunHour += weldingDuration;

    _runhour = RunHour / 3600;
    _totalrunhour = TotalRunHour / 3600;

    Serial.println(TotalRunHour / 3600);
    Serial.println(TotalRunHour);
    //    writeSecData();



    lcd.setCursor(0, 1);
    lcd.print("T.R_Hr:" + String(_runhour));
    //    lcd.setCursor(9, 1);
    //    lcd.print("R_Hr:" + String(_runhour));
  }

  if (startRecord - stopRecord >= recordTime)
  {
    writeHourData();
    stopRecord = startRecord;
  }



  // Power fail detection

  if (powerState == LOW)
  {
    writeSecData();

    //    lcd.setCursor(0, 0);
    //    lcd.print("POWER OUT");
    //    lcd.setCursor(0, 1);
    //    lcd.print("SAVING LAST DATA");
  }
}

Looking forward to your response.

Regards

You need to read in the entire number and then convert it from ascii ("12345") to the actual number

char line[40];
uint32_t value;
myStorage = SD.open("totRsec.txt");

  if (myStorage)
  {
    myStorage.read(line, sizeof(line));
    value = atol(line);
    myStorage.close();
  }
//...

blh64:
You need to read in the entire number and then convert it from ascii ("12345") to the actual number

char line[40];

uint32_t value;
myStorage = SD.open("totRsec.txt");

if (myStorage)
  {
    myStorage.read(line, sizeof(line));
    value = atol(line);
    myStorage.close();
  }
//...

Okay thanks, um, but for the write, I use...

myStorage.println(RunHour);

...would the above line of code work?

Yes, println() turn a number into the ascii representation of that value and then appends a newline so if value = 789, println(value) would produce “789\n” in the file. You would then read that into line and reverse the process with atol() which converts a string into its numerical value. The newline will be ignored.

Okay thanks