I use RTC3231 and a temperature sensor DS18B20 and display-TM1637
I don't know how to do this - First, show the time on the display. Then show the temperature.
Please help me if possible. Thanks.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TM1637Display.h>
#include "RTClib.h"
#define ONE_WIRE_BUS 1 //Digital Pin to connect the DS18B20 Sensor
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1;
TM1637Display display(9,8);
const uint8_t DEGREES[] = {
0x0, 0x0,
SEG_A | SEG_B | SEG_G | SEG_F, // Degree Symbol
SEG_A | SEG_F | SEG_E | SEG_D, // C
};
unsigned int temperature = 0;
byte PreviousValue = 0;
RTC_DS3231 rtc;
void setup()
{
sensors.begin();
if (!sensors.getAddress(sensor1, 0))
{
Serial.println("Sensor not found!");
}
// Check if RTC is connected correctly:
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time:
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// The following line sets the RTC to the date & time this sketch was compiled:
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// Set the display brightness (0-7):
display.setBrightness(5);
// Clear the display:
display.clear();
}
void loop()
{
sensors.requestTemperatures();
int tempC = sensors.getTempC(sensor1);
if (tempC != PreviousValue)
PreviousValue = tempC;
DateTime now = rtc.now();
int displaytime = (now.hour() * 100) + now.minute();
display.showNumberDecEx(displaytime, 0b11100000, true);
delay(1000);
display.showNumberDec(displaytime, true);
delay(2000);
display.setSegments(DEGREES); //Display the Variable value
display.showNumberDec(tempC, false, 2, 0);
delay(1000);
}
The problem is solved.
It showed only the hour. The temperature did not appear on the display. But all is well. I changed
#define ONE_WIRE_BUS 1 to
#define ONE_WIRE_BUS 4
Thank you all for your help.