Reading multiple parameters through serial port using delimiter (,)

Hi friends!

I have been trying to receive two parameters through serial port at the same time (using , as delimiter) but I always get the second one as zero. I don't know whats wrong with code. Can anyone point out my mistake?

unsigned long T, T0, T1, T2, T3, T4, t, t_start, t1, t2, t3, t4;
boolean signal_ON;
boolean flag = 1;
int user_command = 0;
int user_command2 = 8000;
int Vlow1, Vhigh1;
int v;
String comdata = "";
int numdata[12] = {0}, Users_Parameters[12] = {3, 5, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17}, mark = 0; //numdata is array of numbers after splitting
int Camera_power = 6; //Camera Power Supply
int Laser_power = 7; //Laser Power Supply
int Camera_signal = 3; //Camera Signal
int Laser_signal = 2; //Laser Signal
int Switch_pos = 10; //+24V Switch
int Switch_neg = 12; //-24V Switch

void setup() {

  t1 = 30000;
  t2 = 20479;
  t3 = 1000;
  t4 = 2000;
  T0 = 0;
  T1 = t1;
  T2 = t1 + t2;
  T3 = t1 + t2 + t3;
  T4 = t1 + t2 + t3 + t4;
  T = t1 + t2 + t3 + t4;
  t = T + 1;

  Vlow1;
  Vhigh1;
  signal_ON = true;

  pinMode(Camera_signal, OUTPUT);
  pinMode(Laser_signal, OUTPUT);
  pinMode(Laser_power, OUTPUT);
  pinMode(Switch_pos, OUTPUT);
  pinMode(Switch_neg, OUTPUT);


  analogWriteResolution(12);
  digitalWrite(Camera_signal, LOW);
  digitalWrite(Laser_signal, LOW);
  digitalWrite(Laser_power, LOW);
  digitalWrite(Switch_pos, HIGH);
  digitalWrite(Switch_neg, HIGH);
  Serial.begin(9600);
  Serial.println("Enter a command:");
  Serial.println("For Switching on the Camera, Enter 1");
  Serial.println("For Switching off the Camera, Enter 2");
  Serial.println("For Switching on the Laser, Enter 3");
  Serial.println("For Switching off the Laser, Enter 4");
  Serial.println("For Switching on the Scanner, Enter 5");
  Serial.println("For Switching off the Scanner, Enter 6");
  Serial.println("For changing the laser/scanner high time, Enter 7, value for laser/scanner high");
  Serial.println("For changing the camera high time, Enter 8, value for Camera");
  Serial.println("For changing the camera exposure time, Enter 9, value for Camera exposure");
  Serial.println("For changing the angle of scanner, Enter 10, Value of Scanner between 0 to 4095 ");
  Serial.println("For Switching on the Signal_ON, Enter 11");
  Serial.println("For Switching off the Signal_ON, Enter 12");


}

void loop() {


  PIV_Preset();
  Users_Parameter_Acquisition();


}

void PIV_Preset()
{

  int j = 0;
  if (signal_ON)
  { T1 = t1;

    if (t > T)
    {
      t_start = micros();
    }

    t = micros() - t_start;
    if (t < T1)
    {
      flag = 0;
      digitalWrite(Camera_signal, HIGH);
    }

    if (t > T1)
    { T0 = 0;
      T2 = t1 + t2;
      T3 = t1 + t2 + t3;
      T4 = t1 + t2 + t3 + t4;
      T = t1 + t2 + t3 + t4;
      flag = 0;
      v = map(t, T1, T2, Vlow1, Vhigh1);
      analogWrite(DAC1, v);
      //digitalWrite(Laser_signal, HIGH);

    }
    if (t > T2)
    {
      flag = 0;
      digitalWrite(Laser_signal, LOW);
    }

    if (t > T3)
    {
      flag = 0;
      digitalWrite(Camera_signal, LOW);
    }
  }
}

void Users_Parameter_Acquisition()
{ int j = 0;
  while ((Serial.available() > 0))
  {
    // Read in the string and connect it to comdata
    comdata += char(Serial.read());
    delay(2);
    mark = 1;
  }

  if (mark == 1) //If data is received perform comdata analysis otherwise do nothing
  {
    //Display the string just entered (optional)
    Serial.println(comdata);

    for (int i = 0; i < comdata.length() ; i++)
    {
      if (comdata[i] == ', ')
      {
        j++;
      }
      else
      {
        numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
      } 

    }
    //Comdata strings have been converted to numdata. Clear comdata for next use
    //If you don't clear it, this result is likely to interfere with the next time.


    comdata = String("");
   
    //The content of numdata are output in a cycle and written to Users_Parameters[]

    for (int i = 0; i < 12; i++)
    {
      Users_Parameters[i] = numdata[i];
      numdata[i] = 0;
    }
    user_command = int(Users_Parameters[0]);
    user_command2 = int(Users_Parameters[1]);
    
    if (user_command == 1)     {
      Serial.println("Camera On");
      digitalWrite(Camera_power, HIGH);
    }
    else if (user_command == 2) {
      Serial.println("Camera Off");
      digitalWrite(Camera_power, LOW);
    }
    else if (user_command == 3) {
      Serial.println("Laser On");
      digitalWrite(Laser_signal, HIGH);
    }
    else if (user_command == 4) {
      Serial.println("Laser Off");
      digitalWrite(Laser_signal, LOW);
    }
    else if (user_command == 5) {

      Serial.println("Scanner On");

      int v = map(t, 0, T, 0, 4095);
      analogWrite(DAC1, v);

    }
    else if (user_command == 6) {
      Serial.println("Scanner Off");
      analogWrite(DAC1, Vlow1);
    }

else if (user_command == 7) {
      t2 = user_command2;
      Serial.println(t2);
 
    }
    
    else if (user_command == 8) {
      t3 = user_command2;
      Serial.println(t3);
    }
    
    else if (user_command == 9) {
      t4 = user_command2;
      Serial.println(t4);
    }
    
    else if (user_command == 10) {
      Vhigh1 = user_command2;
      Serial.println(Vhigh1);
    }

    else if (user_command == 11) {
      Serial.println("Signal On");
      signal_ON = true;
    }
    
    else if (user_command == 12) {
      Serial.println("Signal Off");
      signal_ON = false;
    }

    else
    {
      delay(10);
    }

    //After output, the mark read to data must be set to 0. If it is not set to 0, the next cycle cannot be used
    mark = 0;
    flag = 1;
  }
}

PIV_Arduino.ino (5.51 KB)

Most probably something is wrong with these parts:

for (int i = 0; i < 12; i++)
{
Users_Parameters = numdata*;*
_ numdata = 0;
* }_
user_command = int(Users_Parameters[0]);
user_command2 = int(Users_Parameters[1]);
if (user_command == 7) {
t2 = user_command2;
_ Serial.println(t2);*_

* }*
Whenever I print value of t2, its always zero.

First it is impossible, if you want to receive both concurrently with one serial port. you need two serial ports. The software serial only allows one channel at a time. Since you did not state which Arduino you are using look at the Mega, it has three ports available and all three can receive concurrently. I hope this helps.

Thanks for your time. I am using Arduino Due. I meant to say that when I enter 7, 1000 in serial port. 1000 should be transferred to user_command2. Is that possible?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Also avoid using Strings.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Thanks a lot. I already used the example you mentioned and it worked. But for knowledge's sake, can you point out the basic mistake in my code?

BJHenry:
Also avoid using Strings.

What problems does string use cause in general?

nauman_khan_93:
But for knowledge's sake, can you point out the basic mistake in my code?

Might be this:

      if (comdata[i] == ', ')

There are two characters enclosed in single quotes. Single quotes are for a single character. Double quotes are for string literals - a trailing null character is added.

Websearch 'C++ single quote'.

nauman_khan_93:
What problems does string use cause in general?

Memory fragmentation which leads eventually to unexpected/unwanted behavior.

Thanks a lot dougp. I got it.