Go Down

Topic: Programming logic using 2 ultrasonic sensor hc-sr04 (Read 2324 times) previous topic - next topic

capab

hi im trying to make a project which if sensor A detect movement LED A will light, if sensor B detect movement LED B will light up, i tri the code, but sensor A only give "out of range" output..

here is the code.

Code: [Select]

#define trigPin 2            //sensor A
#define echoPin 3          //sensor A
#define btrigPin 7          //sensor B
#define bechoPin 8         //sensor B
#define LED 13
#define LED2 12

unsigned long blinkTime = 0;

void blink(void)
{  static int x = 0;                   /* Current LED state 0=>off, 1=>on     */
   digitalWrite(LED,x ^= 1);           /* Set LED to opposite state           */
   blinkTime = millis();               /* Schedule next state change          */
}
void watch(void)
{  if (millis() - blinkTime >= 1)    /* If it's time to change state        */
      blink();                         /*  then go do it                      */
}

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(btrigPin, OUTPUT);
  pinMode(bechoPin, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
  int bduration, bdistance;
  digitalWrite(btrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);
  bdistance = (bduration/2) / 29.1;
 
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 
  if (distance >= 3 && distance <= 200)
  {

    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite(LED, HIGH);
  }
 
   if (bdistance >= 3 && bdistance <= 200)
  {

   
    digitalWrite(LED2, HIGH);
  }
 
  else {
     Serial.println("Out of range");
     digitalWrite(LED, LOW);
     digitalWrite(LED2, LOW);
  }
  delay(50);
}


im also trying to make LED blink. is it possible to have

void blink2(void) for the other LED??

AWOL

#1
Apr 08, 2012, 10:25 am Last Edit: Apr 08, 2012, 10:48 am by AWOL Reason: 1
Could it be that the second sensor is getting the return echo from the first?
Try a few tens of milliseconds delay between the ranging.

BTW, when you've got more than one identical sensors, driven by identical software, then it's time for a function.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

capab

#2
Apr 08, 2012, 01:55 pm Last Edit: Apr 08, 2012, 02:07 pm by capab Reason: 1
will echo problem occur even the sensor are pointed at different direction??

function? im very new at programming, and i only know the basic. can you please give me an example?

AWOL

Quote
will echo problem occur even the sensor are pointed at different direction??

It depends on the environment.
Did you try the delay?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
  int bduration, bdistance;
  digitalWrite(btrigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);

I'd recommend taking a peek at the reference page. The pulseIn() function does not return an int.

capab

i change it to this..

Code: [Select]
int bduration, bdistance;
  digitalWrite(btrigPin, HIGH);
  delayMicroseconds(5000);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);
  bdistance = (bduration/2) / 29.1;


no progress..

AWOL

#6
Apr 08, 2012, 04:01 pm Last Edit: Apr 08, 2012, 04:04 pm by AWOL Reason: 1
Thanks for keeping us updated.

Quote
no progress..

And pulseIn  still doesn't return an int.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

capab

the led light up, but for sensor A, it give weird result




here is the code..

Code: [Select]
#define trigPin 2
#define echoPin 4
#define btrigPin 7
#define bechoPin 8
#define LED 13
#define LED2 12

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(btrigPin, OUTPUT);
 pinMode(bechoPin, INPUT);
 pinMode(LED, OUTPUT);
 pinMode(LED2, OUTPUT);
}

void loop() {
 
 int duration, distance;
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(1000);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 
 int bduration, bdistance;
 digitalWrite(btrigPin, HIGH);
 delayMicroseconds(1000);
 digitalWrite(btrigPin, LOW);
 bduration = pulseIn(bechoPin, HIGH);
 bdistance = (bduration/2) / 29.1;
 

 
 if (distance >= 3 && distance <= 50)
 {
 
   Serial.print(distance);
   Serial.println(" cm");
 
   digitalWrite(LED, HIGH);
 }
 
  if (bdistance >= 3 && bdistance <= 50)
 {

 
   digitalWrite(LED2, HIGH);
 }
 
 else {
    Serial.println("Out of range");
    digitalWrite(LED, LOW);
    digitalWrite(LED2, LOW);
 }
 delay(500);
}

AWOL

Did you give up on my idea of introducing a delay between pulses?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

capab

did you mean the delay between trigpin HIGH and trigpin LOW? i change it 5000 microsecond. still nothing happen. but right now im thinking my programming logic realy2 went wrong.

is my "if" function correct?

thank you for helping me.

AWOL

No put a delay between taking the a reading and the b reading.
Print raw results.
Figure out what's going on.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

capab

i already put the delay function.. the same thing occur. is there anything that i miss?

cjdelphi

could you please post back the value of the 2nd sensor please...


also, voltage matters here, my breakout board came with a regulator, 3v supply was not enough and had to switch to a 5v rail, without a regulator it's important you use the correct voltage you're supposed to use.

and lastly, the sensor could be dead, swap them around see what happens (you might try that first to rule out a broken sensor/ic)



capab

im using arduino uno, i donnt think there will be a problem with the voltage..

here is the value of 2nd sensor


Code: [Select]
Out of range
Out of range
13 cm
22 cm
11 cm
Out of range
Out of range
Out of range
13 cm
37 cm
16 cm
12 cm


the sensor is not broken, i test it one by one. it work great when alone.. but when i program it together, it went wrong..

AWOL

Quote
is there anything that i miss?

Did you try printing the raw results?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up