Pages: [1] 2   Go Down
Author Topic: Programming logic using 2 ultrasonic sensor hc-sr04  (Read 2030 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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??
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 287
Posts: 25681
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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.
« Last Edit: April 08, 2012, 03:48:53 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: April 08, 2012, 07:07:36 am by capab » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 287
Posts: 25681
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
will echo problem occur even the sensor are pointed at different direction??
It depends on the environment.
Did you try the delay?
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i change it to this..

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

no progress..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 287
Posts: 25681
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for keeping us updated.

Quote
no progress..
And pulseIn  still doesn't return an int.
« Last Edit: April 08, 2012, 09:04:08 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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




here is the code..

Code:
#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);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 287
Posts: 25681
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you give up on my idea of introducing a delay between pulses?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 287
Posts: 25681
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No put a delay between taking the a reading and the b reading.
Print raw results.
Figure out what's going on.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 29
Posts: 2350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

here is the value of 2nd sensor


Code:
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..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 287
Posts: 25681
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is there anything that i miss?
Did you try printing the raw results?
Logged

"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.

Pages: [1] 2   Go Up
Jump to: