UPDATE:
Got it working thanks to all your guys' help! +1 Karma for all. There was just no boolean logic to change the state back to the original state once a sensor was tripped and the function executed. A very valuable lesson learned!
IDE v1.8.5
#include <NewPing.h>
#include <L298N.h>
#define SONAR_NUM 5 //Number of sensors.
#define MAX_DISTANCE 61 //Maximum distance (in cm) to ping.
//Driver# 1 - Cube Motor# 1.
//L298N Motor Driver, Outputs 1-2. Cube motor# 1.
#define ENA 12 //A PWM Pin (~)
#define IN1 22 //A digital pin
#define IN2 23 //A digital pin
L298N Motor_Driver_1_RS(ENA, IN1, IN2);
NewPing sonar[SONAR_NUM] = //Sensor object array.
{
NewPing(2, 3, MAX_DISTANCE), //Sensor 1
NewPing(4, 5, MAX_DISTANCE), //Sensor 2
NewPing(6, 7, MAX_DISTANCE), //Sensor 3
NewPing(8, 9, MAX_DISTANCE), //Sensor 4
NewPing(10, 11, MAX_DISTANCE) //Sensor 5
};
void setup()
{
Serial.begin(9600); //Starts serial monitor at 9600 bauds.
Motor_Driver_1_RS.setSpeed(90);
}
void loop()
{
for (uint8_t i = 0; i < SONAR_NUM; i++) //Loop through each sensor and display results.
{
delay(1000); //Wait 50ms between pings (about 20 pings per second)
Serial.print(i);
Serial.print("=");
Serial.print(sonar[i].ping_cm());
Serial.print("cm ");
}
Serial.println();
Sensor_Trigger();
}
void Sensor_Trigger()
{
boolean State = false;
for (int sonarCount = 0; sonarCount < SONAR_NUM; sonarCount++)
{
if (sonar[sonarCount].ping_cm() < 57)
{
State = true;
}
}
if (State == true)
{
Motor_Driver_1_RS.backward();
Serial.println ("Motor Driver 1 R/S is ON - Backward");
delay(500);
Motor_Driver_1_RS.stop();
Serial.println ("Motor Driver 1 R/S is OFF");
boolean State = false;
}
}