What is wrong with my code? It want print to the monitor

#include <DHT22.h>

#define DHT22_PIN 4 //#define inputPin 4
#define DHT=myDHT22
DHT22 myDHT22(DHT22_PIN);
#define HeaterPin 6
#define CoolerPin 7
#define DHTTYPE DHT22

float tempC;
float tempF;
const float upperLimit=100;
const float lowerLimit=34;

void setup(){
Serial.begin(9600);

pinMode(HeaterPin,OUTPUT);
digitalWrite(HeaterPin,LOW);
pinMode(CoolerPin,OUTPUT);
digitalWrite(CoolerPin,LOW);

}

void loop(){

DHT22_ERROR_t error;
error=myDHT22.readData();

if (error==0){
tempC=myDHT22.getTemperatureC();
tempF=(tempC * 1.8) + 32.0;
Serial.print("Temperature=");
Serial.println(tempF);

if(tempF<lowerLimit){
digitalWrite(HeaterPin,HIGH);
}
if(tempF>lowerLimit + 3){
digitalWrite(HeaterPin,LOW);
}
if(tempF>upperLimit){
digitalWrite(CoolerPin,HIGH);
}
if(tempF<upperLimit-3){
digitalWrite(CoolerPin,LOW);
}
}
delay(2000); //End of if error==0
}

Use code tags when posting code.

Does that work? What baud rate did you select? Did you select Debug mode: Debug?

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

If there are errors, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Thanks

1 Like

Does it work?

The errors on the serial monitor 7
7
1
1
1
1
7
7
etc

The sensor can only be read from every 1-2s.
try delay(2000);

DHT22_ERROR_t error;
error=myDHT22.readData();
delay(2000);

here and not at the end of the if.
Because if you enter the " if (error == 0) {" , it will read values from the sensor, and you need to wait 2 seconds.

Well, not 0 errors explains why the code does not do your thing.

Did you do as post 8 recommends?

Also, the code now prints to the serial monitor.

Thanks I will keep working on it. I believe the it kicks it self out right before it calculates the temperature data because the error check is not equals to 0. Either way I will be working on it. Thanks for your help.

How is the 2000 delay working for you? If the 2000 delay is working, there is a way to do the delay without putting the CPU to sleep called millis().

Thanks the reason it doesn’t print to the monitor is it has a error in the code. It compiles ok. Thanks for the tool that will help me conquer some of the bugs and the suggestion millis(). Do you have any suggestions on a good beginner Arduino code book?

The very very basic things are covered by this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

non-blocking timing based on function millis() is some steps further down the road than the tutorial linked above.

Here is an example-code with a helper-function for non-blocking timing

unsigned long DemoTimer      = 0; // variables that are used to store values of function millis()
unsigned long DemoTimerTwo   = 0; // the must be of type unsigned long to work properly all the time
unsigned long DemoTimerThree = 0;
unsigned long DoDelayTimer   = 0;

unsigned long myCounter = 0;

// helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  }
  else return false;            // not expired
}


void setup() {
  Serial.begin(115200);
  Serial.println("Program started activate Show timestamp in serial monitor");
  Serial.println("maximise window of serial monitor");
  Serial.println("to see the the messages in full.......................................................length");
}


void myDemofunction_1() {
  Serial.println("once per second Huhu ! time for Action A ");
  Serial.print("myCounter=");
  Serial.println(myCounter);
}

void myDemoFuncB() {
  Serial.println("once every 3 seconds Hi there                      time for Action B once every 3 seconds");
}

void my_Demo_function_3() {
  Serial.print("once every 5 seconds ready now");
  for ( int i = 0; i< 40; i++) {
    Serial.print(".");
  }  
  Serial.println("time for Action C once every 5 seconds");
}


void loop() {
  myCounter++; // count up very fast to demonstrate the non-blocking character
  if (  TimePeriodIsOver(DemoTimer, 1000)  ) {
    myDemofunction_1();
  }


  if (  TimePeriodIsOver(DemoTimerTwo, 3000)  ) {
    myDemoFuncB();
  }

  if (  TimePeriodIsOver(DemoTimerThree, 5000)  ) {
    my_Demo_function_3();
  }

  // show the effect of BLOCKING timing caused by function delay()
  if (  TimePeriodIsOver(DoDelayTimer, 20000)  ) {
    Serial.println("every 20 seconds execute delay(5500)... to make all other timers overdue");
    
    Serial.print("value of myCounter right before delay =");
    Serial.println(myCounter);
    
    delay(5500);
    
    Serial.print("value of myCounter right AFTER delay =");
    Serial.println(myCounter);

    Serial.println("as delay(5500 has BLOCKED code-execution all three timers are overdue");
    Serial.println("which means all three timers fire in the SAME microsecond one after the other ");
  }
}

/*
  the basic principle of non-blocking timing is to check if a defined timeinterval
  has passed by.

  This can be done by using the function millis()
  The function millis()gives back the amount of milliseconds (hence the name millis)
  that have passed by since power-up of the microcontroller.
  It counts up to 2^32 which means reaching the max-value is reached after 49 days.
  There is a calculation-technique that even "rollover" from max to zero is handled
  automatically the right way

  This non-blocking timing needs a timer-variable which is used for taking
  snapshots of time as a comparison-point

  The variable-type for this variable MUST be of type unsigend long
  to make it work reliably all the time

  unsigned long myLcdUpdateTimer;

  now the following construction executes the code inside the if-condition
  only once every two seconds

  if ( TimePeriodIsOver(myLcdUpdateTimer,2000) ) {
     // time for timed action
  }

additionally the code demonstrates how you can code your own functions
and how to call/execute them.
The names of the functions are a bit lengthy to demonstrate which names 
inside the code you can choose freely and to demonstrate where really a relation
between names is and where NO relation between names is

*/

best regards Stefan

Thanks a million you are a great help. So nice to find someone That is legit.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.