0

I am trying to send two different decimal values serially to the Arduino. The values which are sent to the Arduino are separated by a comma(,):

For e.g. 1.23,4.56

My problem is that when the values are received by the Arduino micro-controller, the code doesn't seem to output the desired result.

Both the Serial.println command seen in the code below outputs the following for the variables value_1 and value_2:

1.20

0.00

4.50

0.00

So what I don't understand is why is there an additional '0.00' value in both variables.

Thanks in advance.

const int MaxChars = 3; // an int string contains up to 3 digits (3 s.f.) and
                        // is terminated by a 0 to indicate end of string
char strValue_1[MaxChars+1]; // must be big enough for digits and terminating null
char strValue_2[MaxChars+1]; // must be big enough for digits and terminating null
 int index_1 = 0;         // the index into the array storing the received digits
 int index_2 = 0;         // the index into the array storing the received digits
 double value_1;
 double value_2;

 void setup()
 { 
   Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
 }

 void loop()
{
 if(Serial.available())
 {
char ch = Serial.read();
if(index_1 <  MaxChars && ch >= '.' && ch <= '9')
{
  strValue_1[index_1++] = ch; // add the ASCII character to the array;

}
else if (ch == ',')
{ 
    if(index_2 <  MaxChars && ch >= '.' && ch <= '9')
    {
      strValue_2[index_2++] = ch; // add the ASCII character to the array;
    }
}
else
{
  // here when buffer full or on the first non digit
  strValue_1[index_1] = 0;        // terminate the string with a 0
  strValue_2[index_2] = 0;        // terminate the string with a 0
  value_1 = atof(strValue_1);     // use atof to convert the string to an float
  value_2 = atof(strValue_2);     // use atof to convert the string to an float
  Serial.println(value_1);  
  Serial.println(value_2);  
  index_1 = 0;
  index_2 = 0;
}
}
}

Below is the most recent edited version of the code as suggested by @mactro and @aksonlyaks, I'm still unable to obtain the desired outputs;hence I'm open to more suggestions.

As of current the output I receive for the specific input of 1.23,4.56 for the following variables are:

strValue[0]:

1.2

strValue[1]:

1.2

4.5

value_1:

1.20

0.00

value_2:

1.20

4.50

Thanks in Advance.

Here is the Most Recent Version of the Code:

const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '\0' and
                    // is terminated by a 0 to indicate end of string

const int numberOfFields = 2;  //Amount of Data to be stored
char strValue[numberOfFields][MaxChars+1]; // must be big enough for digits and terminating null

int index_1 = 0;         // the index into the array storing the received digits

double value_1;
double value_2;

int arrayVal = 0;

void setup()
{ 
  Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{

 if(Serial.available())
{
    char ch = Serial.read();

if (ch == ',')
{ 
    arrayVal = 1;

    if(index_1 <  MaxChars-1 && ch >= '.' && ch <= '9')
    {
      strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array;
    }
    if(index_1 == MaxChars - 1)
    {
      strValue[arrayVal][index_1++] = '\0';
    }

 }
 else if(index_1 <  MaxChars-1 && ch >= '.' && ch <= '9')
 {
   strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array;

 if(index_1 == MaxChars - 1)
 {
    strValue[arrayVal][index_1++] = '\0';
 }

 }

 else
 {

  value_1 = atof(strValue[0]);     // use atof to convert the string to an float
  value_2 = atof(strValue[1]);     // use atof to convert the string to an float
  Serial.println(value_1);  
  Serial.println(value_2);  
  index_1 = 0;
  arrayVal = 0;
}
}
}
bat_wave
  • 15
  • 2
  • 7

2 Answers2

0

You never add anything to strValue_2, because

if(index_2 <  MaxChars && ch >= '.' && ch <= '9')
{
  strValue_2[index_2++] = ch; // add the ASCII character to the array;
}

is executed only when ch==','. When you receive comma, you should set a flag, that would signal a code to write further characters to strValue_2 instead of strValue_1. Or you could have an array of strings like char strValues[2][MaxChars+1] and change and index of an element to which you write strValues[stringNumber][index++].

mactro
  • 518
  • 7
  • 13
  • I gave your method of using an array of strings a try and I have added the code to my question as you can see above. However, I'm still unable to resolve the issue, could you give me more hints as to what I'm doing wrong this time around with the new code. I think the issue is with setting the flag, maybe you could propose a way as to how I can better do that. Thank you very much for your feedback. – bat_wave Oct 19 '16 at 15:02
  • @bat_wave What output are you getting now? Try `Serial.println(strValue[0])`, so you will know what's inside the buffer. – mactro Oct 19 '16 at 18:01
  • I tried `Serial.println(strValue[0])`. I input the values: 1.23,4.56. And the value which output is: 1.2 1.2 I'm not sure what is causing the duplication. And when I do the same but with `Serial.println(strValue[1])` for the same input of 1.23,4.56. I'm getting the output: 4.5 After the atof conversion, value_1 gives: 1.20 1.20 – bat_wave Oct 20 '16 at 15:23
  • This is a continuation from the previous comment; Strangely after the atof conversion, value_2 gives: 0.00 4.50 not sure what is causing the duplication and the 0.00 to appear. – bat_wave Oct 20 '16 at 15:27
  • After printing you are reseting index_1 variable but you are not resetting arrayVal. Reset it to 0. Also, add null character at the end of each float string before atof. – aksonlyaks Oct 24 '16 at 13:34
  • @aksonlyaks Thanks for your feedback, I updated the code with regards to your suggestions; You can see the changes to the code in my question above. However, I'm still not getting the desired results. – bat_wave Oct 25 '16 at 10:16
  • @aksonlyaks As seen in the above update to the question; the output I currently receive for the specific input of 1.23,4.56 for the following variables are: strValue[0]: 1.2 strValue[1]: 1.2 4.5 value_1: 1.20 0.00 value_2: 1.20 4.50 – bat_wave Oct 25 '16 at 10:18
0

I did some modification to your code and now it's printing what you desire. The modified code is as follows:

const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '\0' and
                    // is terminated by a 0 to indicate end of string

const int numberOfFields = 2;  //Amount of Data to be stored
char strValue[numberOfFields][MaxChars+1]; // must be big enough for digits and terminating null

int index_1 = 0;         // the index into the array storing the received digits

double value_1;
double value_2;

int arrayVal = 0;

void setup()
{ 
  Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{

    if(Serial.available())
    {
        char ch = Serial.read();

        if (ch == ',')
        { 
            arrayVal = 1;
            index_1 = 0;    // Initialise this to zero for the float value received after ','
/*
            if(index_1 <  MaxChars-1 && ch >= '.' && ch <= '9')
            {
              strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array;
            }
            if(index_1 == MaxChars - 1)
            {
              strValue[arrayVal][index_1++] = '\0';
            }
*/
         }
         else if( (index_1 <  MaxChars + 1) && (ch >= '.' && ch <= '9'))  // one float value size including null character is 5 (1.23 size 4)
         {
            strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array;
            if(index_1 == MaxChars)             // When we have recevied the 4 character of float value add NULL character
            {
                strValue[arrayVal][index_1++] = '\0';
            }
         }else
         {
            value_1 = atof(strValue[0]);     // use atof to convert the string to an float
            value_2 = atof(strValue[1]);     // use atof to convert the string to an float
            Serial.println(value_1);  
            Serial.println(value_2);  
            index_1 = 0;
            arrayVal = 0;
         }
    }
}

Also, I did proper indentation for your code.

Let me know if this helps.

Regards

aksonlyaks
  • 193
  • 7