ARDUINO CODE WITH ULTRASONIC SENSOR

what i need is

  1. 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
  2. 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);
}

}
}

The problem is caused by how you have written the code. Basically it says "if the hand is currently less than 11 cm from the sensor then turn on the LED otherwise turn off the LED". You need to change the code so that it does "if the hand has just become less that 11 cm from the sensor then turn on the LED"

Look at the StateChangeDetection example in the IDE to see the principle. Apply it to both sensors to switch the LEDs on and off alternately.

Cross-post deleted.

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.