I am new to arduino and i have a problem in this code. I have two sonar sensor hc-04 which i am using to find a obstacle in the path.
But the problem is i have to place sonar sensors in a straight path having some distance between them and something will cross the path
I have to detect that thing and detection will be done when both sensors has acknowledged that the thing has crossed
Problem is i am not be able to write a code which does that
I have try a method using flags such that when sensor 1 is true flag is high and it waits for sensor 2 to be true but it is not working
Kindly help me as soon as possible here is the code
const int trigpin=9;
const int echopin=10;
const int trigPin=11;
const int echoPin=12;
int flagA=0;
int flagB=0;
unsigned long cTime=0;
unsigned long pTime=0;
int interval = 15000;
void setup() {
pinMode(trigpin,OUTPUT);
pinMode(trigPin,OUTPUT);
pinMode(echopin,INPUT);
pinMode(echoPin,INPUT);
// pinMode(tpin,OUTPUT);
// pinMode(epin,INPUT);
Serial.begin(9600);
}
void loop()
{
int x,y,z;
x = calculateDistance(trigpin,echopin);
y = calculateDistance(trigPin,echoPin);
// z = calculateDistance(tpin,epin);
Serial.print("Distance X:");
Serial.print(x);
Serial.println();
Serial.print("Distance Y:");
Serial.print(y);
Serial.println();
if ( x > 1 && x <= 40)
{
flagA = 1;
cTime = millis();
Serial.println(cTime);
if ( flagA==1 && y <= 40)
{
Serial.print(cTime - pTime);
if ( (cTime - pTime) < 2000)
{Serial.println("basket"); }
pTime = cTime;
flagA = 0;
}
else
{
pTime = cTime;
flagA = 0;
}
}
delay(1000);
}
double calculateDistance( int trigpin, int echopin )
{
double duration, distance;
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration = pulseIn(echopin,HIGH);
distance = (duration/2)/29.4;
return distance;
}