Read a start and stop timer from a text file and apply it to a relay

How to read a start and stop timer from a text file and apply it to a relay in Arduino? for example read the number below 158500,178500,251800,350000 start, stop, start, stop

the first number is start time and the second is the stop time in millisecond and when the function of millis() reach the one of the start time, start and when reach the stop time stop the relay.

Many thanks, Ali

I can't help you with the reading as I don't have a card reader. This basics there would be to read the file character by character into a buffer till you encounter a newline character ('\n') or carriage return ('\r') or till you reach the end of file (whichever comes first).

Next you can use strtok() (or strchr()) to split the line on the comma. Once you have the first number as text, you can convert it to an unsigned long (I think there is a dedicated function in arduino though I'm not sure, else you can use strtoul()). Once you have the second number, convert it to unsigned long. And so on.

strtok strchr strtoul

You can google for strtok example, strchr example and strtoul example.

In order to read a line from a file, you should first save it in a “buffer”, then process it. Like this:

// Assuming that you already opened the file in read-only mode (FILE_READ)

void readFile() {
  char line1 [80]; // Make this array large enough to fit the line completely
  char line2 [80];
  readLine(line1);
  readLine(line2);
  // Process the saved lines...
}

void readLine(char *buffer) {
  if (file.available() < 1) { return; } // If reached the EOF, then abort this function.
  byte index = 0;
  char currentChar = file.read();
  while (currentChar != '\r') { // Stop if we reached the "carriage return" character
    buffer[index] = currentChar;
    index++;
    currentChar = file.read();
  }
  buffer[index] = 0; // This is equivalent to add the "string terminator"
  file.seek(file.position() + 1); // Usually, a new line is marked by two control characters, so we need to
  // move the "file's pointer" one byte foward before reading another line
}
// I would type how to handle a full buffer, but it would make the code more difficult to understand...

Notice that the file MUST end with an empty line, otherwise attempting to read the last line may “crash” the program.

Thanks for your respond,

My problem is after reading and converting the text to a number. How can I control the relay based on the start and stop time?
I tried several procedure, below code seems working well.
please check that.

readdataforrelay.ino (691 Bytes)

AlirezaRabieh: I tried several procedure, below code seems working well. please check that.

If it is working well I don't see the need to check it.

If it is not working as you want you need to tell us what it actually does and what you want it to do.

...R

Code that is as small as yours should be posted in code tags (button </>)

#include <string.h>
char sz[] = "6500,10120,15800,20000";
int i = 0;
long Timer[20];
int j = 0 ;
bool startb = false ;
int RelayPin = 3;

void setup() {
  // put your setup code here, to run once:
  char*p = sz ;
  char* str;
  Serial.begin(9600);
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, HIGH);
  while ((str = strtok_r(p, ",", &p)) != NULL) {
    Timer[i] = atol(str) ;
    i = i + 1 ;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  long previous = millis();

  if ((Timer[2 * j] < previous ) && (Timer[2 * j + 1] > previous)) {
    startb = true ;
    digitalWrite(RelayPin, LOW);
  }
  else {
    digitalWrite(RelayPin, HIGH);
    if (startb == true) {
      startb = false ;
      j = j + 1;
    }
  }
}

You should use unsigned long for any variable holding millis(), micros() or a comparation value.
Timining based on the absolute value of millis() is rarely useful.

If your application only switches something on/off twice and both actions are within days after the start, you could use it.
Your code uses two events that are done after 7 minutes.
Remember that the cycle starts over after any power-cycle or other reboot.

Why do you include string.h?