Can you combine my code because i don't know how to combine

can you help me combine my code this

// Libraries
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

// Constants
#define DHTPIN A3 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
#define BUZZER_PIN 9 // Pin connected to the buzzer
#define TEMP_LIMIT 40 // New temperature limit (adjust as needed)

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino

// Variables
int h; // Stores humidity value
int t; // Stores temperature value

void setup() {
Serial.begin(9600);
Serial.println("Temperature and Humidity Sensor Test");
dht.begin();
lcd.init(); // Initialize the lcd
lcd.backlight(); // Open the backlight

pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output

}

void loop() {
// Read data and store it to variables h (humidity) and t (temperature)
h = dht.readHumidity();
t = dht.readTemperature();

// Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %, Temp: ");
Serial.print(t);
Serial.println(" ° Celsius");

// Set the cursor to (0,0) and print
lcd.setCursor(0, 0);
lcd.println(" TEMP AND HUMI  ");

lcd.setCursor(0, 1);
lcd.print(" T:");
lcd.print(t);
lcd.print("C");

lcd.setCursor(11, 1);
lcd.print("H:");
lcd.print(h);
lcd.print("%");

// Check temperature against the limit
if (t > TEMP_LIMIT) {
    digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer on
} else {
    digitalWrite(BUZZER_PIN, LOW); // Turn buzzer off
}

delay(100); 

}

and this is my second code

#include <DHT.h>
#include <SoftwareSerial.h>

#define DHTPIN 2 // Pin where the DHT11 is connected
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);

SoftwareSerial sim800(9, 10); // RX, TX for SIM800L

String phone_number = "YOUR_PHONE_NUMBER"; // Replace with your phone number

void setup() {
Serial.begin(9600);
sim800.begin(9600);
dht.begin();
delay(1000); // Give time for modules to initialize
Serial.println("System started");
}

void loop() {
float temp = dht.readTemperature();

if (isnan(temp)) {

Serial.println("Failed to read from DHT sensor!");
return;

}

Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");

// If temperature exceeds 40°C, trigger a call
if (temp > 40) {
makeCall();
delay(300000); // Delay for 5 minutes before rechecking the temperature
}

delay(2000); // Read temperature every 2 seconds
}

void makeCall() {
Serial.println("Temperature exceeded 40°C. Making a call...");

// AT command to dial a number
sim800.print("ATD");
sim800.print(phone_number);
sim800.println(";");

delay(30000); // Allow the call to last for 30 seconds before hanging up

// AT command to hang up
sim800.println("ATH");

Serial.println("Call ended.");
}

welcome to the arduino-forum.
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

This forum is not a free code writing service. You have 2 options.

  1. You attempt to combine the code yourself and the forum will help and guide you to success. But you will be doing most of the work and you must be willing to accept this challenge and be willing to learn from the experience.
  2. I can move your post to a section of the forum where you can offer payment for someone to combine the code for you. Take great care in this forum section that you reserve most of the agreed fee until you are satisfied that the results are working correctly.

I never like combining codes, better to understand how these two work , then write your own .
If you don’t know how they work , combine and it’s not working properly , what do you do ?

1 Like

Why is bits and pieces of the code in code tags and a large part not?

Pay attention to @StefanL38 @PaulRB advice , nobody will try to read thru your code as it is posted in this way.

can you explain what the separate pieces of code do?

General procedure:

Check for pins that are used by both sketches.
If needed, adjust conflicting pins.

Take first sketch.
Copy global variables and #includes from second sketch into first sketch (before setup()).
Copy code in setup() of second sketch into setup() of first sketch.
Same for code in loop().

Indented chunks of text are marked as code in the forum’s Markdown formatting

1 Like

Sorry but my internet service provider pulled the plug on the free web space they offered and took my whole site off line and didn't even offer a web redirection service. Tech support says every time they suggest this the CEO of the company says it will cost hundreds of thousand pounds to implement.
This file when un-compressed you can double click the .HTML file and it should load just that page into your default browser. Let me know if you have any problems with this. Of course none of the links off this page will work.

Merging_Code.zip (663.3 KB)

sorry I will do it later, I'm just busy rn due to I have so many things to do hehe

Understood, we’re all busy some days.
See you later, when you have more time free.

1 Like

@terryking228's page is still up.

https://arduinoinfo.mywikis.net/wiki/CombiningArduinoSketches

1 Like

Please, refrain from posting duplicate topics with the same subject.

One displays temperature and humidity, the other makes a phone call above 40c.

Code dump. You will never return or learn.

sketch.ino
#include <DHT.h>
#include <SoftwareSerial.h>
SoftwareSerial sim800(9, 10); // RX TX

char phone_number[] = "+44208675309";

#define DHTPIN 2
#define DHTTYPE DHT11
#define TEMP_LIMIT 40

DHT dht(DHTPIN, DHTTYPE);

float h, t;

void setup() {
  Serial.begin(115200);
  sim800.begin(9600);
  dht.begin();
}

void loop() {
  h = dht.readHumidity();
  t = dht.readTemperature();

  if (isnan(t)) {
    Serial.println("dht fail");
    while (1);
  }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("% Temperature: ");
  Serial.print(t);
  Serial.println("°C");

  if (t > TEMP_LIMIT) {
    Serial.print("TEMPERATURE LIMIT. ");
    makeCall();
  }
  delay(2000); // dht interval
}

void makeCall() {
  Serial.print("CALLING. ");
  sim800.print("ATD"); // dial
  Serial.print("(ATD ");
  sim800.print(phone_number);
  Serial.print(phone_number);
  sim800.println(";");
  Serial.print("; ");
  delay(30000); // hold
  sim800.println("ATH");
  Serial.print("ATH) "); // hangup
  Serial.println("ENDED.");
  delay(300000); // high temp interval
}

Why have a five minutes interval between warnings?