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?
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
}
Thank you, it might be crude but it works perfectly! 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.
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...