Trouble merging 2 separate code

Hello guys. So I'm working on this project which is a bluetooth controlled car that has a temperature and humidity sensor and the information gathered by the sensor will be displayed on an LCD. My problem is that I can't figure out how to combine the code of the temperature and humidity sensor with LCD to the code of the bluetooth controlled car.

This is the code of temperature and humidty sensor:

#include <DHT.h>;
#include <LiquidCrystal_I2C.h>
#include <Wire.h>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
LiquidCrystal_I2C lcd(0x27,16,2); 
#define DHTPIN 7  
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE); 
int h;  
int t; 

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

void loop(){
    h = dht.readHumidity();
    t = dht.readTemperature();
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %, Temp: ");
    Serial.print(t);
    Serial.println(" ° Celsius");
    lcd.setCursor(0, 0);
    lcd.println(" Now Temperature ");    
    lcd.setCursor(0, 1);
    lcd.print("T:");
    lcd.print(t);
    lcd.print("C");
    lcd.setCursor(6, 1);
    lcd.println("2020 ");     
    lcd.setCursor(11, 1);
    lcd.print("H:");
    lcd.print(h);
    lcd.print("%");    
  delay(1000);
}

code of the bluetooth car:

#include <AFMotor.h>
AF_DCMotor motor4(1, MOTOR12_1KHZ); 
AF_DCMotor motor3(2, MOTOR12_1KHZ); 
AF_DCMotor motor2(3, MOTOR34_1KHZ);
AF_DCMotor motor1(4, MOTOR34_1KHZ);
char command; 

void setup() {       
  Serial.begin(9600); 
}
void loop(){
  if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); 
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      left();
      break;
    case 'R':
      right();
      break;
    }
  } 
}

Note: I didn't state the rest of the case code of the bluetooth car. I assumed it's not necessary.

Your topic does not indicate a problem with IDE 1.x and hence has been moved to a more suitable location on the forum.

First thing to do is to make sure that your DHT pin in the first code does not conflict with any pins used by the AFMotor library.

Can you indicate where your problems are? And yes, full code is probably necessary.

The Bluetooth module for the car is connected to the RX and TX pins of the Arduino. So this will conflict with any Serial parts of the DHT code. For debugging where the serial monitor is used instead of Bluetooth this is not a problem and it might not even by a problem for the final code.

There are numerous articles on the web about merging; see e.g. http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

To combine program code, first verify that the same keywords are not used. Next, is to make your code "modular" by using function calls (see below "readDHT()" and "getKey()"). In the "loop()" I have a simple timer waiting to read the DHT at two second intervals. The "getKey()" should run all the time.

This program/combination is untested, so next step is to roll it down the hill and see if it crashes.

#include <AFMotor.h>
AF_DCMotor motor4(1, MOTOR12_1KHZ);
AF_DCMotor motor3(2, MOTOR12_1KHZ);
AF_DCMotor motor2(3, MOTOR34_1KHZ);
AF_DCMotor motor1(4, MOTOR34_1KHZ);
char command;

#include <DHT.h>;
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int h;
int t;

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

void loop() {
  newMillis = millis(); // start a timer
  if (millis() - oldMillis > 2000) { // if 2000ms has passed
    oldMillis = newMillis; // store new timer
    readDHT(); // read DHT at 2000ms
  }
  getKey(); // getKey() all the time
}

void readDHT() {
  h = dht.readHumidity();
  t = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %, Temp: ");
  Serial.print(t);
  Serial.println(" ° Celsius");
  lcd.setCursor(0, 0);
  lcd.println(" Now Temperature ");
  lcd.setCursor(0, 1);
  lcd.print("T:");
  lcd.print(t);
  lcd.print("C");
  lcd.setCursor(6, 1);
  lcd.println("2020 ");
  lcd.setCursor(11, 1);
  lcd.print("H:");
  lcd.print(h);
  lcd.print("%");
}


void getKey() {
  if (Serial.available() > 0) {
    command = Serial.read();
    Stop();
    switch (command) {
      case 'F':
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
    }
  }
}

I tried the available pins which is the pin 13 and 2 but it still doesn't work.

It is not a ready code, it is just a conception that you have to complete yourself

I tried your code but it shows an error saying that newMillis is not specified under the scope.

You should become independant of experienced users advice for each and every single line of code.

This requires to learn programming to a certain level of knowledge.
It seems that you have a rather small knowledge-base about programming.

You started this thread 9 days ago. Of course you do not work on this project 16 hours a day. But you could have used 10 hours of the 16 h * 9 days = 144 hours to learn more about programming.

Take a look into this tutorial. It will take you 3 to 5 hours to work through this tutorial
and then you will be able to ask specific questions about the code and how to combine these two codes.

As soon as it becomes visible through specific questions and posting new code-versions that you climb up the learning-curve you will always receive really helpful answers.

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.

Sorry... it was just to show an idea of how to write modular code how to read the temperature at two seconds while commanding your motors. This one compiles, but has a messy "input" (the CRLF on the input).

#include "AFMotor.h"
AF_DCMotor motor4(1, MOTOR12_1KHZ);
AF_DCMotor motor3(2, MOTOR12_1KHZ);
AF_DCMotor motor2(3, MOTOR34_1KHZ);
AF_DCMotor motor1(4, MOTOR34_1KHZ);
char command;

#include <DHT.h>;
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int h;
int t;

unsigned long newMillis, oldMillis;

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

void loop() {
  newMillis = millis(); // start a timer
  if (millis() - oldMillis > 2000) { // if 2000ms has passed
    oldMillis = newMillis; // store new timer
    readDHT(); // read DHT at 2000ms
  }
  getKey(); // getKey() all the time
}

void readDHT() {
  h = dht.readHumidity();
  t = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %, Temp: ");
  Serial.print(t);
  Serial.println(" ° Celsius");
  lcd.setCursor(0, 0);
  lcd.println(" Now Temperature ");
  lcd.setCursor(0, 1);
  lcd.print("T:");
  lcd.print(t);
  lcd.print("C");
  lcd.setCursor(6, 1);
  lcd.println("2020 ");
  lcd.setCursor(11, 1);
  lcd.print("H:");
  lcd.print(h);
  lcd.print("%");
}


void getKey() {
  if (Serial.available() > 0) {
    command = Serial.read();
    Stop();
    switch (command) {
      case 'F':
      case 'f':
        forward();
        break;
      case 'B':
      case 'b':
        back();
        break;
      case 'L':
      case 'l':
        left();
        break;
      case 'R':
      case 'r':
        right();
        break;
    }
  }
}

void Stop() {
  Serial.println("Stop");
}
void forward() {
  Serial.println("Forward");
}
void back() {
  Serial.println("Backward");
}
void left() {
  Serial.println("Left");
}
void right() {
  Serial.println("Right");
}

I'm sorry but I'm only doing this for a school project and they're not even teaching us how to code. They'd just show us a sample code and expects us to understand it. I would like to understand how to code if only I have free time.

Not sure what you want to say with that.

Do you want to say that you are expected to deliver a working project on wednesday 03 of april 2024 and that you have no free time to work on the code?

You should post when you have to have finished this project and how many hours you can work on this project each week.

Or
another option is:
pay somebody for developping the whole thing. Be aware that even clarifiyng the exact details of your hardware will take two hours each hour paid with $50 or more

And developing will take 10 to 20 hours including far distance testing by phone.
So I guess not a real option for a student.

The free of cost support works this way:
You make a first attempt in combining the two codes and try to compile it
If it does not compile or work es expected post this code and a detailed description of what your car is doing

If this personal engagement is visible through

  • posting your own attempt to combine the two codes
  • providing the compiler-log
  • providing a description of the behaviour of your car
  • asking specific questions

you can be sure that you get continiously support and answers to your questions
but showing own effort is a must.