Hello,
I am having code block lol. I am trying to code something like this with if statements:
If this makes any sense, I am trying to print different soemthingon my serial monitor when an objected is detected within a certain distance ("STOP") of my 4 Ultrasonic sensors. However, after the object is not detected anymore, i want to print ("I am clearing Stop so now Go") only once, then proceed with printing ("Go")
Unfortunately, its printing ("GO") and ("I am clearing Stop so now Go") when an object is not within the certain distance
#include <NewPing.h>
//Defining UltraSonic Sensor #1 Pins
#define TRIGGER_PIN A0 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A0 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
//Defining UltraSonic Sensor #2 Pins
#define TRIGGER_PIN2 A1 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2 A1 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE2 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
//Defining UltraSonic Sensor #3 Pins
#define TRIGGER_PIN3 A4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN3 A4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE3 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
//Defining UltraSonic Sensor #4 Pins
#define TRIGGER_PIN4 A6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN4 A6 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE4 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
//Ping Setup for Ultrasonic
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2); // NewPing setup of pins and maximum distance.
NewPing sonar3(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE3); // NewPing setup of pins and maximum distance.
NewPing sonar4(TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE4); // NewPing setup of pins and maximum distance.
void setup()
{
Serial.begin(115200);
}
void loop(void)
{
if (sonar.ping_cm() >=1 && sonar.ping_cm() <=50 || sonar2.ping_cm() >=1 && sonar2.ping_cm() <=50 || sonar3.ping_cm() >=1 && sonar3.ping_cm() <=50 || sonar4.ping_cm() >=1 && sonar4.ping_cm() <=50){
Serial.println("Stop");
}
else {
Serial.println("I am clearing Stop so now Go");
}
Serial.println("Go");
}