Guidance to improve my code

Hi everyone,

I'm working on this project (a downtime tracker for one of my machine) utilizing NetDevilz PLX-DAQ v2.

My arduino has a simple LDR sensor attached to it with an RTC DS3231 module with it.

It records every second and with the help of PLX DAQ v2, it saves a .csv file into my personal computer every 15 minutes.

In order for it to save every 15 minutes, I set my code as so:

      if ((now.dayOfTheWeek() == 5 && seconds >= 46800 && seconds <= 52199) || (now.dayOfTheWeek() == 0 && seconds >= 52200 && seconds <= 80999))  {

      }

      else {


        if (seconds == 81899) {  // save at 10:44.59pm

          //Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()));
          //Serial.println("CLEARDATA");

          if ((future.day()) > (now.day())) {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (future.day()) + "_01" );
          }
          else {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (future2.month()) + "-" + (future.day()) + "_01" );
          }
          Serial.println("CLEARDATA");

        }

        if (seconds == 82799) {  // save at 10:59.59pm

          if ((future.day()) > (now.day())) {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (future.day()) + "_02" );
          }
          else {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (future2.month()) + "-" + (future.day()) + "_02" );
          }
          Serial.println("CLEARDATA");
        }


        if (seconds == 83699) {  // save at 11:14.59pm

          if ((future.day()) > (now.day())) {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (future.day()) + "_03" );
          }
          else {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (future2.month()) + "-" + (future.day()) + "_03" );
          }
          Serial.println("CLEARDATA");
        }

        if (seconds == 84599) {  // save at 11:29.59pm

          if ((future.day()) > (now.day())) {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (future.day()) + "_04" );
          }
          else {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (future2.month()) + "-" + (future.day()) + "_04" );
          }
          Serial.println("CLEARDATA");
        }

        if (seconds == 85499) {  // save at 11:44.59pm

          if ((future.day()) > (now.day())) {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (future.day()) + "_05" );
          }
          else {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (future2.month()) + "-" + (future.day()) + "_05" );
          }
          Serial.println("CLEARDATA");
        }

        if (seconds == 86399) {  // save at 11:59.59pm

          if ((future.day()) > (now.day())) {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (future.day()) + "_06" );
          }
          else {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (future2.month()) + "-" + (future.day()) + "_06" );
          }
          Serial.println("CLEARDATA");
        }


        if (seconds == 899) {  // save at 12:14.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_07" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 1799) {  // save at 12:29.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_08" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 2699) {  // save at 12:44.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_09" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 3599) {  // save at 12:59.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_10" );
          Serial.println("CLEARDATA");
        }


        if (seconds == 4499) {  // save at 01:14.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_11" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 5399) {  // save at 01:29.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_12" );
          Serial.println("CLEARDATA");
        }


        /////////////////////
        //MAKAN TIME 1:30AM - 2:00AM
        ////////////////////


        if (seconds == 8099) {  // save at 02:14.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_13" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 8999) {  // save at 02:29.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_14" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 9899) {  // save at 02:44.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_15" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 10799) {  // save at 02:59.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_16" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 11699) {  // save at 03:14.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_17" );
          Serial.println("CLEARDATA");
        }

        if (seconds == 12599) {  // save at 03:29.59am

          Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (now.day()) + "_18" );
          Serial.println("CLEARDATA");
        }

This is just a short snipper but I hope you understand what I mean. Now instead of 15 minutes, I would like to save every 5 minutes or even lesser. But that would mean I'll have to write and edit a very tasking long list of codes as what I've done for 15 minutes (about 90).

My question and request for help is that is there a more efficient way to code and accomplish what I want as per my example code above?

Much thanks.

sprintf() can be used to format strings. it can be used to format a base string with year, month and day which can subsequently be used to format a string with a suffix (e.g. "_02")

rather than explicitly check for seconds being at 15 min intervals, the same techniques using millis() can be used to when ((seconds - secondsLst) > 900)

You need to use a for loop in which the seconds thresholds are calculated using the index of the loop and the duration you want (5 or 15 minutes)

int durationMinutes = 5;
int numberOfIterations = 24 * 60 / durationMinutes; // Assuming the whole duration is 24 hours
for (int i = 0; i < numberOfIterations; i++) {
  int threshold = i * durationMinutes * 60;
          if (seconds == threshold ) {  // save at ...
            String suffix = "_" + String(i);
          if ((future.day()) > (now.day())) {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (now.month()) + "-" + (future.day()) + suffix );
          }
          else {
            Serial.println((String)"SAVEWORKBOOKAS,J2_" + (now.year()) + "-" + (future2.month()) + "-" + (future.day()) + suffix );
          }
          Serial.println("CLEARDATA");
        }
}

This is just an example, not tested nor compiled...

s there a more efficient way to code and accomplish what I want

Yes

Each time you save a data point, work out the time of the next save then test for that in loop(). That way you only ever have one test to perform

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