problems with ASCII message for recovering timers

hello,
i am trying to control a relay with different timers (and the times are given by an ASCII message in the format "Sxxx,xxxx\n")
from the help i got in this post:https://forum.arduino.cc/index.php?topic=658007.0

but when i charge the program in the arduino, before sending whatever i send, it already turns on the relaies. and i dont get why. here is the code:

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 8;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
const int relay1 =  5;
const int relay2 =  8;
const int relay3 =  12;
const uint8_t       valve_OFF = LOW;
const uint8_t       valve_ON = HIGH;
// variables to hold the parsed data

int time1 = 0;
int time2 = 0;
boolean newData = false;

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

void setup() {
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
}

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

void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    newData = false;
  }
}

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

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = 'S';
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

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

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  time1 = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  time2 = atoi(strtokIndx);     // convert this part to an integer

}

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

void showParsedData() {
  delay(time1 * 1000);
  digitalWrite(relay1, valve_ON);
  digitalWrite(relay2, valve_ON);
  digitalWrite(relay3, valve_ON);
  delay((time2 - time1) * 1000);
  digitalWrite(relay1, valve_OFF);
  digitalWrite(relay2, valve_OFF);
  digitalWrite(relay3, valve_OFF);
  newData = false;
}

otaolafr:
i am trying to control a relay with different timers (and the times are given by an ASCII message in the format "Sxxx,xxxx\n")

The message is: "Sxxx, xxxx\n".

1. Is it coming from a Serial Port or from the InputBox of Serial Monitor?

2. Does S mark the beginning of the message?

3. Does \n (newline character = 0x0A) indicate the end of message?

4. What do these three characters: xxx refer to? Are they referring to a Relay or Timer Number?

5. What do these four characters: xxxx refer to? Are they referring to a Relay or Timer Number?

6. How many Relays are there in your system?

7. What is the nature of these timers? Are they referring to the tiers of MCU or some kind of time slots?

GolamMostafa:
The message is: "Sxxx, xxxx\n".

1. Is it coming from a Serial Port or from the InputBox of Serial Monitor?

2. Does S mark the beginning of the message?

3. Does \n (newline character = 0x0A) indicate the end of message?

4. What do these three characters: xxx refer to? Are they referring to a Relay or Timer Number?

5. What do these four characters: xxxx refer to? Are they referring to a Relay or Timer Number?

6. How many Relays are there in your system?

7. What is the nature of these timers? Are they referring to the tiers of MCU or some kind of time slots?

wow ahahah sorry i am beginig to use arduino, sorry i didnt clarify.

1. Is it coming from a Serial Port or from the InputBox of Serial Monitor?
the message is sended by a labview program and the arduino es connected by the micro usb (it is an leonardo)
so, serial port? i image....

2. Does S mark the beginning of the message?
yup :slight_smile:
3. Does \n (newline character = 0x0A) indicate the end of message?
yup :slight_smile:
4. What do these three characters: xxx refer to? Are they referring to a Relay or Timer Number?
xxx the first time in seconds
5. What do these four characters: xxxx refer to? Are they referring to a Relay or Timer Number?
xxxx the second time in seconds
6. How many Relays are there in your system?
two in total, so, is a three relay with pins connected to (realy/arduino):
gnd/gnd
vcc/5V
in1/pin 8
in2/pin 5
in3/pin 12
7. What is the nature of these timers? Are they referring to the tiers of MCU or some kind of time slots?
sorry i think i didn't understood the question, they are timers to open and close solenoid valves via the relays.
i have a measurement that needs to be sync with the open/close of the valves, (and the measurement is done via labview)
in labview i give the two times (the xxx and xxxx) and when to start the measurement, after that
the labview sends the message, for example, S23,155\n to the Arduino and after sending it it begings the measurement, then, after 23 seconds (time 1) the arduino turns on valve the three relays (opening the valves) and then after 155 seconds (time 2) it turn them off in parallel the measurement is being done in continous by the labview program.
thanks, and sorry for not being so specific.

Thank you very much and +karma for an excellent one-to-one answer. The following sketch extracts the times 23 seconds and 155 seconds from this ASCII message: S23,155\n and save them into two int type variables (secOne and secNext). The sketch strongly checks the detection of S and \n. You can now uses these variables in your main program as needed.

char myData[10];     //array to hold whole string
char sec1Array[5];   //array to hold 1st second
char sec2Array[5];  //array to hold 2nd second
int i, j, k;
bool flag = false;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  byte n = Serial.available();//check if charcater has arrived
  if (n != 0)
  {
    char x = Serial.read(); //read charctaer from FIFO Buffer
    if (flag != true)   //Start Mark of Msg not yet found
    {
      if (x == 'S')  //Start Mark of Msg found
      {
        myData[i] = x; //save S
        i++;
        flag = true;
      }
    }
    else  //S has been detected; now collect the remaining charcaters of the string
    {
      if (x != '\n')  //no newline charctaer has arrived
      {
        myData[i] = x;
        i++;
      }
      else
      {
        myData[i] = '\0';
        Serial.println(myData);  //show whole string
        //----- extract 1st second-------
        do
        {
          sec1Array[j] = myData[j];
          j++;
        }
        while (myData[j - 1] != ',');
        sec1Array[j - 1] = '\0';
        //Serial.println(sec1Array);//S123
        sec1Array[0] = 0x30;  //replace S by 0
        int secOne = atoi(sec1Array);
        Serial.println(secOne); //show 1st second
        //--- extrcat 2nd second--------------
        for (int p = 0; p < j; p++)
        {
          myData[p] = 0x30;
        }
        //Serial.println(sec2Array);//S123
        int secNext = atoi(myData);
        Serial.println(secNext);  //show 2nd second
        //---------------------------
        i = 0;
        j = 0;
        k = 0;
        memset(myData, 0, 10);   //array reset
        flag = false;
      }
    }
  }
}

sm-1.png

sm-1.png

but when i charge the program in the arduino, before sending whatever i send, it already turns on the relaies. and i dont get why.

const uint8_t       valve_OFF = LOW;
const uint8_t       valve_ON = HIGH;

Can you please provide some information on the relays. Many of the common modules are active LOW, and that will be the initial state with your code.

GolamMostafa:
Thank you very much and +karma for an excellent one-to-one answer. The following sketch extracts the times 23 seconds and 155 seconds from this ASCII message: S23,155\n and save them into two int type variables (secOne and secNext). The sketch strongly checks the detection of S and \n. You can now uses these variables in your main program as needed.

char myData[10];     //array to hold whole string

char sec1Array[5];  //array to hold 1st second
char sec2Array[5];  //array to hold 2nd second
int i, j, k;
bool flag = false;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  byte n = Serial.available();//check if charcater has arrived
  if (n != 0)
  {
    char x = Serial.read(); //read charctaer from FIFO Buffer
    if (flag != true)  //Start Mark of Msg not yet found
    {
      if (x == 'S')  //Start Mark of Msg found
      {
        myData[i] = x; //save S
        i++;
        flag = true;
      }
    }
    else  //S has been detected; now collect the remaining charcaters of the string
    {
      if (x != '\n')  //no newline charctaer has arrived
      {
        myData[i] = x;
        i++;
      }
      else
      {
        myData[i] = '\0';
        Serial.println(myData);  //show whole string
        //----- extract 1st second-------
        do
        {
          sec1Array[j] = myData[j];
          j++;
        }
        while (myData[j - 1] != ',');
        sec1Array[j - 1] = '\0';
        //Serial.println(sec1Array);//S123
        sec1Array[0] = 0x30;  //replace S by 0
        int secOne = atoi(sec1Array);
        Serial.println(secOne); //show 1st second
        //--- extrcat 2nd second--------------
        for (int p = 0; p < j; p++)
        {
          myData[p] = 0x30;
        }
        //Serial.println(sec2Array);//S123
        int secNext = atoi(myData);
        Serial.println(secNext);  //show 2nd second
        //---------------------------
        i = 0;
        j = 0;
        k = 0;
        memset(myData, 0, 10);  //array reset
        flag = false;
      }
    }
  }
}



![sm-1.png|950x438](upload://qpZdcjO5MUTpadf30V6CyEbNEq1.png)

thanks a lot!!! for all the help!

cattledog:

const uint8_t       valve_OFF = LOW;

const uint8_t      valve_ON = HIGH;




Can you please provide some information on the relays. Many of the common modules are active LOW, and that will be the initial state with your code.

mmm it one like this one ?https://www.amazon.fr/Elegoo-Optocoupleur-4-Channel-Arduino-Raspberry/dp/B06XKST8XC/ref=sr_1_4?__mk_fr_FR=ÅMÅŽÕÑ&crid=2Y8ZUXSJJRAG3&keywords=relay+arduino&qid=1579769658&sprefix=relay+%2Caps%2C154&sr=8-4
i didnt know that, thanks!

GolamMostafa:
Thank you very much and +karma for an excellent one-to-one answer. The following sketch extracts the times 23 seconds and 155 seconds from this ASCII message: S23,155\n and save them into two int type variables (secOne and secNext). The sketch strongly checks the detection of S and \n. You can now uses these variables in your main program as needed.

char myData[10];     //array to hold whole string

char sec1Array[5];   //array to hold 1st second
char sec2Array[5];  //array to hold 2nd second
int i, j, k;
bool flag = false;

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 byte n = Serial.available();//check if charcater has arrived
 if (n != 0)
 {
   char x = Serial.read(); //read charctaer from FIFO Buffer
   if (flag != true)   //Start Mark of Msg not yet found
   {
     if (x == 'S')  //Start Mark of Msg found
     {
       myData[i] = x; //save S
       i++;
       flag = true;
     }
   }
   else  //S has been detected; now collect the remaining charcaters of the string
   {
     if (x != '\n')  //no newline charctaer has arrived
     {
       myData[i] = x;
       i++;
     }
     else
     {
       myData[i] = '\0';
       Serial.println(myData);  //show whole string
       //----- extract 1st second-------
       do
       {
         sec1Array[j] = myData[j];
         j++;
       }
       while (myData[j - 1] != ',');
       sec1Array[j - 1] = '\0';
       //Serial.println(sec1Array);//S123
       sec1Array[0] = 0x30;  //replace S by 0
       int secOne = atoi(sec1Array);
       Serial.println(secOne); //show 1st second
       //--- extrcat 2nd second--------------
       for (int p = 0; p < j; p++)
       {
         myData[p] = 0x30;
       }
       //Serial.println(sec2Array);//S123
       int secNext = atoi(myData);
       Serial.println(secNext);  //show 2nd second
       //---------------------------
       i = 0;
       j = 0;
       k = 0;
       memset(myData, 0, 10);   //array reset
       flag = false;
     }
   }
 }
}



![sm-1.png|950x438](upload://qpZdcjO5MUTpadf30V6CyEbNEq1.png)

hello, i am trying to integrate with the functions i need,
and my issue is that i send the message but it is not stock the value i send...
arduino says me that i have not declared the variables (secOne and secNext) so i give them an initial value to be replaced but when i do this the arduino ignores the new values... (it prints the good ones in the consol but when i see the actions, it does with the original timer...)

char myData[10];     //array to hold whole string
char sec1Array[5];   //array to hold 1st second
char sec2Array[5];  //array to hold 2nd second
int i, j, k;
bool flag = false;
bool measure = false;
const int relay1 =  5;
const int relay2 =  8;
const int relay3 =  12;
const uint8_t       valve_OFF = LOW;
const uint8_t       valve_ON = HIGH;
int secOne = 30;
int secNext = 50;
void setup()
{
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
}

void loop()
{
  byte n = Serial.available();//check if charcater has arrived
  if (n != 0)
  {
    char x = Serial.read(); //read charctaer from FIFO Buffer
    if (flag != true)   //Start Mark of Msg not yet found
    {
      if (x == 'S')  //Start Mark of Msg found
      {
        myData[i] = x; //save S
        i++;
        flag = true;
      }
    }
    else  //S has been detected; now collect the remaining charcaters of the string
    {
      if (x != '\n')  //no newline charctaer has arrived
      {
        myData[i] = x;
        i++;
      }
      else
      {
        myData[i] = '\0';
        Serial.println(myData);  //show whole string
        //----- extract 1st second-------
        do
        {
          sec1Array[j] = myData[j];
          j++;
        }
        while (myData[j - 1] != ',');
        sec1Array[j - 1] = '\0';
        sec1Array[0] = 0x30;  //replace S by 0
        int secOne = atoi(sec1Array);
        Serial.println(secOne); //show 1st second
        //--- extrcat 2nd second--------------
        for (int p = 0; p < j; p++)
        {
          myData[p] = 0x30;
        }
        int secNext = atoi(myData);
        Serial.println(secNext);  //show 2nd second
        //---------------------------
        i = 0;
        j = 0;
        k = 0;
        memset(myData, 0, 10);   //array reset
        flag = false;
        measure = true;
      }
    }
  }
  if (measure == true)
  {
        parseData();
        measure = false;
  }
}

void parseData() {
        delay(secOne * 1000);
      Serial.println('1');
      digitalWrite(relay1, valve_ON);
      digitalWrite(relay2, valve_ON);
      digitalWrite(relay3, valve_ON);
      delay((secNext - secOne) * 1000);
      Serial.println('2');
      digitalWrite(relay1, valve_OFF);
      digitalWrite(relay2, valve_OFF);
      digitalWrite(relay3, valve_OFF);
  }

any idea why :/?

EDIT i erase the "int" in the secNext and secOne and works correctly