how i can compine these two sketches ?

hello...
Im working on my final project, that to compine two sketches together (ultrasonic sensor and mlx90614 non-contact temperature sensor).
the project is all about taking the distance that the ultrasonic sensor measures and if a high temperature is mesuared by the distance that ultrasonic sensor gave a piezzo starts to pip.

please help
thanks

What have you tried ?

i have the codes for each sensor:

ultrasonic:

//Ultasonic Sensor

//Pins connected to the ultrasonic sensor
#define trigPin 2
#define echoPin 3
//LED pins
#define ledGreen 9
#define ledRed 8

//Pin connected to the piezo buzzer
#define alarm 11

int range = 5;//range in inches

void setup() {
// initialize serial communication:
Serial.begin(9600);
//initialize the sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//initialize LED pins
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
//set LEDs
digitalWrite(ledGreen, HIGH);
digitalWrite(ledRed, LOW);

}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);

// Take reading on echo pin
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

if(inches < 5) {
Serial.println("DANGER");
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, HIGH);
tone(alarm, 2000);
delay(100);
} else {
Serial.println("GOOD");
digitalWrite(ledGreen, HIGH);
digitalWrite(ledRed, LOW);
noTone(alarm);
delay(100);
}

delay(200);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

mlx90614

#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

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

Serial.println("Adafruit MLX90614 test");

mlx.begin();
}

void loop() {
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C");
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempF());
Serial.print("*F\tObject = "); Serial.print(mlx.readObjectTempF()); Serial.println("*F");

Serial.println();
delay(500);
}

the question is how can i compine these two codes ...

Please post you code in </> tags. first

This Simple Merge Demo may give you some ideas.

...R

In principle combining programs is easy but in practice there can be problems.

Start a new, empty sketch.
Copy the #includes, global variable declarations and object instantiations from both sketches into the new one
Resolve any conflicts of pin numbers, variable names etc
Copy the body of the setup() functions into the new setup() function
Resolve any conflicts such as baud rates and remove duplicate lines
Copy the body of the loop() function of program one into a new function in the combined sketch and name it newLoop1()
Copy the body of the loop() function of program two into a new function in the combined sketch and name it newLoop2()
Call newLoop1() and newLoop2() from the loop() function in the combined program

OK, you now have a combined program but what do you want it to do ? Any delay()s in your programs will cause it to stop running for a while. Is this acceptable ? Do you want functions from one original program, or the whole program, to be called from the other one?

#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

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

  Serial.println("Adafruit MLX90614 test");  

  mlx.begin();  
}

void loop() {
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC()); 
  Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C");
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempF()); 
  Serial.print("*F\tObject = "); Serial.print(mlx.readObjectTempF()); Serial.println("*F");

  Serial.println();
  delay(500);
}

i want to but an (if) function that if the distance of the object is smaller than 2m for example and the temperature of the object is higher than 50C the piezzo starts to peep

Have you actually tried to combine the two programs ?

the proplem is that i dont know how

hamza113:
the proplem is that i dont know how

Did you read reply #4?

i read it but i dont know which things to put in the new sketch and which not

I dont see why you cant combine them

In the ultrasonic sketch, you know how to get inches.

 inches = microsecondsToInches(duration);

In the Temperature sketch, you know how to read temperature.

mlx.readObjectTempC()

Now in a new combined sketch, can you able to do.

void loop(){
  inches = microsecondsToInches(duration);
  temp = mlx.readObjectTempC()
  if(inches>2 && temp>50){
    //do the task
  }
}

Note: this is not a valid sketch, it just a pseudo code.