Hi all,
I am working on a project where I have a sensor taking data and sending it over serial to software called Max, where I want to use the sensor readings.
In the Arduino serial monitor window I can see the sensor readings formatted like this:
.90115.90115.90115.90115.90115.90115.90115.90115.90115.90115 etc
And in Max I am getting the same thing. It just comes in as one massive number that I can't even break into a list.
I have a feeling that I am formatting Serial.print incorrectly.
I am doing this: Serial.print(Value_O2, 3);
where Value-02 is my sensor reading. Is there a way to add a space or something so I can break down this data? I think 4 decimal points is enough for the resolution I need.
Thank you any help would be greatly appreciated.
I am posting all of my code below.
#define Version 11 // version, 1.0 or 1.1, which depands on your board you use as it is
const int pinO2 = A0; // Connect Grove – Gas Sensor(O2) to A0.
#if Version==11
const int AMP = 121;
#elif Version==10
const int AMP = 201;
#endif
const float K_O2 = 7.43;
void setup()
{
Serial.begin(115200); //Start the Serial connection
}
void loop()
{
float sensorValue;
float sensorVoltage;
float Value_O2;
sensorValue = analogRead(A0);
sensorVoltage = (sensorValue / 1024.0) * 3.3;
sensorVoltage = sensorVoltage / (float)AMP * 10000.0;
Value_O2 = sensorVoltage / K_O2;
Serial.print(Value_O2, 3); // this is the value I am using
delay(50); // I seem to get better values with the delay
}
After
Serial.print(Value_O2, 3);
add
Serial.print(',');
to separate the values wit a comma (delimiter). it is called comma separated values or .CSV.
Here is a version of your code that does not use delay(). It uses the method from the blink without delay example for timing.
#define Version 11 // version, 1.0 or 1.1, which depands on your board you use as it is
const int pinO2 = A0; // Connect Grove - Gas Sensor(O2) to A0.
#if Version==11
const int AMP = 121;
#elif Version==10
const int AMP = 201;
#endif
const float K_O2 = 7.43;
void setup()
{
Serial.begin(115200); //Start the Serial connection
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();
float sensorValue;
float sensorVoltage;
float Value_O2;
sensorValue = analogRead(A0);
sensorVoltage = (sensorValue / 1024.0) * 3.3;
sensorVoltage = sensorVoltage / (float)AMP * 10000.0;
Value_O2 = sensorVoltage / K_O2;
Serial.print(Value_O2, 3); // this is the value I am using
Serial.print(',');
}
}
Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().
@groundFungus, thank you for the reply.
I tried adding the Serial.print(" "); and Serial.print(",");
It was still kind of hard parsing the data in Max.
But I did discover another way to get the values one at a time by using:
Serial.println(Value, 3);
Thank you for suggesting using millis to avoid the delay. But I think what I want to do is have the Arduino only send a value if it changes. It's looking ok so far. Hopefully I won't have more issues.
Here is the code I am going with for now:
// test Grove – Gas Sensor(O2), I am using GSR sensor
//
// Analog Grove Connector
// Yellow Connected to A0
// Red to 3.3V
// Black to GND
//
// #include
#define Version 11 // version, 1.0 or 1.1, which depands on your board you use as it is
const int pinO2 = A0; // Connect Grove – Gas Sensor(O2) to A0. GSR sensor
#if Version==11
const int AMP = 121;
#elif Version==10
const int AMP = 201;
#endif
const float K_O2 = 7.43;
float Value;
float previousValue = 10;
void setup()
{
Serial.begin(115200); //Start the Serial connection
}
void loop()
{
float sensorValue;
float sensorVoltage;
sensorValue = analogRead(A0);
sensorVoltage = (sensorValue / 1024.0) * 3.3;
sensorVoltage = sensorVoltage / (float)AMP * 10000.0;
Value = sensorVoltage / K_O2;
// only transmit Value_02 if the input has changed
if ( Value != previousValue) {
previousValue = Value;
Serial.println(Value, 3);
}
// Serial.println(Value, 3); // this is the value I am using
// delay(50); // I seem to get better values with the delay
}