Please help: Using multiple HC-SR04 for movement detecting streetlights

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");
}
}

First: this is not an installation or troubleshooting issue. Please move it to programming.
Second: you have too much code. This makes it difficult to isolate problems and test things.
You should have one function that reads any specified sensor and returns the range.
You may be pinging the sensors too frequently - this can cause problems when a late echo from one is interpreted as a close echo by the next sensor.

(Mod edit comment removed)

Some code snippets to help you reduce the amount of code you have (uncompiled, untested)

struct {
  const byte echoPin;
  const byte trigPin;
} sensors = { {4, 8}, {2, 3}, {9, 7}, {13, 12}};
const int nSensors = sizeof (sensors) / sizeof (sensors [0]);

...

void setup() 
{
  ...
  for (auto& sensor : sensors) {
    pinMode(sensor.trigPin, OUTPUT);
    digitalWrite (sensor.trigPin, LOW);
    pinMode(sensor.echoPin, INPUT);
  }
}
...
int range (int sensorIndex)
{
  if (sensorIndex < 0 || sensor Index >= nSensors)
    return -1;
  digitalWrite(sensors [sensorIndex].trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(sensors [sensorIndex].trigPin, LOW);
  unsigned long duration = pulseIn(sensors [sensorIndex].echoPin, HIGH);
  return (duration / 2) / 29.1;
}

void loop() 
{
  int distance1 = range (0);
  ... etc, etc.

Factoring the code as above, is absolutely critical to your request. Whatever you do, applies to all the lights. So the risk of typographical errors and omissions is huge if you have to modify the code for each one individually.

For the dimming, what I would do is make functions that turn a light on/off with dimming, encapsulated in the function. A call to that function would say nothing about any dimming state or process, just a simple on/off command. Then have a service routine in loop() to control the dimming animation for all the lights.

11 posts were split to a new topic: Dictionary Corner

@davidox21 ,
I'm sure you came here for help with your project, not a spelling lesson.

The rest of you going off topic so much can result in a ban. You can continue your spelling discussion over in Bar Sport, for this topic please stick to helping @davidox21 with his project.

Thank you.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.