How i can create a Json Array with double values and 2 decimal point using the ArduinoJson Library?
https://arduinojson.org/v6/how-to/configure-the-serialization-of-floats/
In the folowing small example it is possible to write to the Json file a double value with 2 decimal point.
#include <ArduinoJson.h>
double round2(double value) {
return (int)(value * 100 + 0.5) / 100.0;
}
double round3(double value) {
return (int)(value * 1000 + 0.5) / 1000.0;
}
double var1 = 1.23456789;
double var2 = 5.05599991;
void setup() {
Serial.begin(115200);
StaticJsonDocument<200> doc;
doc["hello"] = "world";
doc["var1"] = round2(var1);
doc["var2"] = round3(var2);
serializeJsonPretty(doc, Serial);
}
In my sketch right now i am able to create the Json Array with the values read from a Analog input. Only the created Json Array have 9 decimal points. I do not know how to implement in my sketch, folowing the aboth example that the values inside the Json array have only two decimal points.
Follow my sketch.
#include <ArduinoJson.h>
#define ADC_Range_Pin 35 // Pin Range Sensor
unsigned long scale_previousMillis = 0;
const long scale_interval = 100; // Interval
const byte NUM_SAMPLES = 20;
double buffer_range [NUM_SAMPLES];
// Range Sensor Instanzen-----------------------------
double Current_range = 0; //4-20 mA
double rawADCValue_range = 0;
double ADCVoltage_range = 0; //0-3,3 V
double range = 0; //0-50mm
unsigned long range_t;
double mapfloat(double x, double in_min, double in_max, double out_min, double out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
double round_range(double value) {
return (int)(value * 100 + 0.5) / 100.0;
}
void Read_Scale() {
StaticJsonDocument<1000> doc;
static int scale_counter = 0;
if (millis() - scale_previousMillis >= scale_interval) {
scale_previousMillis = millis();
rawADCValue_range = 0;
for (int i = 0; i < 100; ) {
if (millis() - range_t >= 1) {
rawADCValue_range += analogRead(ADC_Range_Pin);
i++;
range_t = millis();
}
}
ADCVoltage_range = (double)(rawADCValue_range / 100.0) * (3.3 / 4095.0);
Current_range = mapfloat(ADCVoltage_range, 0.8, 3.3, 4, 20);
range = mapfloat(Current_range, 4, 20, 0, 50);
buffer_range[scale_counter] = range ;
scale_counter ++;
JsonArray rangeValues = doc.createNestedArray("Range");
if (scale_counter >= NUM_SAMPLES)
{
for (int i = 0; i < NUM_SAMPLES; i++) {
rangeValues.add (buffer_range[i]);
}
serializeJson(doc, Serial);
Serial.println();
scale_counter = 0;
Serial.println();
}
}
}
void setup() {
Serial.begin(115200);
}
void loop() {
Read_Scale();
}