Switching a relay on and off using "millis()" without interfering with the runtime of the other piece of code

mr. Blackfin
the code works fine, I will have to adapt it to the code. I am working for this.
but this code works fine as below. Thank you for your help and suggestions.

Finally, the code that works as I want is below. I don't know if it slows down the arduino. But it seems to be working. I can record 760-800 samples in 10 seconds.
I created this code based on mr.gerivega's idea.
does this code need improvement? is there any error?

#include <SPI.h>
#include "SdFat.h"
#include "RTClib.h"
#include "HX711.h"//XFW-HX711 amplifier 80Hz

#define calibration_factor -7090.0
int LOADCELL_DOUT_PIN = 7;
int LOADCELL_SCK_PIN = 6;

int LOADCELL_DOUT_PIN_1 = 5;
int LOADCELL_SCK_PIN_1 = 4;

int LOADCELL_DOUT_PIN_2 = 3;
int LOADCELL_SCK_PIN_2 = 2;
HX711 scale;
HX711 scale1;
HX711 scale2;
int navoser;
int sensorPin = A0;
int relay1 = 8;
int relay2 = 9;
int a = 0;
unsigned long previousMillis = 0;
long OnTime = 10000;
long OffTime = 50;
int relay2State = LOW;
boolean alreadyRun = false;

int sensorValue = 0;
float voltage;
RTC_DS1307 RTC;//using RTClib
DateTime now;

bool enableDataLogging = false;
String receivedChar;
boolean newData = false;

const uint8_t chipSelect = 10;

const uint32_t SAMPLE_INTERVAL_MS = 1;

#define FILE_BASE_NAME "Result"
#define error(msg) sd.errorHalt(F(msg))
//------------------------------------------------------------------------------

SdFat sd;

SdFile file;// Log file.

uint32_t logTime;

//==============================================================================

int Date_count = 1;
int Time_count = 1;
int Load_Cell1 = 1;
int Load_Cell2 = 1;
int Load_Cell3 = 1;
//------------------------------------------------------------------------------

void load_cell () {

  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("t3.txt=");
  Serial.write(0x22);
  Serial.print(scale.get_units(), 1);
  Serial.write(0x22);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("t4.txt=");
  Serial.write(0x22);
  Serial.print(scale1.get_units(), 1);
  Serial.write(0x22);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("t5.txt=");
  Serial.write(0x22);
  Serial.print(scale2.get_units(), 1);
  Serial.write(0x22);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  navoser = (scale.get_units() + scale1.get_units() + scale2.get_units() / 3);
  Serial.print("t8.txt=");
  Serial.write(0x22);
  Serial.print(navoser);
  Serial.write(0x22);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  /*
    Serial.print("Reading: ");
    Serial.print(scale.get_units(), 1);
    // Serial.print(" kg");
    Serial.print("\t");
    Serial.print(scale1.get_units(), 1);
    Serial.print("\t");
    Serial.print(scale2.get_units(), 1);
    Serial.print(" kg");
    Serial.println();
  */

}

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  Serial.begin(9600);//

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale1.begin(LOADCELL_DOUT_PIN_1, LOADCELL_SCK_PIN_1);
  scale2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);

  scale.set_scale(calibration_factor);
  scale.tare();

  scale1.set_scale(calibration_factor);
  scale1.tare();

  scale2.set_scale(calibration_factor);
  scale2.tare();

  RTC.begin();

}
void creat_file() {
  const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
  char fileName[13] = FILE_BASE_NAME "00.csv";
  if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
    sd.initErrorHalt();
  }

  // Find an unused file name.

  if (BASE_NAME_SIZE > 7) {
    error("FILE_BASE_NAME long");
  }
  while (sd.exists(fileName)) {
    if (fileName[BASE_NAME_SIZE + 1] != '9') {
      fileName[BASE_NAME_SIZE + 1]++;
    } else if (fileName[BASE_NAME_SIZE] != '9') {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    } else {
      error("filename not created");
    }
  }
  if (!file.open(fileName, O_WRONLY | O_CREAT | O_EXCL)) {
    error("file.open");
  }
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("page 4");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("g1.txt=");
  Serial.write(0x22);
  Serial.print(fileName);
  Serial.write(0x22);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  //Serial.print(F("Recording: "));
  //Serial.println(fileName);
  //Serial.println(F("enter any character to stop"));
  writeHeader();

}
//------------------------------------------------------------------------------
void loop() {

  recvOneChar();
  useNewData();
  if (enableDataLogging) {

    logTime += SAMPLE_INTERVAL_MS;
    logData();


    if (!file.sync() || file.getWriteError()) {
      error("error write");
    }
  } else {
    load_cell();
  }
  //MOD GERRY
  relay (); //check if anything should be done on the relay
}
void recvOneChar()
{

  sensorValue = analogRead(sensorPin);
  voltage = sensorValue * (5.0 / 1023.0);
  if (Serial.available() > 0 || voltage > 2 ) {
    receivedChar = Serial.readStringUntil('#');

    newData = true;
  }
}//recvOneChar
void useNewData()
{
  if (newData == true)
  {
    //Serial.print("This just in ... ");
    Serial.println(receivedChar);

    if (receivedChar.indexOf("2") > -1 || voltage > 2 )
    {

      creat_file();
      enableDataLogging = true;
      //MOD GERRY
      previousMillis = 0;    // <----------------
      //alreadyRun = false;    // <----------------
      if (previousMillis == 0)       //has not been set before
        previousMillis = millis();  //will turn on after this time
      a = 0;
      //Serial.println("Data logging enabled");
      //digitalWrite(LED_BUILTIN, HIGH);
    }
    if (receivedChar.indexOf("5") > -1)
    {
      enableDataLogging = false;

      //Serial.println("   Data logging disabled");
      //digitalWrite(LED_BUILTIN, LOW);

      if (file) file.close();
      //SysCall::halt();
    }
    newData = false;
  }
}//useNewData

void writeHeader() {

  file.print(F("time(us)"));
  for (int c = 0; c < Load_Cell1; c++) {
    file.print(F(",load1"));
    //file.print(c, DEC);
  } for (int d = 0; d < Load_Cell2; d++) {
    file.print(F(",load2"));
    //file.print(d, DEC);
  } for (int e = 0; e < Load_Cell3; e++) {
    file.print(F(",load3"));
    //file.print(e, DEC);
  }
  for (int a = 0; a < Date_count; a++) {
    now = RTC.now();
    file.print(F(",date:"));
    file.print(now.day(), DEC);
    file.print('/');
    file.print(now.month(), DEC);
    file.print('/');
    file.print(now.year(), DEC);

  } for (int b = 0; b < Time_count; b++) {
    file.print(F(",time(hour-minute-second)"));

  }
  file.println();
}
//------------------------------------------------------------------------------

void logData() {

  file.print(logTime);
  for (int c = 0; c < Load_Cell1; c++) {
    file.write(',');
    file.print(scale.get_units());
  } for (int d = 0; d < Load_Cell2; d++) {
    file.write(',');
    file.print(scale1.get_units());
  } for (int e = 0; e < Load_Cell3; e++) {
    file.write(',');
    file.print(scale2.get_units());//file.print(scale2.get_units(),DEC);
  }
  for (int a = 0; a < Date_count; a++) {
    file.write(',');
    //file.print("test");
  } for (int b = 0; b < Time_count; b++) {
    file.write(',');
    file.print(now.hour(), DEC);
    file.print(":");
    file.print(now.minute(), DEC);
    file.print(":");
    file.print(now.second(), DEC);
    file.print(":");
    file.print(micros(), DEC);
  }
  file.println();
  //MOD GERRY

  //relay ();
}
//==============================================================================
void relay () {

  if ( a < 1) {
    unsigned long currentMillis = millis();
    //MOD GERRY
    //if ((relay2State == LOW) && (currentMillis - previousMillis >= OnTime) ) {
    if ((relay2State == LOW) && (currentMillis - previousMillis >= OnTime) && !(previousMillis == 0)) {

      relay2State = HIGH;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(relay2, relay2State);  // Update the actual LED
    }
    else if ((relay2State == HIGH) && (currentMillis - previousMillis >= OffTime) ) {

      relay2State = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(relay2, relay2State);  // Update the actual LED
      //alreadyRun = true;
      a++;
      Serial.println(a);

    }
  }
}