Pages: [1]   Go Down
Author Topic: 2 arduino hc-sr04  (Read 719 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 use 2 sensor as an input. the sensor is hc-sr04 ultrasonic. here is the diagram.


http://imageshack.us/photo/my-images/600/capturemil.png/

there is two sensor, A & B

If there is motion on both sensor A &B, the LED suppose not to blink.
LED should only blink if sensor A have motion, sensor B no motion.

here is my code
Code:
#define trigPin 2
#define echoPin 3
#define btrigPin 9
#define bechoPin 7
#define LED 13

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);
}

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 (bdistance >= 200 || bdistance <= 0)
  {
  if (distance >= 3 && distance <= 150){
   
    Serial.print(distance);
    Serial.println(" cm");
    watch();
  }
  else {
     Serial.println("Out of range");
     digitalWrite(LED, LOW);
  }
  delay(50);
}
}

so far this code only work if i use sensor A, but if i combine, it do not work.
any help or suggestion are appriciated..
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11672
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps you should be passing timeout values to your calls to pulseIn() so you know they'll return in finite time?
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: