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