Help coding

So im working on a “complex” Composting machine for apple and im trying to get a simple code done, I have 2 codes.

a code that turns a relay on for 5s every 12 hours and a code that gives a live output of temp and humidity to the serial monitor. The problem Is I dont seem to be getting a live view of the temp and humidity.

Here is the merged code

#define RELAY1  7    
#include <DHT11.h>
int pin=4;
DHT11 dht11(pin); 
                    
void setup()
{
   Serial.begin(9600);
  while (!Serial) {
      ; 
    }
}

   

 



  void loop()
{
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    Serial.print("temperature:");
    Serial.print(temp);
    Serial.print(" humidity:");
    Serial.print(humi);
    Serial.println();
  }
  else
  {
    Serial.println();
    Serial.print("Error No :");
    Serial.print(err);
    Serial.println();    
  }
  delay(DHT11_RETRY_DELAY); 




   digitalWrite(RELAY1,0);           // Turns ON Relays 1
   Serial.println("Light N");
   delay(43200000);                                      // Wait 12 hours

   digitalWrite(RELAY1,1);          // Turns Relay Off
   Serial.println("Light OFF");
   delay(5000);
   
}

here is the relay code

#define RELAY1  7    
#include <DHT11.h>
int pin=4;
DHT11 dht11(pin); 
                    
void setup()
{
   Serial.begin(9600);
  while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
    }
}

and here is the sensor code

#include <DHT11.h>
int pin=4;
DHT11 dht11(pin); 
void setup()
{
   Serial.begin(9600);
  while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
    }
}

void loop()
{
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    Serial.print("temperature:");
    Serial.print(temp);
    Serial.print(" humidity:");
    Serial.print(humi);
    Serial.println();
  }
  else
  {
    Serial.println();
    Serial.print("Error No :");
    Serial.print(err);
    Serial.println();    
  }
  delay(DHT11_RETRY_DELAY); //delay for reread
}

thanks

delay() is one of the things you have to look at carefully , especially when you merge sketches. You have to find another solution. It will certainly block your new code.

Robin2 has a discussion you should go through:

Here is a example:

//Blink without Delay skeleton example using a structure.
//LarryD

//======================================================================
struct timer
{
  //lastMillis = the time this "timer" was (re)started
  //waitMillis = delay time (mS) we are looking for. You can use math 60*60*1000 for 1 hour
  //restart    = do we start "this timer" again and again  
  //enableFlag = is "this timer" enabled/allowed to be accessed
  //**********************
  //For each timer object you need: 
  //Example:
  //   timer myTimer = //give the timer a name "myTimer"
  //   {
  //     0, 200UL, true, true  //lastMillis, waitMillis, restart, enableFlag 
  //   };
  // You have access to: 
  // myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.CheckTime() 
  //**********************

  unsigned long lastMillis; 
  unsigned long waitMillis; 
  bool          restart; 
  bool          enableFlag;
  
  bool CheckTime() //Delay time expired function "CheckTime()"
  {
    //is the time up for this task?
    if (enableFlag && millis() - lastMillis >= waitMillis) 
    //Note: if delays of < 2 millis are needed use micros() and adjust waitMillis as needed
    {
      //should this start again? 
      if(restart)
      {
        //get ready for the next iteration
        lastMillis += waitMillis;  
      }
      //time was reached
      return true;
    }
    //time was not reached
    return false;
  } //END of CheckTime()

}; //END of structure timer
//======================================================================


//**********************************************************************
//Let's create 6 timer objects and initialize them in this sketch
//**********************************************************************
timer pin13 = //create timer pin13
{
  0, 200UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin12 = //create timer pin12
{
  0, 3*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin11 = //create timer pin11
{
  0, 10*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin10 = //create timer pin10
{
  0, 6*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer Toggle10 = //create timer Toggle10
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer checkSwitches = //create timer checkSwitches
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************

byte lastMySwitchState = 1; //for mySwitch on Pin 2
byte counter           = 0; 

const byte Pin13 = 13;
const byte Pin12 = 12;
const byte Pin11 = 11;
const byte Pin10 = 10;
const byte Pin9  =  9;

const byte mySwitch = 2;

//**********************************************************************

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

  pinMode(Pin13,OUTPUT);
  pinMode(Pin12,OUTPUT);
  pinMode(Pin11,OUTPUT);
  pinMode(Pin10,OUTPUT);
  pinMode(Pin9, OUTPUT);

  digitalWrite(Pin13,LOW);
  digitalWrite(Pin12,LOW);
  digitalWrite(Pin11,LOW);
  digitalWrite(Pin10,LOW);
  digitalWrite(Pin9, LOW);

  pinMode(mySwitch,INPUT_PULLUP);


} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<


void loop()
{
  //Below are examples demonstrating different timing situations 

  //***************************
  //example 1    Toggle Pin13 every 200ms
  if (pin13.CheckTime())
  {
    //Toggle Pin13
    digitalWrite(Pin13,!digitalRead(Pin13));

    //if you only want this section of code to happen once
    //uncomment the next line 
    //pin13.enableFlag = false;
  }

  //***************************
  //example 2    After 3 seconds, Pin12 goes and stays HIGH 
  if (pin12.CheckTime())
  {
    //Make Pin12 HIGH now
    digitalWrite(Pin12,HIGH);
    //disable timing section of code 
    pin12.enableFlag = false;
  }

  //***************************
  //example 3    Pin11 is HIGH for 10 seconds, then goes and stays LOW
  if (pin11.enableFlag && !pin11.CheckTime())
  {
    digitalWrite(Pin11,HIGH);
  }
  //10 seconds is now up now, leave the Pin11 LOW
  else
  {
    digitalWrite(Pin11,LOW);
    //disable timing section of code 
    pin11.enableFlag = false;
  }

  //***************************
  //example 4    For 6 seconds, toggle Pin10
  if (pin10.enableFlag && !pin10.CheckTime())
  {
    //example 5  Toggling Pin10 every 50mS
    if(Toggle10.CheckTime())
    {  
      //toggle Pin10
      digitalWrite(Pin10,!digitalRead(Pin10));    
    }
  }
  //6 seconds is now up, toggling is stopped
  else
  {
    digitalWrite(Pin10,LOW);
    //disable timing section of code 
    pin10.enableFlag = false;
  }

  //***************************
  //example 6    Is it time to check the switches?
  if (checkSwitches.CheckTime())
  {
    //time to read the switches
    Switches();      
  } 

  //**********************************
  //Put other non-blocking stuff here
  //**********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//**********************************************************************
//switches are checked every checkSwitches.waitMillis milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void Switches()  
{
  boolean thisState; //re-usable for all the switches      

  //*************************** mySwitch Pin 2 code  
  //check if this switch has changed state
  thisState = digitalRead(mySwitch); 
  if (thisState != lastMySwitchState)
  {  
    //update the switch state
    lastMySwitchState = thisState;  

    //This switch position has changed, let's do some stuff

    //"HIGH condition code"
    //switch goes from LOW to HIGH
    if(thisState == HIGH)        
    {
      //example: LED on Pin9 is Push ON, Push OFF
      digitalWrite(Pin9,!digitalRead(Pin9)); 
    }

    //"LOW condition code"
    //switch goes from HIGH to LOW
    else                          
    {
      //example: display the current switch push count 
      Serial.println(++counter);      
    }
  } //END of mySwitch Pin 2 code

  //******************************************  
  //similar code for other switches goes here 
  //******************************************  

} //END of Switches()


//======================================================================
//                        E N D  O F  C O D E
//======================================================================

LarryD:
Robin2 has a discussion you should go through:
https://forum.arduino.cc/index.php?topic=223286.0

Here is a example:

//Blink without Delay skeleton example using a structure.

//LarryD

//======================================================================
struct timer
{
  //lastMillis = the time this “timer” was (re)started
  //waitMillis = delay time (mS) we are looking for. You can use math 60601000 for 1 hour
  //restart    = do we start “this timer” again and again 
  //enableFlag = is “this timer” enabled/allowed to be accessed
  //**********************
  //For each timer object you need:
  //Example:
  //  timer myTimer = //give the timer a name “myTimer”
  //  {
  //    0, 200UL, true, true  //lastMillis, waitMillis, restart, enableFlag
  //  };
  // You have access to:
  // myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.CheckTime()
  //**********************

unsigned long lastMillis;
  unsigned long waitMillis;
  bool          restart;
  bool          enableFlag;
 
  bool CheckTime() //Delay time expired function “CheckTime()”
  {
    //is the time up for this task?
    if (enableFlag && millis() - lastMillis >= waitMillis)
    //Note: if delays of < 2 millis are needed use micros() and adjust waitMillis as needed
    {
      //should this start again?
      if(restart)
      {
        //get ready for the next iteration
        lastMillis += waitMillis; 
      }
      //time was reached
      return true;
    }
    //time was not reached
    return false;
  } //END of CheckTime()

}; //END of structure timer
//======================================================================

//**********************************************************************
//Let’s create 6 timer objects and initialize them in this sketch
//**********************************************************************
timer pin13 = //create timer pin13
{
  0, 200UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin12 = //create timer pin12
{
  0, 31000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//
**************************
timer pin11 = //create timer pin11
{
  0, 101000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//
**************************
timer pin10 = //create timer pin10
{
  0, 61000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//
**************************
timer Toggle10 = //create timer Toggle10
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer checkSwitches = //create timer checkSwitches
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************

byte lastMySwitchState = 1; //for mySwitch on Pin 2
byte counter          = 0;

const byte Pin13 = 13;
const byte Pin12 = 12;
const byte Pin11 = 11;
const byte Pin10 = 10;
const byte Pin9  =  9;

const byte mySwitch = 2;

//**********************************************************************

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

pinMode(Pin13,OUTPUT);
  pinMode(Pin12,OUTPUT);
  pinMode(Pin11,OUTPUT);
  pinMode(Pin10,OUTPUT);
  pinMode(Pin9, OUTPUT);

digitalWrite(Pin13,LOW);
  digitalWrite(Pin12,LOW);
  digitalWrite(Pin11,LOW);
  digitalWrite(Pin10,LOW);
  digitalWrite(Pin9, LOW);

pinMode(mySwitch,INPUT_PULLUP);

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<

void loop()
{
  //Below are examples demonstrating different timing situations

//***************************
  //example 1    Toggle Pin13 every 200ms
  if (pin13.CheckTime())
  {
    //Toggle Pin13
    digitalWrite(Pin13,!digitalRead(Pin13));

//if you only want this section of code to happen once
    //uncomment the next line
    //pin13.enableFlag = false;
  }

//***************************
  //example 2    After 3 seconds, Pin12 goes and stays HIGH
  if (pin12.CheckTime())
  {
    //Make Pin12 HIGH now
    digitalWrite(Pin12,HIGH);
    //disable timing section of code
    pin12.enableFlag = false;
  }

//***************************
  //example 3    Pin11 is HIGH for 10 seconds, then goes and stays LOW
  if (pin11.enableFlag && !pin11.CheckTime())
  {
    digitalWrite(Pin11,HIGH);
  }
  //10 seconds is now up now, leave the Pin11 LOW
  else
  {
    digitalWrite(Pin11,LOW);
    //disable timing section of code
    pin11.enableFlag = false;
  }

//***************************
  //example 4    For 6 seconds, toggle Pin10
  if (pin10.enableFlag && !pin10.CheckTime())
  {
    //example 5  Toggling Pin10 every 50mS
    if(Toggle10.CheckTime())
    { 
      //toggle Pin10
      digitalWrite(Pin10,!digitalRead(Pin10));   
    }
  }
  //6 seconds is now up, toggling is stopped
  else
  {
    digitalWrite(Pin10,LOW);
    //disable timing section of code
    pin10.enableFlag = false;
  }

//***************************
  //example 6    Is it time to check the switches?
  if (checkSwitches.CheckTime())
  {
    //time to read the switches
    Switches();     
  }

//**********************************
  //Put other non-blocking stuff here
  //**********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<

//======================================================================
//                      F U N C T I O N S
//======================================================================

//**********************************************************************
//switches are checked every checkSwitches.waitMillis milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void Switches() 
{
  boolean thisState; //re-usable for all the switches

//*************************** mySwitch Pin 2 code 
  //check if this switch has changed state
  thisState = digitalRead(mySwitch);
  if (thisState != lastMySwitchState)
  { 
    //update the switch state
    lastMySwitchState = thisState;

//This switch position has changed, let’s do some stuff

//“HIGH condition code”
    //switch goes from LOW to HIGH
    if(thisState == HIGH)       
    {
      //example: LED on Pin9 is Push ON, Push OFF
      digitalWrite(Pin9,!digitalRead(Pin9));
    }

//“LOW condition code”
    //switch goes from HIGH to LOW
    else                         
    {
      //example: display the current switch push count
      Serial.println(++counter);     
    }
  } //END of mySwitch Pin 2 code

//****************************************** 
  //similar code for other switches goes here
  //******************************************

} //END of Switches()

//======================================================================
//                        E N D  O F  C O D E
//======================================================================

How can I use this in my code though? im not very expirienced with coding at all

I don’t know the DHT11 but I’ve found your sensor code and now know what DHT11_RETRY_DELAY is (standard 1000mS to give the sensor time to stabilise).
So the sensor code looks fine. You simply want to add a relay with a relatively long (12 hour cycle) which at the end of the cycle simply operates
for 5 seconds, and without disturbing the sensor reading part during the waiting period.

I guess you have understood by now that while this statement: ‘delay(43200000);’ is executing, nothing else is happening in your sketch, so it is effectively blocked.

I’ve modified you code (untested) to deal with the long delay, and left you (maybe using LarryD’s code) to handle the short (5 second delay).
For testing, you can naturally shorten these times.

#define RELAY1  7   
#include <DHT11.h>
int pin=4;
DHT11 dht11(pin);

const unsigned long relayOffTimeMs = 43200000 ;  
const unsigned long relayOnTimeMs = 5000 ;
unsigned long relayCycleStartMs = 0 ;
                   
void setup()
{
   Serial.begin(9600);
  while (!Serial) {
      ;
    }
  digitalWrite(RELAY1,1);          // Turns Relay Off
}

   

  void loop()
{
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    Serial.print("temperature:");
    Serial.print(temp);
    Serial.print(" humidity:");
    Serial.print(humi);
    Serial.println();
  }
  else
  {
    Serial.println();
    Serial.print("Error No :");
    Serial.print(err);
    Serial.println();   
  }
  delay(DHT11_RETRY_DELAY);


  if ( millis() - relayCycleStartMs > relayOffTimeMs ) {
    // ok we have reached the end of the OFF period.
   digitalWrite(RELAY1,0);           // Turns ON Relays 1
   Serial.println("Light N");
   relayCycleStartMs = millis() ;
   
   // the delay(relayOnTimeMs) should also be got rid of in a similar manner using millis()
   // because it will block here for the length of time the relay is on.
   // this is left as an exercise for the OP.
   delay(relayOnTimeMs);
   digitalWrite(RELAY1,1);          // Turns Relay Off
   Serial.println("Light OFF");
  }
 
}

6v6gt:
I don’t know the DHT11 but I’ve found your sensor code and now know what DHT11_RETRY_DELAY is (standard 1000mS to give the sensor time to stabilise).
So the sensor code looks fine. You simply want to add a relay with a relatively long (12 hour cycle) which at the end of the cycle simply operates
for 5 seconds, and without disturbing the sensor reading part during the waiting period.

I guess you have understood by now that while this statement: ‘delay(43200000);’ is executing, nothing else is happening in your sketch, so it is effectively blocked.

I’ve modified you code (untested) to deal with the long delay, and left you (maybe using LarryD’s code) to handle the short (5 second delay).
For testing, you can naturally shorten these times.

#define RELAY1  7   

#include <DHT11.h>
int pin=4;
DHT11 dht11(pin);

const unsigned long relayOffTimeMs = 43200000 ; 
const unsigned long relayOnTimeMs = 5000 ;
unsigned long relayCycleStartMs = 0 ;
                 
void setup()
{
  Serial.begin(9600);
  while (!Serial) {
      ;
    }
  digitalWrite(RELAY1,1);          // Turns Relay Off
}

void loop()
{
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    Serial.print(“temperature:”);
    Serial.print(temp);
    Serial.print(" humidity:");
    Serial.print(humi);
    Serial.println();
  }
  else
  {
    Serial.println();
    Serial.print(“Error No :”);
    Serial.print(err);
    Serial.println(); 
  }
  delay(DHT11_RETRY_DELAY);

if ( millis() - relayCycleStartMs > relayOffTimeMs ) {
    // ok we have reached the end of the OFF period.
  digitalWrite(RELAY1,0);          // Turns ON Relays 1
  Serial.println(“Light N”);
  relayCycleStartMs = millis() ;
 
  // the delay(relayOnTimeMs) should also be got rid of in a similar manner using millis()
  // because it will block here for the length of time the relay is on.
  // this is left as an exercise for the OP.
  delay(relayOnTimeMs);
  digitalWrite(RELAY1,1);          // Turns Relay Off
  Serial.println(“Light OFF”);
  }

}

I got the code to work after some trial and error since you forgot to add the digital write. Thanks for the help though. Next step for me is adding another relay and adding sone indicator leds,

Julianfer5:
I got the code to work after some trial and error since you forgot to add the digital write. Thanks for the help though. Next step for me is adding another relay and adding sone indicator leds,

I’m sorry that you were inconvenienced by my supply of a incomplete solution. I’m pleased, however, that you were able to make it useable. Can you post the corrected code so I can also identify the error?