what i need is
- when bring your hand near to one sensor - 1st led will be on and it stays on untill you bring your hand near to the other sensor
- when you bring your hand near to the other sensor 2nd led is on and turns off the 1st led.
keep on alternate like that when you bring your hand near to each sensor.
below is a code with 2 ultrasonic sensors.
currently one led light turns on when hand bring near to one sensor. then it turns off when you take your hand away. 2nd sensor works the same
#define trigPin 7
#define echoPin 6
#define led 12
#define trigPin1 9
#define echoPin1 8
#define led1 10
boolean lastled = LOW;
boolean currentled = LOW;
int sound = 250;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(led1, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led,LOW);
}
{
long duration, distance;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10) {
digitalWrite(led1, HIGH);
}
else {
digitalWrite(led1,LOW);
}
}
}