I'm trying to display the temp from a DH11 sensor on the Grove display & have just combined the 2 example sketches.
Problem I'm having is that when the serial console shows a temp as say 18.00 the display shows 17.99.
Also if I give t a fixed value e.g. 18.45 display shows 18.44 so seems to be always .01 less then the value of t
Any ideas welcome!
// Example testing sketch for various DHT humidity/temperature sensors
// PREP FOR TEMP SENSOR
#include "DHT.h" // - DHT Sensor Library:
#define DHTPIN 7 // Digital pin connected to the DHT sensor
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
// \ END PREP FOR TEMP SENSOR
//PREP FOR 7 SEG DISPLAY
#include "TM1637.h"
// Pins definitions for TM1637 and can be changed to other ports
const int CLK = 6;
const int DIO = 7;
TM1637 tm1637(CLK, DIO);
// \END PREP FOR 7 SEG DISPLAY
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin(); //temp sensor
tm1637.init(); //display
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
// countdown to loop start
int num = 5;
while (num >= 0) {
tm1637.displayNum(num);
delay(1000);
num = num - 1;
}
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
// Colon light up as decimal point
t = 18.45; // put this in to give t a fixed value, display shows 18:44
tm1637.displayNum(t, 2);
}
You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.
In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can easily be quite garbled and is always more difficult to read due to the font.
It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.
Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.
Thanks, odd thing is that I'm mostly using the code from the demo library sketches which use float for both the temp variable and the variable sent to the display.
Adding 0.01 to the variable before sending to the display seems to correct the issue, but is a bit of a fudge
So, I can't fathom why or how but I reckon calling displayNum() with an int argument is the root of the issue. You do this in the countdown loop and if you comment out the countdown completely or
change tm1637.displayNum(num);
to tm1637.display( 3,num ); the call to displayNum() with a float argument magically starts behaving itself.
More of a workaround than a fix so I'd advise trying another library.
A popular choice round here is this one.