I try to read SPRINTIR co2 sensor and it has four pins(RX, TX, GND, VCC)
I am fine to read only one sensor but when I try to read two sensors, the data I get sometimes is wrong.(For example, it will be super large..) What I found is the data should have 18 components for each set, most of time it is correct but sometimes it will receive less components than 18. Could anyone help me?
My code is like that:
String val = "";
double co2 = 0;
uint8_t buffer[18];
uint8_t ind = 0;
uint8_t flag = 0;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
}
void loop()
{
while((buffer[ind-1] != 0x0A))
{
if(Serial1.available()&&flag==0)
{
buffer[ind] = Serial1.read();
ind++;
}
else if(Serial2.available() && flag==1)
{
buffer[ind] = Serial2.read();
ind++;
}
}
report();
}
void report()
{
for (int i = 0; i<ind+1 ; i++)
{
if(buffer == 'z')break;
if((buffer != 0x5A) && (buffer != 0x20))
* {*
_ val +=buffer*-48;
}
}
co2 = val.toInt();*_
* if(flag==0)*
* {*
* Serial.print("Co2_0=");
_ Serial.print(co2);
Serial.println("ppm");
flag=1;
}
else if(flag==1)
{_
Serial.print("Co2_1=");
_ Serial.print(co2);
Serial.println("ppm");
flag=0;
}
ind = 0;
val = "";
}*_