Hi all,
I'm currently trying to use an oxygen sensor (datasheet of it is attached). This is meant to be done by sending a 9 byte HEX array and receiving an 11 byte array back, from which using 2 of those bytes the oxygen concentration can be calculated. However, when I run my code I do not receive the 11 bytes that I should and I am not sure why that is. Any help would be appreciated.
Thanks you all <3
#include <AltSoftSerial.h>
AltSoftSerial altSerial(8, 9); // RX, TX
byte reading[10]; // byte array to store 11 bytes
int result; // final result from reading
boolean dataAvailable = false; //to check if I received 11 bytes of data
float O2concentration; //concentraiton calculated
void setup() {
// put your setup code here, to run once:
altSerial.begin(19200);
Serial.begin(9600);
}
void loop() {
byte message[] = { 0x55, 0xAA, 0x7E, 0x02, 0x4F, 0x43, 0x94, 0x0E, 0x0D }; //Sending this HEX array to sensor
altSerial.write(message, sizeof(message));
int MAX_MILLIS_TO_WAIT = 1000;
unsigned long starttime;
starttime = millis();
while ( (altSerial.available()<11) && ((millis() - starttime) < MAX_MILLIS_TO_WAIT) )
{
// Wait until I get all 11 bytes
}
if(altSerial.available() < 11)
{
// the data didn't come in - handle that problem here
Serial.println("ERROR - Didn't get 11 bytes of data!");
}
else
{
for(int n=0; n<10; n++){
reading[n] = altSerial.read(); // Then: Get them.
}
dataAvailable = true;
}
if (dataAvailable == true) {
result = reading[6] * 256 + reading[7]; // converting array to readable stuff
O2concentration = result/10; // measured concentration in %
}
Serial.print(O2concentration);
Serial.println("%");
delay(1000);
}