Need help at combine two cods

i have to cods that are in one screen (two tabs) and i am having a hard time to combine them

#include "DHT.h"
#include <Wire.h>
#include <ds3231.h>
#include <TimeLib.h>
struct ts t;
int r_h_l1 = 2;
int r_h_m1 = 3;
int r_h_l2 = 4;
int r_h_m2 = 5;
int r_h_l3 = 6;
int r_h_m3 = 7;
int r_h_m4 = 8;
int lamp = 9;
float minimom = 24;
float maxsimom = 36;
int sleep = 20;
int awake = 7;
#define DHT1 10
#define DHT2 11
#define DHT3 12
#define DHTTYPE DHT11
DHT dht_1(DHT1, DHTTYPE, 6);
DHT dht_2(DHT2, DHTTYPE, 6);
DHT dht_3(DHT3, DHTTYPE, 6);
//void update_RTC()
//{
//  Serial.println("clock edit Mode...");
//  const char txt_RTC[2][15] = { "hours [0~23]", "minutes [0~59]"};
//  String str_RTC = "";
//  long newDate_RTC[2];
//  while (Serial.available()) {
//    Serial.read();
//  }
//  for (int i = 0; i < 2; i++) {
//    Serial.print("Enter ");
//    Serial.print(txt_RTC[i]);
//    Serial.print(": ");
//    while (!Serial.available()) {
//      ;
//    }
//    str_RTC = Serial.readString();
//    newDate_RTC[i] = str_RTC.toInt();
//    Serial.println(newDate_RTC[i]);
//  }
//  t.hour = newDate_RTC[0];
//  t.min = newDate_RTC[1];
//  Serial.println("clock Updated!");
//}
void update_TEMPERATURE()
{
  Serial.println("temperature edit Mode...");
  const char txt_temperature[2][17] = { "max temperature", "min temperature"};
  String str_temperature = "";
  long newDate_temperature[2];
  while (Serial.available()) {
    Serial.read();
  }
  for (int i = 0; i < 2; i++) {
    Serial.print("Enter ");
    Serial.print(txt_temperature[i]);
    Serial.print(": ");
    while (!Serial.available()) {
      ;
    }
    str_temperature = Serial.readString();
    newDate_temperature[i] = str_temperature.toInt();
    Serial.println(newDate_temperature[i]);
  }
  minimom = newDate_temperature[0];
  maxsimom = newDate_temperature[1];
  Serial.println("temperature Updated!");
}
void update_SLEEP_AND_AWAKE()
{
  Serial.println("sleep and awake edit Mode...");
  const char txt_sleep_and_awake[2][15] = { "sleep time", "awake time"};
  String str_sleep_and_awake = "";
  long newDate_sleep_and_awake[2];
  while (Serial.available()) {
    Serial.read();
  }
  for (int i = 0; i < 2; i++) {
    Serial.print("Enter ");
    Serial.print(txt_sleep_and_awake[i]);
    Serial.print(": ");
    while (!Serial.available()) {
      ;
    }
    str_sleep_and_awake = Serial.readString();
    newDate_sleep_and_awake[i] = str_sleep_and_awake.toInt();

    Serial.println(newDate_sleep_and_awake[i]);
  }
  sleep = newDate_sleep_and_awake[0];
  awake = newDate_sleep_and_awake[1];
  Serial.println("Sleep and awake time Updated!");
}

void terrarium_1_day() {
  float t_1 = dht_1.readTemperature();
  if ( isnan(t_1)) {
    Serial.println("Failed to read from alecandro sensor!");
    return;
  }
  if (t_1 < minimom) {
    Serial.print("Temperature in alecandro's terrarium: ");
    Serial.print(t_1);
    Serial.println(" *C ");
    digitalWrite(r_h_l1, HIGH);
    digitalWrite(r_h_m1, HIGH);
    //pinMode(r_h_m4,HIGH);
  }
  else if (t_1 > maxsimom) {
    Serial.print("Temperature in alecandro's terrarium: ");
    Serial.print(t_1);
    Serial.println(" *C ");
    digitalWrite(r_h_l1, LOW);
    digitalWrite(r_h_m1, HIGH);
    //pinMode(r_h_m4,LOW);
  }
  else {
    Serial.println("temperature is fine in alecandro's terrarium");
    digitalWrite(r_h_m1, HIGH);
  }
}
void terrarium_2_day() {
  float t_2 = dht_2.readTemperature();
  if ( isnan(t_2)) {
    Serial.println("Failed to read from chupacabra's sensor!");
    return;
  }
  if (t_2 - 1 <= minimom) {
    Serial.print("Temperature in chupacabra's terrarium: ");
    Serial.print(t_2);
    Serial.println(" *C ");
    digitalWrite(r_h_l2, HIGH);
    digitalWrite(r_h_m2, HIGH);
    //pinMode(r_h_m4,HIGH);
  }
  else if (t_2 - 1 >= maxsimom) {
    Serial.print("Temperature in chupacabra's terrarium: ");
    Serial.print(t_2);
    Serial.println(" *C ");
    digitalWrite(r_h_l2, LOW);
    digitalWrite(r_h_m2, HIGH);
    //pinMode(r_h_m4,LOW);
  }
  else {
    Serial.println("temperature in chupacabra's terrarium is fine");
    digitalWrite(r_h_m2, HIGH);
  }
}
void terrarium_3_day() {
  float t_3 = dht_3.readTemperature();
  if ( isnan(t_3)) {
    Serial.println("Failed to read from zigzag's sensor!");
    return;
  }
  if (t_3 <= minimom) {
    Serial.print("Temperature in zigzag's terrarium: ");
    Serial.print(t_3);
    Serial.println(" *C ");
    digitalWrite(r_h_l3, HIGH);
    digitalWrite(r_h_m3, HIGH);
    //pinMode(r_h_m4,HIGH);
  }
  else if (t_3 >= maxsimom) {
    Serial.print("Temperature in zigzag's terrarium: ");
    Serial.print(t_3);
    Serial.println(" *C ");
    digitalWrite(r_h_l3, LOW);
    digitalWrite(r_h_m3, HIGH);
    //pinMode(r_h_m4,LOW);
  }
  else {
    Serial.println("temperature in zigzag's terrarium is fine");
    digitalWrite(r_h_m3, HIGH);
  }
}
void nightmode() {
  digitalWrite(lamp, LOW);
  digitalWrite(r_h_m1, HIGH);
  digitalWrite(r_h_m2, HIGH);
  digitalWrite(r_h_m3, HIGH);
  digitalWrite(r_h_l1, LOW);
  digitalWrite(r_h_l2, LOW);
  digitalWrite(r_h_l3, LOW);
}
void daymode() {
  digitalWrite(lamp, HIGH);
  terrarium_1_day();
  terrarium_2_day();
  terrarium_3_day();
}
void setup() {
  Serial.begin(9600);
  Wire.begin();
  dht_1.begin();
  dht_2.begin();
  dht_3.begin();
  while (!Serial) ; // Needed for Leonardo only
  setSyncProvider( requestSync);
  Serial.println("Waiting for sync message");
  pinMode(r_h_l1, OUTPUT);
  pinMode(r_h_m1, OUTPUT);
  pinMode(r_h_l2, OUTPUT);
  pinMode(r_h_m2, OUTPUT);
  pinMode(r_h_l3, OUTPUT);
  pinMode(r_h_m3, OUTPUT);
  pinMode(r_h_m4, OUTPUT);
  pinMode(lamp, OUTPUT);
  digitalWrite(r_h_l1, LOW);
  digitalWrite(r_h_m1, LOW);
  digitalWrite(r_h_l2, LOW);
  digitalWrite(r_h_m2, LOW);
  digitalWrite(r_h_l3, LOW);
  digitalWrite(r_h_m3, LOW);
  digitalWrite(r_h_m4, LOW);
  digitalWrite(lamp, LOW);
  delay(1000);
//  Serial.println("hello, in a few seconds you will have to write the current time. to reset the clock press the letter 'u' in the input butten, to rest the temperature press the letter 'w' and for changing the sleep and awake time press 't'");
//  delay(1000);
//  Serial.println("clock start mode...");
//  const char txt_RTC[2][15] = { "hours [0~23]", "minutes [0~59]"};
//  String str_RTC = "";
//  long newDate_RTC[2];
//  while (Serial.available()) {
//    Serial.read();
//  }
//  for (int i = 0; i < 2; i++) {
//    Serial.print("Enter ");
//    Serial.print(txt_RTC[i]);
//    Serial.print(": ");
//    while (!Serial.available()) {
//      ;
//    }
//    str_RTC = Serial.readString();
//    newDate_RTC[i] = str_RTC.toInt();
//
//    Serial.println(newDate_RTC[i]);
//  }
//  t.hour = newDate_RTC[0];
//  t.min = newDate_RTC[1];
//  Serial.println("clock Updated!");
//  //lcd.clear();
//}
void loop() {
//  if (Serial.available()) {
//    processSyncMessage();
//  }
//  if (timeStatus()!= timeNotSet) {
//    digitalClockDisplay();  
//  }
//  if (Serial.available()) {
//    char input = Serial.read();
//    if (input == 'u') {
//      update_RTC();
//    }
//    if (input == 't') {
//      update_TEMPERATURE();
//    }
//    if (input == 'w') {
//      update_SLEEP_AND_AWAKE();
//    }
//  }
    if (Serial.available()) {
    processSyncMessage();
  }
  if (timeStatus()!= timeNotSet) {
    digitalClockDisplay();  
  }
    Serial.print("time: ");
    Serial.print(t.hour);
    Serial.print(":");
    Serial.print(t.min);
    Serial.print(".");
    Serial.println(t.sec);
    //Serial.println('"');
//    t.sec += 1;
//    if (t.sec == 60) {
//      t.sec = 0;
//      t.min += 1;
//      if (t.min == 60) {
//        t.min = 0;
//        t.hour += 1;
//        if (t.hour == 24) {
//          t.hour = 0;
//        }
//      }
//    }
  if (t.hour == awake && t.hour < sleep) {
    if (t.min % 10 == 0 && t.sec == 0) {
      daymode();
    }
  }
  else {
    nightmode();
    if (t.min % 10 == 0 && t.sec == 0) {
      float t_3 = dht_3.readTemperature();
      float t_2 = dht_2.readTemperature();
      float t_1 = dht_1.readTemperature();
      Serial.print("Temperature in alechandro's terrarium: ");
      Serial.println(t_1);
      Serial.print("Temperature in chopacabra's terrarium: ");
      Serial.println(t_2);
      Serial.print("Temperature in zigzag's terrarium: ");
      Serial.println(t_3);
    }
  }
  delay(1000);
}

first code


#include <TimeLib.h>

#define TIME_HEADER  "T"   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


void processSyncMessage() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
       setTime(pctime); // Sync Arduino clock to the time received on the serial port
     }
  }
}

time_t requestSync()
{
  Serial.write(TIME_REQUEST);
  return 0; // the time will be sent later in response to serial mesg
}

seconde code

the problem is in the line: setSyncProvider( requestSync); in the void main at code one.
can someone fix this?? thanks

Post your best results with your combine the two codes and we'll see how we can help.

Oh! Oh! Almost forgot what do you want the combined code to do?

i have lizard and the winter is coming and i build something to control the temp and turn on and off the light

exit status 1
'requestSync' was not declared in this scope

this is the erorr

Post your combined code? Even if its not working.

thanks for the help
i did it, the problem was that in one code i used one library and in the athor a difirent one.
thanks anyway

1 Like

Yes, I can totally relate. Cods are very hard to combine: they are slippery and won't stay put. Maybe you can search on the net for some help.

2 Likes

Cod goes very well with chips...
download (2)

1 Like

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