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