Interfacing TM1637-DS3231-DHT11

Hello,
I came back with an older project of mine, which now I want to improve it. The ingredients are now Arduino Uno, TM1637, DS3231 and DHT11.
I want the display to show me the hours and minutes, and from 10 to 10 minutes to show the temperature and humidity. I have a program for DS3231 and one for DHT11. I tried to put each program in a separate function and then call them as follows:

void loop(){
  
  for(int m=10000; m>0; m--){
    Clock();
    }
  for(int n=5000; n>0; n--){
    Temp();
    }   
  
}//loop

It remains locked on the Clock function. I know this method is not exactly right, but there is something to do?

The problem lies in the part of the code you didn't show us. Please post your complete sketch.

blh64:
The problem lies in the part of the code you didn’t show us. Please post your complete sketch.

This are the two main programs:

#include "TM1637.h"

//{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 9//Pins for TM1637       
#define DIO 8
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; 

void setup()
{
 tm1637.init();
 tm1637.set(5); 
 //BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;

 rtc.begin();
// manual adjust
 // January 21, 2014 at 3am you would call:
 // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// automatic adjust
 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}//end "setup()"

void loop(){
DateTime now = rtc.now();
hh = now.hour(), DEC;
mm = now.minute(), DEC;

tm1637.point(POINT_ON);
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);    // 
delay(500);
   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);    // 
delay(500);
}// end loop()
#include "TM1637.h"
//{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,n
// 10 -> A, 11 -> b, 12 ->C, 13 -> d, 14 -> E, 15 -> F
// 16 -> -, 17 -> space, 18 -> degree sign, 19 -> r, 20 -> h, 21 -> n


#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); 

#define CLK 9//Pins for TM1637       
#define DIO 8

TM1637 tm1637(CLK,DIO);

void setup(){
 
 tm1637.init();
 tm1637.set(BRIGHT_TYPICAL); 
 //BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;

 delay(1500);//Delay to let system boot
 dht.begin();   

}//end "setup()"



void loop(){

 //Start of Program 

//  DHT.read11(dht_pin);

  int humidity = dht.readHumidity();
  int temp = dht.readTemperature();  
//   int temp = 23;
//   int humidity = 48;

int digitoneT = temp / 10;
int digittwoT = temp % 10;

int digitoneH = humidity / 10;
int digittwoH = humidity % 10;
 

   tm1637.display(0,digitoneT); 
   tm1637.display(1,digittwoT);
   tm1637.display(2,18); // put degree
   tm1637.display(3,12);  // put a C at the end
   
   delay (3000);
   
   tm1637.display(0,17);  // empty
   tm1637.display(1,17);  // empty
   tm1637.display(2,17);  // empty
   tm1637.display(3,17);  // empty


   tm1637.display(0,digitoneH); 
   tm1637.display(1,digittwoH);
   tm1637.display(2,19);  // r
   tm1637.display(3,20);  // h

 
 //Fastest should be once every two seconds.
   delay(3000);

}// end loop()

I also tried to use a software interrupt:
Timer1.initialize(1000000);
Timer1.attachInterrupt(Temp);//this in setup ,where Temp is a function, meanwhile in the void loop I will run the Clock function.

You have to model your code after the BlinkWithoutDelay example so you can be checking the current state. I am not sure exactly when you want to display the Temerature/Humidity vs. the clock, but this code (untested) should display the temp/humidity at the beginning of every minute with a duration of 3 seconds each for the temperature and humidity and then revert back to displaying the time with the blinking point every 1/2 second.

Adjust as needed

#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 9//Pins for TM1637      
#define DIO 8
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()
{
  tm1637.init();
  tm1637.set(5);
  //BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;

  delay(1500);//Delay to let system boot

  dht.begin();

  rtc.begin();
  // manual adjust
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 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 ( displayPoint == 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); //
}


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
  }
}

blh64:
You have to model your code after the BlinkWithoutDelay example so you can be checking the current state. I am not sure exactly when you want to display the Temerature/Humidity vs. the clock, but this code (untested) should display the temp/humidity at the beginning of every minute with a duration of 3 seconds each for the temperature and humidity and then revert back to displaying the time with the blinking point every 1/2 second.

Adjust as needed

#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 9//Pins for TM1637     
#define DIO 8
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()
{
  tm1637.init();
  tm1637.set(5);
  //BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;

delay(1500);//Delay to let system boot

dht.begin();

rtc.begin();
  // manual adjust
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 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 ( displayPoint == 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); //
}

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
  }
}

Thank you sir, it works. At some point, you replaced showPoint with displayPoint but I fixed it.
In order to increase the delay between clock and temp & hum, do I have to write if (mm == 0)? Maybe I want to show temp & hum every 5 minutes. Another thing, when I display the temp & hum, will the clock run in the background or stop for 3 seconds from minute to minute?

cristian10001:
Thank you sir, it works. At some point, you replaced showPoint with displayPoint but I fixed it.

Good problem solving. That is why I labeled my code "untested"

In order to increase the delay between clock and temp & hum, do I have to write if (mm == 0)? Maybe I want to show temp & hum every 5 minutes.

It depends on when you want it displayed. It will be a combination of minutes (mm) and seconds (ss). If you want it every 5 minutes, that would be mm % 5 == 0 && ss == 0.

The one thing you have to watch out for is if you only test for the minutes being a multiple of 5. The temp and humidity will display and then revert back to the clock so quickly that the minutes will not have updated to the next minute and it will trigger another cycles of temp/humidity being displayed.

Another thing, when I display the temp & hum, will the clock run in the background or stop for 3 seconds from minute to minute?

The RTC runs all the time as long as it is connected to a battery.