Hello all,
I have this project that I just cant seem to figure out, I have 4 HC-SR04 connected that check if there is something nearby. if that is the case the light of that streetlight is turned on. That part works. but what I want is that for instance lantern 2 is actived that led 1,2,3 are turned on. and if you go to lantern 3 leds 2,3,4 are turned on. so both the streetlight behind and infront of the person are on.
the led should dim back to default 10 when they are not used.
The problem I am expierencing is that when lantern 1 is activated and you walk to streetlight 2 the leds 1,2 are giving two commands at the same time. one tells it to dim to 10 and the other one tells it to brighten it 250. this makes the leds flicker and behave weirdly. I really need to fix this problem.
I would like to increase the speed of the dimming as well, but that is not as important.
int echoPin1 = 4; //GREEN
int trigPin1 = 8; //BLUE
int echoPin2 = 2; //YELLOW
int trigPin2 = 3; //ORANGE
int echoPin3 = 9; //PURPLE
int trigPin3 = 7; //GRAY
int echoPin4 = 13; //RED
int trigPin4 = 12; //BLACK
int led1 = 5;
int led2 = 10;
int led3 = 11;
int led4 = 6;
int bright1 = 10;
int bright2 = 10;
int bright3 = 10;
int bright4 = 10;
int distance = 10;
int fadeAmount = 10;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
}
void loop() {
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
long duration4, distance4;
//HC 1
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 / 2) / 29.1;
if (distance1 >= 500 || distance1 <= 0) {
Serial.println("Out of range");
}
//L 1
if (distance1 < distance ) {
if (bright1 < 250) {
bright1 = bright1 + 10;
analogWrite (led1, bright1);
analogWrite (led2, bright1);
}
if (bright1 >= 250) {
analogWrite (led1, 250);
analogWrite (led2, 250);
}
}
if (distance1 >= distance && bright1 > 10) {
bright1 = bright1 - 10;
analogWrite (led1, bright1);
analogWrite (led2, bright1);
}
//L 1
else {
Serial.print ( "Sensor1 ");
Serial.print ( distance1);
Serial.println("cm");
}
//HC 2
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1;
if (distance2 >= 500 || distance2 <= 0) {
Serial.println("Out of range");
}
//L 2
if (distance2 < distance) {
if (bright2 < 250) {
bright2 = bright2 + 10;
analogWrite (led1, bright2);
analogWrite (led2, bright2);
analogWrite (led3, bright2);
}
if (bright2 >= 250) {
analogWrite (led1, 250);
analogWrite (led2, 250);
analogWrite (led3, 250);
}
}
if (distance2 >= distance && bright2 > 10) {
bright2 = bright2 - 10;
analogWrite (led1, bright2);
analogWrite (led2, bright2);
analogWrite (led3, bright2);
}
//L 2
else {
Serial.print("Sensor2 ");
Serial.print(distance2);
Serial.println("cm");
}
//HC 3
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
distance3 = (duration3 / 2) / 29.1;
if (distance3 >= 500 || distance3 <= 0) {
Serial.println("Out of range");
}
//L 3
if (distance3 < distance) {
if (bright3 < 250) {
bright3 = bright3 + 10;
analogWrite (led2, bright3);
analogWrite (led3, bright3);
analogWrite (led4, bright3);
}
if (bright3 >= 250) {
analogWrite (led2, 250);
analogWrite (led3, 250);
analogWrite (led4, 250);
}
}
if (distance3 >= distance && bright3 > 10) {
bright3 = bright3 - 10;
analogWrite (led2, bright3);
analogWrite (led3, bright3);
analogWrite (led4, bright3);
}
//L 3
else {
Serial.print("Sensor3 ");
Serial.print(distance3);
Serial.println("cm");
}
//HC 4
digitalWrite(trigPin4, LOW);
delayMicroseconds(2);
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin4, LOW);
duration4 = pulseIn(echoPin4, HIGH);
distance4 = (duration4 / 2) / 29.1;
if (distance4 >= 500 || distance4 <= 0) {
Serial.print("Sensor4 ");
Serial.print(distance3);
Serial.println("cm");
}
//L 4
if (distance4 < distance) {
if (bright4 < 250) {
bright4 = bright4 + 10;
analogWrite (led3, bright4);
analogWrite (led4, bright4);
}
if (bright4 >= 250) {
analogWrite (led4, 250);
analogWrite (led3, 250);
}
}
if (distance4 >= distance && bright4 > 10) {
bright4 = bright4 - 10;
analogWrite (led3, bright4);
analogWrite (led4, bright4);
}
//L 4
else {
Serial.print("Sensor4 ");
Serial.print(distance4);
Serial.println("cm");
}
}

