IoT Cloud Display value to 1 decimal place?

Afternoon all,

Just playing around with IoT Cloud for the first time, temperature & voltage are sent to the cloud dash board and are displayed. My issue is that its listing both values to 3 decimal places, i can't seem to find anywhere in the dash board to change the display behavior so I'm now looking to only send (round?) value to 1 decimal place.

Current code is:

void readtemperature()
{
  dummy = analogRead(Temp_Pin);                                     // First read & disregard to let mux settle
  delay(10);                                                        // Delay to let voltage settle.
  temperature = (analogRead(Temp_Pin) * 0.32258064516129);          // Calculate temp from LM35 sensor deg c.
}

Any idea how i might achieve this. Variable is declared as 'float temperature', it looks like float is the only datatype to support decimal places, would a different datatype be easier to work with?

Thanks

Dave

Why is the function void?
Don't you want it to return the temperature?

(Your code is incomplete)

Please post your entire sketch.

No I don't need it to return a value, its setting the variable 'temperature' which is setup via the IoT Cloud which in turn auto generates the sketch.

It does work, but the dashboard currently displays "19.425" whch not only looks a bit crap but is a bit pointless, just looking for a way to display "19.4"

Entire sketch:

#include "arduino_secrets.h"
/* 
  Sketch generated by the Arduino IoT Cloud Thing "***"
  https://create.arduino.cc/cloud/things/*** 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float batteryvoltage;
  float systemvoltage;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

int Temp_Pin = A0;                                      // Temperature sensor is connected to Arduino #A0
int dummy = 0;                                          // Global variable for dummy ADC reads

void setup() {
  analogReference(AR_DEFAULT);                          // Set analogue ref to default 3.3volt. 
  pinMode(Temp_Pin, INPUT);                             // Set the temp sensor pin
  
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  // Your code here
  readtemperature();
  readsysvolts();
}

void readtemperature()
{
  dummy = analogRead(Temp_Pin);                                     // First read & disregard to let mux settle
  delay(10);                                                        // Delay to let voltage settle.
  temperature = (analogRead(Temp_Pin) * 0.32258064516129);          // Calculate temp from LM35 sensor deg c.
}

void readsysvolts()
{
  dummy = analogRead(ADC_BATTERY);                                  // First read & disregard to let mux settle
  delay(10);                                                        // Delay to let voltage settle.
  systemvoltage = analogRead(ADC_BATTERY) * (4.3 / 1023.0);         // Read & calculate system voltage
}

Here's a crude way:

void setup()
{
Serial.begin(115200);
float temperature = 19.45729;
int t = temperature * 10.0;
temperature= t/10.0;
Serial.println(temperature);
}

void loop()
{
}

Note that the serial print defaults to 2 decimals. There's a little bit more to tweak if you want rounding rather than truncation.

Thank you, it might be crude but it works perfectly! :slight_smile: Not too sure why it works, i'm guessing it to do with internal rounding when multiplying then dividing...

Turned it into a little function:

float CrudeTruncate(float input)
{
  int i = input * 10.0;
  return i/10.0;
}

It works because an int by its nature doesn't have a fractional part. So multiplying 19.45729 by 10 gets you 194.5729 but when you put that in an int, it becomes 194. Divide by 10 and put it in a float: 19.4.

Doh! Yes, of corse it does. How did i miss that :smiley:
Cheers!

float CrudeTruncate(float input)
{
  int i = input * 10.0;
  return i/10.0;
}

Gives the "floor" function, for values like temperature, you should round off:

float CrudeRound(float input)
{
  int i = input * 10.0;
  return i/10.0 + 0.5;
}

Ive just been mulling this one over, could you explain the benifit of CrudeRound over CrudeTruncate, surely it just adds 0.5 to the output ie making the temperature 0.5 degrees higher than it would have been...

It's a common technique to avoid having something like 6.99 be truncated to 6 rather than rounded to 7 that makes more sense.

Try using the two different methods by hand with 6.3 and 6.8 to illustrate what's going on.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.