Hola!
This is a fairly common project, with a common problem as well. Why doesn't the program continue to count the time after the power supply is interrupted? Obviously I tried to solve the problem by searching on google, but without progress.
I do the following: connect the arduino nano to the pc, load the code, then put it in the circuit and with an external source of 12Vdc I supply the circuit through the Nano`s Vin pin. How to set the time in setup (): manual, auto or not?
#include "TM1637.h"
#include <DHT.h>
#define DHTPIN A2 // what pin we're connected the DHT output
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
//{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
//0~9,A,b,C,d,E,F,"-"," ",degree,r,h
#define CLK 2//Pins for TM1637
#define DIO 3
TM1637 tm1637(CLK, DIO);
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
int hh, mm;
unsigned long clockBlinkStart;
const unsigned long clockBlinkDuration = 500; // milliseconds
bool showPoint;
unsigned long TempHumidityStart;
const unsigned long TempHumidityDuration = 3000; // milliseconds
bool showTemperature; // true = show temperature, false = show humidty
bool showTime; // true = display time, false = display Temp/Humidity
void setup()
{
Serial.begin(9600);
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);
//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
delay(1500);//Delay to let system boot
dht.begin();
rtc.begin();
// 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));
}
// manual adjust
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2020, 2, 29, 19, 31, 0));
// automatic adjust
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
showPoint = true;
showTime = true;
showTemperature = true;
displayTime( rtc.now() );
}
void loop() {
unsigned long currentMillis = millis();
DateTime now = rtc.now();
int hh = now.hour();
int mm = now.minute();
int ss = now.second();
if ( showTime == true ) {
// display the time
if ( currentMillis - clockBlinkStart >= clockBlinkDuration ) {
clockBlinkStart = currentMillis;
showPoint = !showPoint;
displayTime(now);
}
// see if it is time to display temperature and humidity, every minute
if ( ss == 0 ) {
showTime = false;
showTemperature = true;
displayTempHumidity();
TempHumidityStart = currentMillis;
}
}
// check temperature/humidity display time
if ( showTime == false ) {
if ( currentMillis - TempHumidityStart >= TempHumidityDuration ) {
TempHumidityStart = currentMillis;
if ( showTemperature == true ) {
// we have been showing temerature, so time to show humidity
showTemperature = false;
displayTempHumidity();
TempHumidityStart = currentMillis;
}
else {
// we already showed humidity so cycle back to displaying time
showTime = true;
}
}
}
}
void displayTime(DateTime now) {
int hh = now.hour();
int mm = now.minute();
if ( showPoint == true ) tm1637.point(POINT_ON);
else tm1637.point(POINT_OFF);
if ((hh / 10) == 0) tm1637.display(0, 17);
else
tm1637.display(0, hh / 10); // hour
tm1637.display(1, hh % 10);
tm1637.display(2, mm / 10); // minutes
tm1637.display(3, mm % 10); //
Serial.print(hh);
Serial.print(':');
Serial.print(mm);
Serial.println();
}
void displayTempHumidity() {
int value;
if ( showTemperature == true ) {
value = dht.readTemperature();
// value = 23;
}
else {
value = dht.readHumidity();
// value = 48;
}
tm1637.display(0, value / 10);
tm1637.display(1, value % 10);
if ( showTemperature == true ) {
tm1637.display(2, 18); // put degree
tm1637.display(3, 12); // put a C at the end
}
else {
tm1637.display(2, 19); // r
tm1637.display(3, 20); // h
}
Serial.println(value);
}