How to merge two codes correctly?

Hi! I want to merge this codes.( I tried it but it bugs me at printTemperature();} The two codes independently work well

one is this

#include<Servo.h>

Servo esc;
int hallSensorPin = 2;     
int ledPin =  13;    
int state = 0; 
float R1=30000;
float R2=7500;
int RELAY = 4;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      
  pinMode(hallSensorPin, INPUT);
  pinMode(RELAY, OUTPUT); 
 esc.attach(9);
 digitalWrite(RELAY, HIGH);
 unsigned long _ATimer;
 _ATimer = millis() ;
 while ((millis() - _ATimer) < (3000)) {
     esc.writeMicroseconds(1900); 
 }
 _ATimer = millis() ;
 while ((millis() - _ATimer) < (1000)) {
      esc.writeMicroseconds(950);  
 }
 delay(30);
}

void loop() {

 static unsigned long DelayTimer;

 int state = digitalRead(hallSensorPin);
 int sensorValue = analogRead(A0);
 float voltage = (5 * sensorValue * (R1 + R2)) / (1024 * R2);
 Serial.println(voltage);
 delay(30);


 if (state == LOW)   
 
 {
   digitalWrite(ledPin, HIGH);
   
   if ((millis() - DelayTimer) >= (4000)) {
     esc.writeMicroseconds(1000);
     digitalWrite(RELAY, LOW);
   }
 }
 if (state == HIGH)

 {
   DelayTimer = millis(); 
   digitalWrite(ledPin, LOW);
   esc.writeMicroseconds(950);
    digitalWrite(RELAY, HIGH);
 }

 if (voltage < 11.00)
 
 {
   digitalWrite(ledPin, LOW);
   while(1) {digitalWrite(RELAY, HIGH); 
   esc.writeMicroseconds(950); }
   delay(30);
 }   [color=blue]// i Want here if temperature it is over 50C 
       // esc.writeMicroseconds(950)    thank you[/color]
}

and the other is this

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempC = 0;


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

void loop(void)
{ 

 sensors.requestTemperatures();
 tempC = sensors.getTempCByIndex(0);
 
 printTemperature();
 
 delay(50);
}

void printTemperature()
{
 Serial.print(tempC);
 Serial.print(char(176));
 Serial.print("C");
  Serial.print("\t");
  Serial.print("\n");
}

After 15 posts you still haven't taken the time to read Nick Gammon's post at the top of this Forum on the proper way to post code using code tags? Why is that?

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

This Simple Merge Demo may help.

You need to make sure that the two programs are not trying to use the same piece of hardware - such as an I/O pin.

…R

An Arduino sketch can only have one setup() and loop() function, so your first step is to factor the code from each of them into the remaining two functions.

Yes i know that and i tried it But it makes me an error

Edit your post to add code tags.

xenofon:
Yes i know that and i tried it But it makes me an error

Then you should have posted what you tried as that would give us some idea of where you went wrong.

econjack:
Then you should have posted what you tried as that would give us some idea of where you went wrong.

οκ

I have merge like this but i have error here printTemperature();

 #include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempC = 0;
#include<Servo.h>
Servo esc;
int hallSensorPin = 2;     
int ledPin =  13;    
int state = 0; 
float R1=30000;
float R2=7500;
int RELAY = 4;

 void setup()
{  
   sensors.begin();
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);      
   pinMode(hallSensorPin, INPUT);
   pinMode(RELAY, OUTPUT); 
  esc.attach(9);
  digitalWrite(RELAY, HIGH);
  unsigned long _ATimer;
  _ATimer = millis() ;
  while ((millis() - _ATimer) < (3000)) {
      esc.writeMicroseconds(1900); 
  }
  _ATimer = millis() ;
  while ((millis() - _ATimer) < (1000)) {
       esc.writeMicroseconds(950); 
  }
  delay(30);
}

void loop() {

  static unsigned long DelayTimer;

  int state = digitalRead(hallSensorPin);
  int sensorValue = analogRead(A0);
  float voltage = (5 * sensorValue * (R1 + R2)) / (1024 * R2);
  Serial.println(voltage);
  delay(30);
  sensors.requestTemperatures();
  tempC = sensors.getTempCByIndex(0);
  printTemperature();
  void printTemperature()
{
  Serial.print(tempC);
  Serial.print(char(176));
  Serial.print("C");
   Serial.print("\t");
   Serial.print("\n");
}

  if (state == LOW)   
  
  {
    digitalWrite(ledPin, HIGH);
    
    if ((millis() - DelayTimer) >= (4000)) {
      esc.writeMicroseconds(1000);
      digitalWrite(RELAY, LOW);
    }
  }
  if (state == HIGH)
 
  {
    DelayTimer = millis();
    digitalWrite(ledPin, LOW);
    esc.writeMicroseconds(950);
     digitalWrite(RELAY, HIGH);
  }

  if (voltage < 11.00)
  
  {
    digitalWrite(ledPin, LOW);
    while(1) {digitalWrite(RELAY, HIGH); 
    esc.writeMicroseconds(950); }
    delay(30);
  }
}

Hi,
This is a very common problem: adding two or more working sketches into one sketch.

Putting a bunch of code together and then getting a Zillion error messages is No Fun!

FIRST! Start by understanding both sketches and the resources and libraries they use before attempting to stitch them together. If there is any part of either sketch you don't understand, that will be the part that causes you trouble.

HERE IS A PAGE that has suggestions on how to approach this...

here it is the error If I remove it, it reads the rest of the code correctly

printTemperature();
void printTemperature()
{
Serial.print(tempC);
Serial.print(char(176));
Serial.print("C");
Serial.print("\t");
Serial.print("\n");
}

here it is the error

WHAT error? What error message(s) do you get?

terryking228:
WHAT error? What error message(s) do you get?

printTemperature(); was not declared in this scope

Where does your loop() function end?
Why has your loop() function got printTemperature defined inside it?

Hi,
You need to slow down and look at the STRUCTURE of an Arduino sketch.

What goes where.. and why..

terryking228:
Hi,
You need to slow down and look at the STRUCTURE of an Arduino sketch.

What goes where… and why…

I will break my head against the wall! it is also a solution!

AWOL:
Where does your loop() function end?
Why has your loop() function got printTemperature defined inside it?

thanκ you That was the problem