Display won't show temprature values.

Hi,

I made a sketch for a temparature sensor, but instead of using the serial monitor i want to use 2 7 segment displays. I could make the display count up to 20 whithout a problem but i cant see to use it as an output for the sensor values.

I made some while statements when x equals a certain values the display will give the vallue. I have set x as an integer because it only needs to show values like 1, 10, 20 not 1,1 20,6 etc etc. I also tried declaring it as a float that also didn’t work.

this is the sketch

#include "cactus_io_DHT22.h"

#define DHT22_PIN 53 // what pin on the arduino is the DHT22 data line connected to
DHT22 dht(DHT22_PIN);

// eerste segment

int led1 = 2;     //rondje rechtsonder
int led2 = 3;     //secie D
int led3 = 4;     //sectie C
int led4 = 5;     //sectie G
int led5 = 6;     //sectie B
int led6 = 7;     //sectie E
int led7 = 8;     //sectie F
int led8 = 9;     //sectie A
int x = 0;
// tweede segment

int led9 = 36;     //rondje rechtsonder
int led10 = 37;     //secie D
int led11 = 35;     //sectie C
int led12 = 34;     //sectie G
int led13 = 33;    //sectie B
int led14 = 32;     //sectie E
int led15 = 31;     //sectie F
int led16 = 30;     //sectie A

void setup() {


  pinMode(led9, INPUT);
  pinMode(led10, INPUT);
  pinMode(led11, INPUT);
  pinMode(led12, INPUT);
  pinMode(led13, INPUT);
  pinMode(led14, INPUT);
  pinMode(led15, INPUT);
  pinMode(led16, INPUT);

  pinMode(led9, LOW);
  pinMode(led10, LOW);
  pinMode(led11, LOW);
  pinMode(led12, LOW);
  pinMode(led13, LOW);
  pinMode(led14, LOW);
  pinMode(led15, LOW);
  pinMode(led16, LOW);

  Serial.begin(9600);
  Serial.println("DHT22 Humidity - Temperature Sensor");
  Serial.println("RH\tTemp (C)\tTemp (F)\tHeat Index (C)\tHeat Index (F)");

  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  dht.readHumidity();
  dht.readTemperature();
  x = dht.temperature_C ;


  // Check if any reads failed and exit early (to try again).
  if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
    Serial.println("DHT sensor read failure!");
    return;
  }
  Serial.println(x);
  Serial.print(dht.humidity); Serial.print(" %\t\t");
  Serial.print(dht.temperature_C); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_F()); Serial.println(" *F");

   while ( x >= 20 && x < 21)
  {
    pinMode(led2, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led6, HIGH);
    pinMode(led8, HIGH);

    pinMode(led10, HIGH);
    pinMode(led11, HIGH);
    pinMode(led13, HIGH);
    pinMode(led14, HIGH);
    pinMode(led15, HIGH);
    pinMode(led16, HIGH);
  }

 while ( x >= 21 && x < 22)
  {
    pinMode(led2, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led6, HIGH);
    pinMode(led8, HIGH);

    pinMode(led10, HIGH);
    pinMode(led12, HIGH);
    pinMode(led13, HIGH);
    pinMode(led14, HIGH);
    pinMode(led16, HIGH);

  }
while ( x >= 22 && x < 23)
  {
    pinMode(led10, HIGH);
    pinMode(led12, HIGH);
    pinMode(led13, HIGH);
    pinMode(led14, HIGH);
    pinMode(led16, HIGH);

    pinMode(led2, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led6, HIGH);
    pinMode(led8, HIGH);


  }



  // Wait a few seconds between measurements. The DHT22 should not be read at a higher frequency of
  // about once every 2 seconds. So we add a 3 second delay to cover this.

  delay(3000);

}

Any ideas how i could solve this or were to look into?

thanks in advance.

I think you need to review the built in functions pinMode() and digitalWrite(). pinMode() is used to set the DIRECTION of a port pin, either INPUT or OUTPUT. pinMode() does not set the pin high or low, that is what digitalWrite() is for.

In each of those while loops, the value of x never changes. So if it is right to get you into one of them, then each one becomes an infinite loop and just keeps changing the pinMode of all those pins over and over until you unplug the board.

Delta_G:
In each of those while loops, the value of x never changes. So if it is right to get you into one of them, then each one becomes an infinite loop and just keeps changing the pinMode of all those pins over and over until you unplug the board.

x changes, it stores the values of the temparature sensor so when the temp is 22 degrees it goes in on while loop if the temp changes and becomes 24 for instance then it goes in another loop. right?

No, go look at how a while loop works. It only runs the code inside the while loop as long as the condition is true. That line that reads the temperature and sets x is outside the while loop. So you aren't going to ever get back to that point to take another reading.

Hi,
Why are you using "while" loops instead of "if" ?

The "if" allows your void loop() to keep looping and so check the value of x.

The "while" goes into the loop if x is in range and stays in "while" until x is out of range.
However you do not check the value of x while in the "while" loop, to see if x changes, so x does not change and you stay locked in the "while" loop.

Check what @Delta_G has said.
Forget about "while", I try to avoid using it.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Look up how to use pinMode and digitalWrite .
Tom..... :slight_smile:

TomGeorge:
Hi,
Why are you using "while" loops instead of "if" ?

The "if" allows your void loop() to keep looping and so check the value of x.

The "while" goes into the loop if x is in range and stays in "while" until x is out of range.
However you do not check the value of x while in the "while" loop, to see if x changes, so x does not change and you stay locked in the "while" loop.

Check what @Delta_G has said.
Forget about "while", I try to avoid using it.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Look up how to use pinMode and digitalWrite .
Tom..... :slight_smile:

Thanks for the info, i'm going to make an circuit diagram for you.

@delta_G I looked into that and i get what you are saying you are right.

In the attachment is the wiring diagram. The resistor at the temp sensor is 10.7K the rest are 249ohm.

Hi,
That Fritzy is pretty useless, not enough detail to read any of the very few labels or pin names.

Fritzy diagrams show layout , but not proper signal flow and component information.

PLEASE draw a diagram and post a picture, note pin numbers/names , component numbers and power supplies.

Thanks.. Tom... :slight_smile:

Does this maybe help? cause im really bad at drawing those things

Hi,


Thanks, its a start, but the wiring is all over the place, you need to move components around so the wires do not cross at angles, right angles is okay, or cross over components.

You might be better off hand drawing your circuit and posting a picture.

Don't send screen captures, if you use that cad it should have an Export Image facility which will be much clearer image.

Tom.. :slight_smile:

Hi,
The schematic is not super important at the moment, as you say you can get the display to work.
Have you tried any code changes?

Tom.. :slight_smile:

TomGeorge:
Hi,
The schematic is not super important at the moment, as you say you can get the display to work.
Have you tried any code changes?

Tom… :slight_smile:

I have tried some code changes but im really stuck now… dont see the problem. I have been working on it for a week or so and i catn seem to find the bug

#include "cactus_io_DHT22.h"

#define DHT22_PIN 53 // what pin on the arduino is the DHT22 data line connected to
DHT22 dht(DHT22_PIN);

// eerste segment

int led1 = 2;     //rondje rechtsonder
int led2 = 3;     //secie D
int led3 = 4;     //sectie C
int led4 = 5;     //sectie G
int led5 = 6;     //sectie B
int led6 = 7;     //sectie E
int led7 = 8;     //sectie F
int led8 = 9;     //sectie A
int x = 0;
// tweede segment

int led9 = 36;     //rondje rechtsonder
int led10 = 37;     //secie D
int led11 = 35;     //sectie C
int led12 = 34;     //sectie G
int led13 = 33;    //sectie B
int led14 = 32;     //sectie E
int led15 = 31;     //sectie F
int led16 = 30;     //sectie A

void setup() {


  pinMode(led9, INPUT);
  pinMode(led10, INPUT);
  pinMode(led11, INPUT);
  pinMode(led12, INPUT);
  pinMode(led13, INPUT);
  pinMode(led14, INPUT);
  pinMode(led15, INPUT);
  pinMode(led16, INPUT);

  digitalWrite(led9, LOW);
  digitalWrite(led10, LOW);
  digitalWrite(led11, LOW);
  digitalWrite(led12, LOW);
  digitalWrite(led13, LOW);
  digitalWrite(led14, LOW);
  digitalWrite(led15, LOW);
  digitalWrite(led16, LOW);

  Serial.begin(9600);
  Serial.println("DHT22 Humidity - Temperature Sensor");
  Serial.println("RH\tTemp (C)\tTemp (F)\tHeat Index (C)\tHeat Index (F)");

  dht.begin();  
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  dht.readHumidity();
  dht.readTemperature();
  x = dht.temperature_C ;


  // Check if any reads failed and exit early (to try again).
  if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
    Serial.println("DHT sensor read failure!");
    return;
  }
  Serial.println(x);
  Serial.print(dht.humidity); Serial.print(" %\t\t");
  Serial.print(dht.temperature_C); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_F()); Serial.println(" *F");

   if ( x >= 20 && x < 21)
  {
    dht.readHumidity();
  dht.readTemperature();
  x = dht.temperature_C ;


  
  if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
    Serial.println("DHT sensor read failure!");
    return;
  }
  Serial.println(x);
  Serial.print(dht.humidity); Serial.print(" %\t\t");
  Serial.print(dht.temperature_C); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_F()); Serial.println(" *F");
  
    pinMode(led2, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led6, HIGH);
    pinMode(led8, HIGH);

    pinMode(led10, HIGH);
    pinMode(led11, HIGH);
    pinMode(led13, HIGH);
    pinMode(led14, HIGH);
    pinMode(led15, HIGH);
    pinMode(led16, HIGH);
  }

 if ( x >= 21 && x < 22)
  {
    dht.readHumidity();
  dht.readTemperature();
  x = dht.temperature_C ;


  
  Serial.println(x);
  Serial.print(dht.humidity); Serial.print(" %\t\t");
  Serial.print(dht.temperature_C); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_F()); Serial.println(" *F");
  
    pinMode(led2, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led6, HIGH);
    pinMode(led8, HIGH);

    pinMode(led10, HIGH);
    pinMode(led12, HIGH);
    pinMode(led13, HIGH);
    pinMode(led14, HIGH);
    pinMode(led16, HIGH);

  }
if ( x >= 22 && x < 23)
  {
    dht.readHumidity();
  dht.readTemperature();
  x = dht.temperature_C ;


  // Check if any reads failed and exit early (to try again).
  if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
    Serial.println("DHT sensor read failure!");
    return;
  }
  Serial.println(x);
  Serial.print(dht.humidity); Serial.print(" %\t\t");
  Serial.print(dht.temperature_C); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_F()); Serial.println(" *F");
  
    pinMode(led10, HIGH);
    pinMode(led12, HIGH);
    pinMode(led13, HIGH);
    pinMode(led14, HIGH);
    pinMode(led16, HIGH);

    pinMode(led2, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led6, HIGH);
    pinMode(led8, HIGH);


  }



  // Wait a few seconds between measurements. The DHT22 should not be read at a higher frequency of
  // about once every 2 seconds. So we add a 3 second delay to cover this.

  delay(3000);

}

this is the code were i am now i perhaps made it even worse, i’m clueless. Hope you could help me out or give me advice on were to look at as we say it in dutch ‘‘ik zie door de bomen het bos niet meer’’ which translates litterly to i cant see the trees true the forrest anymore, what i mean with that is that i cant see the whole thing anymore dont know were the problem is and/or how to solve it.

pinMode(led4, HIGH);

You need to go back to reply 1. Why are you ignoring the advise you've been given? Why should we try if you won't even try the things we give you?

You need to put this project aside and work through the reference material and some of the basic examples in the IDE and at least get a clue about the basics before you start working on program structure.

Delta_G:

pinMode(led4, HIGH);

You need to go back to reply 1. Why are you ignoring the advise you've been given? Why should we try if you won't even try the things we give you?

You need to put this project aside and work through the reference material and some of the basic examples in the IDE and at least get a clue about the basics before you start working on program structure.

Yes, the pinMode stuff worries me when the OP says;

I could make the display count up to 20 whithout a problem

Can you post the code that you had the display counting?
Tom... :o :o :o

You are still using pinMode() where you should be using digitalWrite() in the loop section.

However, you will not be sending anything meaningful to the 7-Segment displays other than all segments on, which you will see as 88. You have to have a decoder sequence that will provide the proper bit pattern to each display for each number, such as a series of switch/case statements.

Also, your current iteration of the sketch does not set the pinMode() for both displays in void setup().

Had to delete some code otherwise i couldn’t post it

// eerste segment

int led1 = 2;     //rondje rechtsonder
int led2 = 3;     //secie D
int led3 = 4;     //sectie C
int led4 = 5;     //sectie G
int led5 = 6;     //sectie B
int led6 = 7;     //sectie E
int led7 = 8;     //sectie F
int led8 = 9;     //sectie A

// tweede segment

int led9 = 36;     //rondje rechtsonder
int led10 = 37;     //secie D
int led11 = 35;     //sectie C
int led12 = 34;     //sectie G
int led13 = 33;    //sectie B
int led14 = 32;     //sectie E
int led15 = 31;     //sectie F
int led16 = 30;     //sectie A
int x = 1;

void setup() {

  // eerste segment

  pinMode(led9, INPUT);
  pinMode(led10, INPUT);
  pinMode(led11, INPUT);
  pinMode(led12, INPUT);
  pinMode(led13, INPUT);
  pinMode(led14, INPUT);
  pinMode(led15, INPUT);
  pinMode(led16, INPUT);

  pinMode(led9, LOW);
  pinMode(led10, LOW);
  pinMode(led11, LOW);
  pinMode(led12, LOW);
  pinMode(led13, LOW);
  pinMode(led14, LOW);
  pinMode(led15, LOW);
  pinMode(led16, LOW);


  // tweede segment


  pinMode(led1, INPUT);
  pinMode(led2, INPUT);
  pinMode(led3, INPUT);
  pinMode(led4, INPUT);
  pinMode(led5, INPUT);
  pinMode(led6, INPUT);
  pinMode(led7, INPUT);
  pinMode(led8, INPUT);

  pinMode(led1, LOW);
  pinMode(led2, LOW);
  pinMode(led3, LOW);
  pinMode(led4, LOW);
  pinMode(led5, LOW);
  pinMode(led6, LOW);
  pinMode(led7, LOW);
  pinMode(led8, LOW);
}

void loop() {
  // voor getal 1
  while (x == 1 || x == 21 || x == 41 || x == 61)
  {
    pinMode(led3, HIGH);
    pinMode(led5, HIGH);
    delay(500);
    pinMode(led1, LOW);
    pinMode(led2, LOW);
    pinMode(led3, LOW);
    pinMode(led4, LOW);
    pinMode(led5, LOW);
    pinMode(led6, LOW);
    pinMode(led7, LOW);
    pinMode(led8, LOW);
    x++;

  }
  // voor getal 2
  while (x == 2 || x == 22 || x == 42 || x == 62)
  {
    pinMode(led2, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led6, HIGH);
    pinMode(led8, HIGH);
    delay(500);
    pinMode(led1, LOW);
    pinMode(led2, LOW);
    pinMode(led3, LOW);
    pinMode(led4, LOW);
    pinMode(led5, LOW);
    pinMode(led6, LOW);
    pinMode(led7, LOW);
    pinMode(led8, LOW);
    x++;
  }
  // voor getal 3
  while (x == 3 || x == 23 || x == 43 || x == 63)
  {
    pinMode(led2, HIGH);
    pinMode(led3, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led8, HIGH);
    delay(500);
    pinMode(led1, LOW);
    pinMode(led2, LOW);
    pinMode(led3, LOW);
    pinMode(led4, LOW);
    pinMode(led5, LOW);
    pinMode(led6, LOW);
    pinMode(led7, LOW);
    pinMode(led8, LOW);
    x++;
  }
  // voor getal 4
  while (x == 4 || x == 24 || x == 44 || x == 64)
  {
    pinMode(led3, HIGH);
    pinMode(led4, HIGH);
    pinMode(led5, HIGH);
    pinMode(led7, HIGH);
    delay(500);
    pinMode(led1, LOW);
    pinMode(led2, LOW);
    pinMode(led3, LOW);
    pinMode(led4, LOW);
    pinMode(led5, LOW);
    pinMode(led6, LOW);
    pinMode(led7, LOW);
    pinMode(led8, LOW);
    x++;
  }
  // voor getal 5
  while (x == 5 || x == 25 || x == 45 || x == 65)
  {
    pinMode(led2, HIGH);
    pinMode(led3, HIGH);
    pinMode(led4, HIGH);
    pinMode(led7, HIGH);
    pinMode(led8, HIGH);
    delay(500);
    pinMode(led1, LOW);
    pinMode(led2, LOW);
    pinMode(led3, LOW);
    pinMode(led4, LOW);
    pinMode(led5, LOW);
    pinMode(led6, LOW);
    pinMode(led7, LOW);
    pinMode(led8, LOW);
    x++;
  }

Hi,
have you read post#15 and others about pinMode and digitalWrite ? ? ?

Tom... :slight_smile:

TomGeorge:
Hi,
have you read post#15 and others about pinMode and digitalWrite ? ? ?

Tom... :slight_smile:

I did that, tried it but it diddnt solve my problems. I then got frustrated and pressed ctrl+z many times to go back were i started so that why its not in the sketch.

Hi,
Do you know the difference between pinMode and digitalWrite ?

Tom... :slight_smile: