I want to turn off the components after 5 seconds if the distance remained constant

#include <NewPing.h>

#define TRIGGER_PIN 12 // Ultrasonic sensor trigger pin
#define ECHO_PIN 11 // Ultrasonic sensor echo pin
#define MAX_DISTANCE 300 // Maximum distance in centimeters

#define BUZZER_PIN 6 // Buzzer pin
#define LED1_PIN 3 // LED 1 pin
#define LED2_PIN 8 // LED 2 pin
#define LED3_PIN 5 // LED 3 pin

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

unsigned long lastDistanceChangeTime = 0; // Time of the last distance change
const unsigned long distanceChangeDelay = 20000; // 5 seconds

int currentDistance = 0;

void setup() {
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
Serial.begin(9600);
}

void loop() {
unsigned int distance = sonar.ping_cm();

if (distance != currentDistance) {
// Distance has changed
currentDistance = distance;
lastDistanceChangeTime = millis();
if (distance < 5) {
// Distance is less than 5 meters
allComponentsOff();
} else if (distance >= 5 && distance <= 10) {
// Distance is between 5 meters and 10 meters
buzzerAndLEDsOn();
} else if (distance > 10 && distance <= 20) {
// Distance is between 10 meters and 20 meters
twoLEDsAndBuzzerOn();
} else if (distance > 20 && distance <= 30) {
// Distance is between 20 meters and 30 meters
oneLEDAndBuzzerOn();
}
}

// Check if the distance has remained constant for 5 seconds
}

void allComponentsOff() {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
}

void buzzerAndLEDsOn() {
if (millis() - lastDistanceChangeTime >= distanceChangeDelay) {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
} else {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, HIGH);
digitalWrite(LED3_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
delay(500);
}
}

void twoLEDsAndBuzzerOn() {
if (millis() - lastDistanceChangeTime >= distanceChangeDelay) {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
} else {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, HIGH);
digitalWrite(LED3_PIN, LOW);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
delay(500);
}
}

void oneLEDAndBuzzerOn() {
if (millis() - lastDistanceChangeTime >= distanceChangeDelay) {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
} else {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
delay(500);
}
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

welcome to the arduino-forum.
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

Hi @

you should re-edit your first post to have your code as a code-section.
This can be done with a few mous-clicks.

Below your posting you find a pencil-icon that will activate editing-mode

Click on this icon.
Then mark all the code. Then delete the marked code.

You can post code by using this method that adds the code-tags

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Just a note: the scale HC-SR04 uses is CM (in), not M (yd).

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