Datalogging issue

I have created a display based datalogger which will take logging interval from user in INT and then log the data according to the logging interval in the sd card.

There are 2 files on sd card.

  1. Log file(in which data is going to get logged)
  2. Settings file(it stored various settings ex. high range, low range, log time, etc)

In my code, settings file is successfully read and written and everything works perfect.

The problem lies in the logging code, it creates the blank file but doesnt write anything in it. My logic is :

  1. Open settings file.
  2. Read the log time value from it.
  3. Convert INT logtime to UNSIGNED LONG interval. Typecasting.
  4. Compare the last time and millis with interval and write the data in file.

Kindly guide me.

Code :

unsigned long lastLogWriteTimeMs = 0 ;
int logtime = 0;

void setup() 
{
  Wire.begin();//needed in code
  RTC.begin();//needed in code
  lcd.begin(16, 2);
  initializeSD(); 
}
                  
void loop() 
{
  
  
  btn_push = ReadKeypad();
  MainMenuBtn();
    if(btn_push == 'S')//enter selected menu
    {
        WaitBtnRelease();
        switch (mainMenuPage)
        {
            case 1:
              MenuA();//sd card logging in this loop
              break;
            case 2:
              MenuB();
              break;
            case 3:
              MenuC();
              break;
            case 4:
              MenuD();
              break;
            case 5:
              MenuE();
              break;
            case 6:
              MenuF();
              break; 
            case 7:
              MenuG();
              break;
            case 8:
              MenuH();//log time selection in this loop.
              break;  
        }

          MainMenuDisplay();
          WaitBtnRelease();
    }
    
     delay(10);
}

void MenuA()
{  
         
            while(ReadKeypad()!= 'L')
            {
                //Insert Task for Menu A here
                  String temperature;
                  String dateEntry;

                  pinMode(relaypin, OUTPUT);
                  pinMode(compressorPin, OUTPUT);

                  temperature = String(temp,1);
                  entryId = String(id);
                  dateEntry = DateLogEntry();
                  String entry = dateEntry+":00; "+temperature+"; ";
                  lcd.setCursor(0, 0);
                  lcd.print(dateEntry);
                  livereading();
                  relay(); //normal relay on/off
                  compress(); //compressor settings

                  readSDSettings();
                  unsigned long interval = (unsigned long)logtime;
                  interval = interval*60000;
                  if ( millis() - lastLogWriteTimeMs > interval )
                  {
                   writeEntryToFile(entry);// Write to data logger here
                   lastLogWriteTimeMs = millis() ;
                  }
            }
}

void MenuH()
{  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Set Log Time");
    readSDSettings();
    logtimenew = logtime; 
    while(ReadKeypad()!= 'L')
    {
                                        //                       
                                          WaitBtnRelease();
                                          if(keypad_value < 200 && keypad_value > 100)//Up
                                          {
                                            logtimenew++;
                                          }
                                          
                                          if(keypad_value < 400 && keypad_value > 200)//Down
                                          {
                                            logtimenew--;
                                          }

                                          delay(50);
                                          lcd.setCursor(0,1);
                                          lcd.print(logtimenew);
                                          lcd.print("           ");

                                          if(keypad_value < 800 & keypad_value > 600)//Select/Enter
                                          {
                                            logtime = logtimenew;
                                            writeSDSettings();
                                            lcd.setCursor(0,1);
                                            lcd.print("Logtime Set");                                              
                                            delay(1000);
                                          }
       
    }
}

int openFileToWrite(char filename[])
{
 myFile = SD.open(filename, FILE_WRITE);
}

int writeToFile(String text)
{
  if (myFile)
  {
    myFile.println(text);
    return 1;
  } else
  {
    return 0;
  }
}

void closeFile()
{
  if (myFile)
  {
    myFile.close();
  }
}

void writeEntryToFile(String entry)
{
  openFileToWrite("log.txt");
  writeToFile(entry);
   closeFile();
}

This code was working previously when i state 60000( for 1 min log time) instead of interval in if statement.

Don't post code snippets, we can't really make educated comments, just guesses so everyone looses time. Indentation of your code is weird

I'd use 60000[color=red]ul[/color] just for the sake of clarity

i will try to post the relevent code. The main program is quite long, i will post it within a day.

OK you could also print exactly what your read for logtime from your settings, before converting to unsigned long.

before converting to unsigned long.

OP is not converting anything to unsigned long. OP is casting some undefined type variable to unsigned long. Whether that makes any sense, or not, remains to be seen.

PaulS:
OP is not converting anything to unsigned long. OP is casting some undefined type variable to unsigned long. Whether that makes any sense, or not, remains to be seen.

well that's a bit playing on words... according to Converting - definition of Converting by The Free Dictionary

con·vert (kən-vûrt′)
v. con·vert·ed, con·vert·ing, con·verts
v.tr.
...
4. To exchange for something of equal value: convert assets into cash.

I think we all understand what he meant and according to what OP did state

  1. Convert INT logtime to UNSIGNED LONG interval. Typecasting.

He is recognizing that this is typecasting at work in the way he approached it.

We need to take his word of course that logtime was indeed an int and not a string for example

We need to take his word of course that logtime was indeed an int and not a string for example

No, we need to see his code, so that we can be certain that the cast IS valid. In general, I do not like to see explicit casts when there is an implicit cast that will be performed to promote one type to another, where such a cast makes sense. Seeing an explicit cast is a warning, to me, to look at the types involved, to see whether the user is just circumventing a compiler complaint. While a string can be cast as an unsigned long, to allow storing something in the variable whose type is unsigned long, doing so makes no sense. "thirtysevenmillion" will not become 37000000 because of a cast.

agreed - asked for getting away form snippets as well.

siren215:
There are 2 files on sd card.

Of which only one can be open at a time?

Nick_Pyner:
Of which only one can be open at a time?

If you have enough memory, any number of files can be open at any time. 512 bytes are needed for each open file. On the 328-based Arduinos, that typically means not more than 2 files.

The relevant code is added. I tried to set the logtime to 2 and briefly tried to display the interval on lcd for 1 sec. It came out 120000 which is correct. Means the calculations are right, just it is not going in the logging loop.

The relevant code is added.

Where?

In the original first post.

siren215:
In the original first post.

so for someone coming in now to the thread, you made the whole discussion totally weird and useless... never do that... Post updated code as you progress in a new post...

Sorry my bad..will remember next time. Please take your time to go through the code and guide me.

code in first post does not even compile...

J-M-L:
code in first post does not even compile...

Ofcource it won't compile. My code compiled successfully and i have post just the relevent part of the code which is needed for datalogging issue.

How do you know this is what is needed?

J-M-L:
How do you know this is what is needed?

Because the same lines of code had worked earlier when i manually set the interval time in the code itself to 60000.same doesnt work when i put INTERVAL variable.