Lighting an LED when a HC-SR04 ultrasonic sensor is triggered

Hello everyone!

I can't seem to figure out why I can't get my Arduino's onboard LED to light up when an object is detected by one of my 5 ultrasonic sensors. It should trigger the LED high when an object is 59cm away or less in any of my sensors.

The wiring is good, and the serial monitor displays the correct data when measuring the distance of an object.

I know that my issue lies within my void LED_Trigger function, but I can't find the right combo. to make it work.

Any advice would be greatly appreciated!

Humbly,

  • Daniel

IDE V. 1.8.5

#include <NewPing.h>

#define SONAR_NUM 5 //Number of sensors.
#define MAX_DISTANCE 63 //Maximum distance (in cm) to ping.

const int LED = 13; //Onboard LED to light up when a sensor is triggered.
 
NewPing sonar[SONAR_NUM] = //Sensor object array.
{   
  NewPing(2, 3, MAX_DISTANCE), //Sensor 1 = Serial monitor "0". 3 Sensor side. Left.
  NewPing(4, 5, MAX_DISTANCE), //Sensor 2 = Serial monitor "1". 3 Sensor side. Middle.
  NewPing(6, 7, MAX_DISTANCE), //Sensor 3 = Serial monitor "4". 3 Sensor side. Right.
  NewPing(8, 9, MAX_DISTANCE), //Sensor 4 = Serial monitor "3". 2 Sensor side. Left.
  NewPing(10, 11, MAX_DISTANCE) //Sensor 5 = Serial monitor "2". 2 Sensor side. Right.
};

void setup() 
{
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
}

void loop() 
{ 
  for (uint8_t i = 0; i < SONAR_NUM; i++) //Loop through each sensor and display results. 
  { 
    delay(50); //Wait 50ms between pings.
    Serial.print(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");
  }
  Serial.println();
  void Trigger();
}

void Trigger()
{
  if(sonar[SONAR_NUM].ping_cm() <= 59)
    {
      digitalWrite(LED, HIGH);
    }
  else(digitalWrite(LED, LOW));
}

For starters,

  Serial.println();
  void Trigger();

Why do you have a void in that last line? That line is a function prototype and does not belong in a function; get rid of the void keyword.

Secondly

void Trigger()
{
  if(sonar[SONAR_NUM].ping_cm() <= 59)
    {

You only check the non-existing 6th element of the array. Remember array index starts from 0, so 4 is the index of the last element in the 5 element array.

@sterretje
Thank you very much for pointing out the void issue with my function! That one I should have known while writing it.
I do understand that arrays are zero indexed, but I'm not 100% on what you're saying by "You only check the non-existing 6th element of the array. Remember array index starts from 0, so 4 is the index of the last element in the 5 element array" How can I display this in my sketch?

If(sonar[4].ping_cm() <= 59)? (Since there are 5 sensors and it's zero indexed, I put 4 in between the brackets so that the code would run through all the sensors)

BTW, I hate working with arrays so I apologize for my ignorance on the subject. I'm going to research right now more about them.

UPDATE: If anyone reading this needs help with arrays, this YouTube video helped me a lot.

UPDATE PT. 2: Adding the [4] caused the LED to light up but only on that sensor and not all of them. Close!... Tried [0,1,2,3,4]; same results.

Maybe you need something like a for loop there.

@sterretje
Okay, I'll try playing around with a for loop instead of the if/else. Thanks!

UPDATE:
As I was playing around with the for loop, it occurred to me to go back to the if/else and try writing a function for each individual sensor since I couldn't get the array to work the first time. I'm positive that my way is over complicating my code, but so far it works.

FINAL UPDATE:
@sterretje (+1 Karma)
It worked adding an individual function for each sensor.
Thank you very much for the help, sir. I couldn't have done it without you! :smiley: I'll continue learning about arrays for future use.

The array and a for loop was the sensible way to do this.

The problem was that you did not call Trigger() inside the for loop and when you did it once it only tests sensor number 5 which does not exist.

@UKHeliBob (+1 Karma)
:o
Oooooh. Well, damn. Yeah, that makes total sense now.