ultrasonic sensors works fine until i merge codes into it

hey everyone newbie here, I am currently working on a project which uses 2 ultrasonic sensors basically, the code works fine when testing only ultrasonic but when I merge into the main code ultrasonic doesn't work. thanks in advance

//for ultraSonic Sensor 1
//-----------------------
  const byte echoPin = 2; // Arduino pin tied to echo pin on the ultrasonic sensor 1.
  const byte trigPin = 3; // Arduino pin tied to trigger pin on the ultrasonic sensor 1.
//for ultraSonic Sensor 2
//-----------------------
  const byte echo2Pin = 4; // Arduino pin tied to echo pin on the ultrasonic sensor 2.
  const byte trig2Pin = 5; // Arduino pin tied to trigger pin on the ultrasonic sensor 2.

void setup() {
  // put your setup code here, to run once:
    //communication channel baetween hardware and software with baud rate of (9600)
    Serial.begin (9600);
    //-----------------------------------------------------------------------------
    //ultrasonic sensors trigger and echo pin modes trigger pins on pwms
    //------------------------------------------------------------------
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(trig2Pin, OUTPUT);
    pinMode(echo2Pin, INPUT);
    //------------------------------------------------------------------
}
void loop() {
  // put your main code here, to run repeatedly:
    unsigned long sensor1 = range (trigPin, echoPin);
    delay(30);
    unsigned long sensor2 = range (trig2Pin, echo2Pin);
    delay(30);


    Serial.print("Distance: ");
    Serial.println(sensor1);// + "\t" + sensor2);
}

unsigned long range (byte trig, byte echo)
{
digitalWrite(trig, LOW);
delayMicroseconds(10);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
unsigned long duration = pulseIn(echo, HIGH);
return duration/58.2;
}

this code works just fine .. i am using arduino mega 2560 and i am making a smart fire extinguishing vehicle voice recognition based.. bluetooth module, car code , flame sensor code all works fine but this one when i merge doesnt work i check on serial monitor it just sends 0 0 P.s i am sorry if i am not making myself clear, using the forum first time

I have code that uses 2 ultrasonic sensors. When 1 sensor detects an object the other sensor is enabled to confirm the 1st sensors detection.

You are welcome.

Perhaps posting your code here in code tags might produce better results.

You obviously failed to read this, or if you did, failed to follow the guidelines. Until you do, we can't offer much if any help.

Idahowalker, i am just mounting it on car chassis both at the front, car chassis is of custom size so 1 sensor cannot cover the whole view.

wvmarie, i am sorry sir i didn’t read the guidelines first … i edit the post and add the ultrasonic sensor code

P.s : i have attached the whole sketch in this post… thanks again for helping me out

mergingTime.ino (11 KB)

Why not use the NewPing library found at: http://playground.arduino.cc/Code/NewPing ?

int led4 = 5;
 const byte trig2Pin = 5;

Can pin 5 be shared?

Not only that one... (oh, and do make those ints for pin declaration into const byte like you use later - much more efficient).

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;

const byte echoPin = 2; // Arduino pin tied to echo pin on the ultrasonic sensor 1.
const byte trigPin = 3; // Arduino pin tied to trigger pin on the ultrasonic sensor 1.
const byte echo2Pin = 4; // Arduino pin tied to echo pin on the ultrasonic sensor 2.
const byte trig2Pin = 5; // Arduino pin tied to trigger pin on the ultrasonic sensor 2.

Trig pins can be shared with an LED (wouldn't know why you'd do that unless when seriously running out of pins, but it can be done if you're really careful), echo pins definitely not.

groundFungus, aaahhh such a silly stupid mistake i was scratching my head from yesterday pin 5 indeed cannot be shared i was using it for testing when i merge that code i didnt notice …

wvmarie, it wasn’t sharing actually in the setup I set 1 to output and other to input so the last recent update changed the previous one … that’s why it wasn’t working … it works now
and i’ll change the declaration part into const byte data type :slight_smile: thanks so much

thanks all of you <3