Go Down

Topic: 2 arduino hc-sr04 (Read 1 time) previous topic - next topic


Hi, im trying to use 2 sensor as an input. the sensor is hc-sr04 ultrasonic. here is the diagram.


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: [Select]
#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);
  digitalWrite(btrigPin, LOW);
  bduration = pulseIn(bechoPin, HIGH);
  bdistance = (bduration/2) / 29.1;
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (bdistance >= 200 || bdistance <= 0)
  if (distance >= 3 && distance <= 150){
    Serial.println(" cm");
  else {
     Serial.println("Out of range");
     digitalWrite(LED, LOW);

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


Perhaps you should be passing timeout values to your calls to pulseIn() so you know they'll return in finite time?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131