Humidity and Temperature on a 4 x 7 segment digit LED

Merry xmas to everybody, i'm working on a project in which i have to display temperature and humidity on a four 7seg digit LED. I want they show once temp once hum for 5 seconds.
i have already written the code, and for now i use a for cicle to calculate the 5000ms (one in the "temperatura" function and the other one in the "umidita" function), but it's not precise, so i would like to improve the code with the millis function but i have some troubles. now i put the current version of the code.
thanks to all people will join this topic :wink:

/* -------------------------------------------------------------------------    Pimpino v 1.1     ------------------------------------------------------------------------------- */
// Mostra umidità e temperatura ogni 5 secondi grazie ad un sensore DHT 11
// delay totale intorno ai 20ms, quindi 5ms per ogni DIG


#include <dht11.h>
#define DHT11_PIN 10                                            // pin DIGITALE del sensore DHT11
int temp, hum, humdec, tempdec, humun, tempun, u, t, n;                    // Su Windows funzionano anche come CHAR, ma su Linux no !!! li riconosce solo come byte int ecc...
dht11 DHT;

const int d1 = 14;
const int d2 = 15;
const int d3 = 11;                // v1.1 PIN modificati per usare RTC
const int d4 = 12;                // V1.1 PIN modificati per usare RTC

/*
 * const int sda =  ;                 // pin SDA per rtc
 * const int scl =  ;                 // pin SCL per rtc
 */

const int a=2;  
const int b=3;  
const int c=4;  
const int d=5;  
const int e=6;  
const int f=7;  
const int g=8;  
const int h=9;  





void ledon(int n)                         // accende i led
{
  switch(n)
  {
  case 0:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW); 
    digitalWrite(d, LOW); 
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
    break;

  case 1:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW); 
    digitalWrite(d, HIGH); 
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
    break;

  case 2:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, HIGH); 
    digitalWrite(d, LOW); 
    digitalWrite(e, LOW);
    digitalWrite(f, HIGH);
    digitalWrite(g, LOW);
    break;
  case 3:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW); 
    digitalWrite(d, LOW); 
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, LOW);
    break;

  case 4:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW); 
    digitalWrite(d, HIGH); 
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;

  case 5:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW); 
    digitalWrite(d, LOW); 
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;

  case 6:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW); 
    digitalWrite(d, LOW); 
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;

  case 7:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW); 
    digitalWrite(d, HIGH); 
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
    break;

  case 8:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW); 
    digitalWrite(d, LOW); 
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;

  case 9:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW); 
    digitalWrite(d, LOW); 
    digitalWrite(e, HIGH);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    break;
  }
};


void ledoff()                             // spegne tutti i led, DIG + SEG
{
  
digitalWrite ( a , HIGH );
digitalWrite ( b , HIGH );
digitalWrite ( c , HIGH );
digitalWrite ( d , HIGH );
digitalWrite ( e , HIGH );
digitalWrite ( f , HIGH );
digitalWrite ( g , HIGH );
digitalWrite ( h , HIGH );

digitalWrite ( d1 , LOW );
digitalWrite ( d2 , LOW );
digitalWrite ( d3 , LOW );
digitalWrite ( d4 , LOW );

}


void umidita ()                                     // inizio funzione UMIDITÀ
{
  

for (u=0; u <  325  ; u++)

{

   ledoff();
    digitalWrite( d1 ,  HIGH );                    // H
  
    digitalWrite( c ,  LOW );
    digitalWrite( f ,  LOW );
    digitalWrite( a ,  HIGH );
    digitalWrite( b,  LOW  );
    digitalWrite( d,  HIGH );
    digitalWrite( e,  LOW  );
    digitalWrite( g,  LOW  );
    digitalWrite( h,  LOW  );
      delay (5);


  

   ledoff();
    digitalWrite (d3, HIGH);
    ledon(humdec);                                  // Decine UMIDITA  >> HUM <<
    delay (5);
  

   ledoff ();
    digitalWrite (d4, HIGH);
    ledon(humun);                                 // Unità UMIDITA   >> HUM << 
    delay(5);

   ledoff();

}
}                                                                // fine funzione UMIDITÀ


void temperatura ()                                     // inizio funzione TEMPERATURA
{
  for (t=0; t <  250  ; t++)
    {
      ledoff();
        digitalWrite (d1, HIGH);
        ledon(tempdec);
        delay (5);

       ledoff();
        digitalWrite (d2, HIGH);
        ledon (tempun);
        delay (5);

       ledoff();
        digitalWrite (d3, HIGH);
        digitalWrite ( a , LOW );                //   AL CONTRARIO !!!  accensione °
        digitalWrite ( b , LOW );                
        digitalWrite ( c , HIGH );
        digitalWrite ( d , HIGH );
        digitalWrite ( e , HIGH );
        digitalWrite ( f , LOW );                
        digitalWrite ( g , LOW );
        digitalWrite ( h , HIGH );
          delay (5);

        ledoff();
          digitalWrite (d4, HIGH);
          digitalWrite ( a , LOW );           //  accendo C      
          digitalWrite ( b , HIGH );
          digitalWrite ( c , HIGH );
          digitalWrite ( d , LOW );
          digitalWrite ( e , LOW );
          digitalWrite ( f , LOW );                
          digitalWrite ( g , HIGH );
          digitalWrite ( h , HIGH );
            delay (5);

        ledoff();
    }                                                     // fine funzione TEMPERATURA
}


void check ()
{
  int chk;
  chk = DHT.read(DHT11_PIN);    // READ DATA
    temp = DHT.temperature;        
    hum = DHT.humidity;            
                                      //    "temp" e "hum"
    Serial.print ("Temperatura:  ");
    Serial.println (temp);
    Serial.print ("Umidita:  ");
    Serial.println (hum);
    Serial.println ();

humdec = hum / 10 ;                                 // umidità decine e unità
humun = hum % 10 ;

tempdec = temp / 10 ;                               // temperatura: decine e unità
tempun = temp % 10;
}


void setup()
{
  Serial.begin(9600);                                         // inizializzazione porta seriale
  Serial.println("Inizializzazione programma...");
  delay(3000);
  
  pinMode(a, OUTPUT);                     // settaggio segment as OUTPUT
  pinMode(b, OUTPUT); 
  pinMode(c, OUTPUT); 
  pinMode(d, OUTPUT); 
  pinMode(e, OUTPUT); 
  pinMode(f, OUTPUT); 
  pinMode(g, OUTPUT);
  pinMode(h, OUTPUT);
  
  pinMode(d1, OUTPUT);                    // settaggio digit as OUTPUT
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);

ledoff ();

}



void loop ()
{
  
   
check ();                                         // controlla temperatura e umidità

temperatura ();                                   // richiamo funzione TEMPERATURA

umidita ();                                       // richiamo funzione UMIDITÀ


  
}
void loop ()
{
 
   
check ();                                         // controlla temperatura e umidità

temperatura ();                                   // richiamo funzione TEMPERATURA

umidita ();                                       // richiamo funzione UMIDITÀ


 
}

What's with

all the blank

lines?

Why
does
all
the
code
start
in
column
one?

Whether to display the temperature or the humidity should be decided by loop(), not the functions. When to change the displayed value should be decided by loop(), not the functions.

have a look at that code structure. (you can run it to see what it does set the Serial Monitor to 115200 baud)

unsigned long lastDisplayTime = 0ul;
const unsigned long interval = 5000ul; // time between switching display

unsigned long lastTickTime = 0ul;
const unsigned long tickInterval = 500ul; // time between blinking something to show it's alive

enum {displayTemperature, displayHumidity} stage;

void setup() {
  Serial.begin(115200);
  stage = displayTemperature;
}

void loop() {
  
  if (millis() - lastTickTime >= tickInterval) {
    Serial.print("."); // do something to show it's active like blink a dot on your module
    lastTickTime += tickInterval;
  }
  
  if (millis() - lastDisplayTime >= interval) {
    switch (stage) {
      case displayTemperature:
        Serial.println("display Temperature");
        stage = displayHumidity;
        break;
      case displayHumidity:
        Serial.println("display Humidity");
        stage = displayTemperature;
        break;
    }
    lastDisplayTime += interval;
  }
}

PaulS:

void loop ()

{

check ();                                        // controlla temperatura e umidità

temperatura ();                                  // richiamo funzione TEMPERATURA

umidita ();                                      // richiamo funzione UMIDITÀ

}



What's with





all the blank





lines?


Why
does
all
the
code
start
in
column
one?



Whether to display the temperature or the humidity should be decided by loop(), not the functions. When to change the displayed value should be decided by loop(), not the functions.

Hi
there
it
helps
me,
sounds
like
cleaner
view
.

By the way, i don't know how to create a loop that switch on the LEDs for 5 sec.
i "solved" by calculating with millis the exact time of each function in ms and dividing it by 5000.
and then making an appropriate number of "for" cicle.
313 times for humidity and 240 times for temperature.
but i'm assuming there is a cleaner way to make things working.
happy befana, enjoy candies !

J-M-L:
have a look at that code structure. (you can run it to see what it does set the Serial Monitor to 115200 baud)

unsigned long lastDisplayTime = 0ul;

const unsigned long interval = 5000ul; // time between switching display

unsigned long lastTickTime = 0ul;
const unsigned long tickInterval = 500ul; // time between blinking something to show it's alive

enum {displayTemperature, displayHumidity} stage;

void setup() {
 Serial.begin(115200);
 stage = displayTemperature;
}

void loop() {
 
 if (millis() - lastTickTime >= tickInterval) {
   Serial.print("."); // do something to show it's active like blink a dot on your module
   lastTickTime += tickInterval;
 }
 
 if (millis() - lastDisplayTime >= interval) {
   switch (stage) {
     case displayTemperature:
       Serial.println("display Temperature");
       stage = displayHumidity;
       break;
     case displayHumidity:
       Serial.println("display Humidity");
       stage = displayTemperature;
       break;
   }
   lastDisplayTime += interval;
 }
}

hi there, i'm sorry i'm newbie, i can't understand why set the monitor to higher rates.
thanks for joining my topic :slight_smile:

hi there, i'm sorry i'm newbie, i can't understand why set the monitor to higher rates.

Well to get the communication to go faster... why go slow? :slight_smile:

Did you study and tried the code I offered?

J-M-L:
Well to get the communication to go faster... why go slow? :slight_smile:

Did you study and tried the code I offered?

hi there it's not as accurate as my first was.
sometimes goes over 5sec, and sometimes below.
i will try it more in the next days, i'm little busy at the moment, but thank you so much !

I am not sure what you mean... 5 seconds to do what?