0

I am communicating with a system serially using Arduino Nano and I am able to communicate with it except for one issue.

I have two functions and I want to call them together so that I can get two values one after another but I am unable to do so. The data values comes correctly when I call either of the functions, but when I try calling them together the data values throw some random values and never comes right. What could be the issue? The code is given below.

#include<SoftwareSerial.h>

#define nodata 0

float data1[11] = { 0.0 };
float data2[11] = { 0.0 };

SoftwareSerial obj (2,3);             


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


void config1()
{
  obj.write(0x01);
  obj.write(0x0A);
  obj.write(0x23);
  obj.write(0x43);
  obj.write(0x45);
  obj.write(0x19);
  obj.write(0x20);

  if(obj.available()>0)
  {
    for(int i=0;i<10;i++)
    {
      data1[i]= obj.read();
    }
    float value1 = (data1[6]*256+data1[5]);
    Serial.println(value1);
    value1 = 0; 
  }

  else
  {
    return nodata;
  }

  for(int i=0;i<10;i++)
  {
     data1[i]= {0};
  }
}

void config2()
{
  obj.write(0x01);
  obj.write(0x0A);
  obj.write(0x24);
  obj.write(0x44);
  obj.write(0x46);
  obj.write(0x19);
  obj.write(0x20);

  if(obj.available()>0)
  {

    for(int i=0;i<10;i++)
    {
      data2[i]= obj.read();
    }
    float value2 = (data2[6]*256+data2[5]);
    Serial.println(value2);
    value2 = 0; 
  }

  else
  {
    return nodata;
  }

  for(int i=0;i<10;i++)
  {
    data2[i]= {0};
  }
}


void loop()
{
  config1();
  delay(100);
  config2();
}     

As you can see I am calling config1 and config2 together, and in this case the data never comes right. But when I call either config1 or config2, it gives data perfectly.

  • 1
  • 1
    How is the data wrong? From what I can tell, there's no significant difference between config1 and config2 besides some different variable naming and some hard coded values. You should just be able to call config1 twice with adding some parameters. Have you tried a 2nd delay after this 2nd method call? – computercarguy Aug 2 '19 at 18:49
  • thanks for replying..you are correct there is not much difference between config1 and config2 but the instructions..they both have same headers and end but the data in between is different..also i tried 2nd delay,it didn't worked .plus i know what values i should get so thats why i was telling that data is wrong when i call both functions together. – N L Aug 2 '19 at 18:54
  • 1
    Are are trying to read 10 bytes, but you only check, if there are ANY bytes in the serial buffer. It can happen, that at that time there are less than 10 bytes available in the buffer. The rest will be returned as -1 from obj.read() – chrisl Aug 2 '19 at 18:55
  • 1
    if you read without data available, then the data arrive in the second function – Juraj Aug 2 '19 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.