Random thread hijack

Hello there I have some serious temperature issues in abasement im living in Im trying tu use my Arduino to control my environment .I have no experience in coding can you please help me with combining theses to codes together .I’m trying to have Relay 1 (channel 1 )turn on for 8 seconds and Off for 10 minutes continuously and Relay 2 ( channel 2) temperature controlled to turn on and off at a certain temperature.I found thses two examples on the Arduino IDE and they both indivully do what I need so can I just combine them.
Code 1
/*
RelayExample.ino
*/

#include <Easyuino.h> // Include the library in order to the compiler know you want Easyuino library

using Easyuino::Relay; // Necessary in order to use Relay

int arduinoPin = 6; // Arduino pin that controls the relay

Relay relay(arduinoPin); // Create the Relay object that exposes the API to use

void setup() {

Code 2
/********************************
name: 5V relay module and DHT 11/22/21 humidity and temperature sensor module
function: This program shows how the temperature and humidity sensor turns on
devices or power sockets connected to 5V relay module
email: info.acoptex@gmail.com
web: http://acoptex.com
********************************/
#include <DHT.h> // include library

const int relayPin = 8;
const int DHTPIN = A0;

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);// Initialize DHT sensor

/********************************/
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
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)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("Temperature = ");
Serial.print(f);
Serial.print(" *F ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.print(" %\t ");
Serial.println();
if (h >= 66) { // you can change humidity value here - h>=66 to your preffered number
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
if (t >= 25) {// you can change temperature value here - t>= 30 to your preferred number or change from Celsius to Fahrenheit readings
digitalWrite(relayPin, LOW);
}
else {
digitalWrite(relayPin, HIGH);
}
delay(1000);// 1 second delay between measurements
}

questscomputers:

/*

RelayExample.ino
*/

#include <Easyuino.h>  // Include the library in order to the compiler know you want Easyuino library

using Easyuino::Relay; // Necessary in order to use Relay

int arduinoPin = 6;   // Arduino pin that controls the relay

Relay relay(arduinoPin);  // Create the Relay object that exposes the API to use

void setup() {

That is not a complete sketch.

/*
RelayExample.ino
*/

#include <Easyuino.h> // Include the library in order to the compiler know you want Easyuino library

using Easyuino::Relay; // Necessary in order to use Relay

int arduinoPin = 6; // Arduino pin that controls the relay

Relay relay(arduinoPin); // Create the Relay object that exposes the API to use

void setup() {
/*

  • Initialize the Relay API.
  • The first parameter is TRUE if we connect something to the Normally Closed (NC) of the relay,
    and FALSE if we connect to the Normally Open (NO).
  • The second parameter is the digital level of the relay when it is in Normally Closed state.
    This is needed because some relays activate when we put HIGH on the pin and others on LOW.
    */
    relay.begin(false, HIGH); // Equivalent to: relay.begin() which is the common configuration.
    }

void loop() {
/*

  • Makes the device connected to relay to turn on.
    */
    relay.turnOn();

delay(2000);

/*

  • Makes the device connected to relay to turn off.
    */
    relay.turnOff();

delay(2000);

/*

  • Return true if the device is on and false if it is off.
    */
    bool result = relay.isOn();

delay(2000);
}

Here is a quick and dirty way to combine two sketches:

void setup()
{
  setup1();
  setup2();
}


void loop()
{
  loop1();
  loop2();
}


// CODE 1
/*
  RelayExample.ino
*/


#include <Easyuino.h>  // Include the library in order to the compiler know you want Easyuino library


using Easyuino::Relay; // Necessary in order to use Relay


int arduinoPin = 6;    // Arduino pin that controls the relay


Relay relay(arduinoPin);  // Create the Relay object that exposes the API to use


void setup1()
{
  /*
    - Initialize the Relay API.
    - The first parameter is TRUE if we connect something to the Normally Closed (NC) of the relay,
    and FALSE if we connect to the Normally Open (NO).
    - The second parameter is the digital level of the relay when it is in Normally Closed state.
    This is needed because some relays activate when we put HIGH on the pin and others on LOW.
  */
  relay.begin(false, HIGH);  // Equivalent to: relay.begin() which is the common configuration.
}


void loop1()
{
  /*
    - Makes the device connected to relay to turn on.
  */
  relay.turnOn();


  delay(2000);


  /*
    - Makes the device connected to relay to turn off.
  */
  relay.turnOff();


  delay(2000);


  /*
    - Return true if the device is on and false if it is off.
  */
  bool result = relay.isOn();


  delay(2000);
}


// CODE 2
/********************************
  name: 5V relay module and DHT 11/22/21 humidity and temperature sensor module
  function: This program shows how the temperature and humidity sensor turns on
            devices or power sockets connected to 5V relay module
  email: info.acoptex@gmail.com
  web: http://acoptex.com
********************************/
#include <DHT.h> // include library


const int relayPin = 8;
const int DHTPIN = A0;


// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)


DHT dht(DHTPIN, DHTTYPE);// Initialize DHT sensor


/********************************/
void setup2()
{
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
  dht.begin();
}


void loop2()
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);


  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Temperature = ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("Temperature = ");
  Serial.print(f);
  Serial.print(" *F ");
  Serial.print("Humidity = ");
  Serial.print(h);
  Serial.print(" %\t ");
  Serial.println();
  if (h >= 66)   // you can change humidity value here - h>=66 to your preffered number
  {
    digitalWrite(relayPin, LOW);
  }
  else
  {
    digitalWrite(relayPin, HIGH);
  }
  if (t >= 25)  // you can change temperature value here - t>= 30 to your preferred number or change from Celsius to Fahrenheit readings
  {
    digitalWrite(relayPin, LOW);
  }
  else
  {
    digitalWrite(relayPin, HIGH);
  }
  delay(1000);// 1 second delay between measurements
}

thanks for the reply

Have no idea if it works but the principles are there I think to get you further along with your project.

#include <DHT.h> // include library

//depending on how your relay coils are wired you may need to reverse these
#define RELAY_ON        LOW
#define RELAY_OFF       HIGH

const int relayPinCH1 = 6;
const int relayPinCH2 = 8;

#define RLYCH1_ONTIME       8000L       //8000mS is 8-seconds
#define RLYCH1_OFFTIME      600000L     //600000mS is 10-min x 60sec/min x 1000mS/sec (10x60x1000 == 600000)
#define RLYCH1_STATE_OFF    0
#define RLYCH1_STATE_ON     1

const int DHTPIN = A0;

#define DHTTYPE             DHT11       //specify DHT11

#define RLYCH2_CHKTIME      1000L       //mS temperature control runs once every 1000mS (1-sec)
#define RLYCH2_STATE_OFF    0
#define RLYCH2_STATE_ON     1

#define METRIC              1           //comment out to operate in oF instead of oC
//these are the temperature setpoint and hysteresis windows
//you can get tighter temperature control with smaller hyst values but
//at the cost of increased relay switching (can reduce the life of relays...)
#define TEMP_SET_POINT_C    23.0        //desired temperature, oC
#define TEMP_LOW_HYST_C     1.5         //heater turn ON point  (e.g. 22-1.5 or 20.5)
#define TEMP_HIGH_HYST_C    1.5         //heater turn OFF point (e.g. 22+1.5 or 23.5)
//
#define TEMP_SET_POINT_F    72.0
#define TEMP_LOW_HYST_F     1.0
#define TEMP_HIGH_HYST_F    1.0


DHT 
    dht(DHTPIN, DHTTYPE);

void setup()
{
    Serial.begin(9600);
    pinMode( relayPinCH1, OUTPUT );
    digitalWrite( relayPinCH1, RELAY_OFF );
    pinMode( relayPinCH2, OUTPUT );
    digitalWrite( relayPinCH2, RELAY_OFF );
    
    dht.begin();
    
}//setup


void loop()
{
    ControlRelayChannelOne();
    ControlRelayChannelTwo();
    
}//loop

///////////////////////////////////////////////////////////////////////////////////////////////
// ControlRelayChannelOne
//  per requirement:
//      cycles relay channel 1 on and off continuously
//      on time is given by RLYCH1_ONTIME mS
//      off time is given by RLYCH1_OFFTIME mS
//
///////////////////////////////////////////////////////////////////////////////////////////////
void ControlRelayChannelOne( void )
{
    static byte
        stateRlyCH1 = RLYCH1_STATE_OFF;
    static unsigned long
        timeRlyCH1Delay = RLYCH1_OFFTIME,
        timeRlyCH1 = millis();
    unsigned long
        timeNow;

    //get the current system mS count
    timeNow = millis();
    //check if elapsed time is sufficient
    if( (timeNow - timeRlyCH1 ) < timeRlyCH1Delay )
        return;

    //the timer has run down; set up for the next time
    timeRlyCH1 = timeNow;

    //and control the relay
    //the relay is either on or it's off
    switch( stateRlyCH1 )
    {
        case    RLYCH1_STATE_OFF:
            //relay is off right now. turn relay on and set up 'on' runtime
            digitalWrite( relayPinCH1, RELAY_ON );
            timeRlyCH1Delay = RLYCH1_ONTIME;
            //after the on-time has expired the next time through the loop the 'on' case will run
            stateRlyCH1 = RLYCH1_STATE_ON;
        break;

        case    RLYCH1_STATE_ON:
            //off time has expired. turn relay on and set up 'on' runtime
            digitalWrite( relayPinCH1, RELAY_OFF );
            timeRlyCH1Delay = RLYCH1_OFFTIME;
            //
            stateRlyCH1 = RLYCH1_STATE_OFF;
        break;
        
    }//switch
    
}//ControlRelayChannelOne

///////////////////////////////////////////////////////////////////////////////////////////////
// ControlRelayChannelTwo
//  per requirement:
//      relay channel 2 is "temperature controlled to turn on and off at a certain temperature"
//          some liberties are taken for "good" control (e.g. hysteresis, error checks)
//
///////////////////////////////////////////////////////////////////////////////////////////////
void ControlRelayChannelTwo( void )
{
    static byte
        stateRlyCH2 = RLYCH2_STATE_OFF;
    static unsigned long
        timeTempCheck = millis();
    unsigned long
        timeNow;    
    float
        fHumidity,
        fTemperatureC,
        fTemperatureF;

    //is it time for a temperature check?
    timeNow = millis();
    if( (timeNow - timeTempCheck) < RLYCH2_CHKTIME )
        return;

    timeTempCheck = timeNow;

    //read the temperature
    //humidity is read but not used at this time
    //could be incorporated to account for "feel" of high or low humidity
    //or to control a third relay doing humidifier control or something
    fHumidity = dht.readHumidity();
    fTemperatureC = dht.readTemperature();
    fTemperatureF = dht.readTemperature(true);

    // Check if any reads failed and exit early (to try again).
    if (isnan(fHumidity) || isnan(fTemperatureC) || isnan(fTemperatureF))
    {
        Serial.println("Failed to read from DHT sensor!");
        //force relay off
        //a valid safety case might be to turn the relay on; end user to decide
        digitalWrite( relayPinCH2, RELAY_OFF );
        
        return;
        
    }//if

    //send current conditions to console
    PrintConditions( fTemperatureC, fTemperatureF, fHumidity );
       
    //not sure what relay 2 is controlling
    //I'll assume when active, whatever relay 2 is connected to is adding heat
    switch( stateRlyCH2 )
    {
        case    RLYCH2_STATE_OFF:
            //"heater" is off so we're looking for temperature to fall to turn-on point
#ifdef  METRIC
            if( fTemperatureC <= (TEMP_SET_POINT_C - TEMP_LOW_HYST_C ) )
                 digitalWrite( relayPinCH2, RELAY_ON );
#else
            if( fTemperatureF <= (TEMP_SET_POINT_F - TEMP_LOW_HYST_F ) )
                 digitalWrite( relayPinCH2, RELAY_ON );
#endif       
            stateRlyCH2 = RLYCH2_STATE_ON;
        break;

        case    RLYCH2_STATE_ON:
            //"heater" is on so we're looking for temperature to rise to the turn-off point
#ifdef  METRIC
            if( fTemperatureC >= (TEMP_SET_POINT_C + TEMP_HIGH_HYST_C ) )
                 digitalWrite( relayPinCH2, RELAY_OFF );
#else
            if( fTemperatureF >= (TEMP_SET_POINT_F + TEMP_HIGH_HYST_F ) )
                 digitalWrite( relayPinCH2, RELAY_OFF );
#endif       
            stateRlyCH2 = RLYCH2_STATE_OFF;
        break;
        
    }//switch
    
}//ControlRelayChannelTwo

void PrintConditions( float t, float f, float h )
{
    if( !Serial )
        return;
#ifdef METRIC
    Serial.print("Temperature = "); Serial.print(t); Serial.println(" *C ");
#else
    Serial.print("Temperature = "); Serial.print(f); Serial.println(" *F ");
#endif    
    Serial.print("Humidity = "); Serial.print(h); Serial.println( '%' );
    Serial.println();
    
}//PrintConditions

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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

Thanks.. Tom... :slight_smile:

Strange choice of Title.

@questscomputers, if you edit your Original Post you can change the Title to something that describes your problem.

...R