While? If statement? loop code until sensor activates it to stop

Hi I’m pretty new to this and I would like some help what type of statement I should be using or placement?

I’m trying to get the code to start with the push button and then have the LED, buzzer and servo code to repeat until itself until motion is detected with 10cm range which would turn the device off.

Currently, the Arduino begins with the button and goes through the program once (does not repeat/loop), ignoring the dual ultrasonic sensor.

Thanks, any help is appreciated.

const int ledPin = 13;                      // assigns LED to pin 13
const int sound = 8;                     // assigns buzzer to pin 8
const int trigPin = 6;                  // assigns trigger to pin 7
const int echoPin = 5;                  //assigns echo to pin 7
const int buttonPin = 2; //assigns push button to digital pin 2
int buttonState = LOW; //creates a variable to check button state is on (HIGH) or off (LOW)
long duration;
int distance_cm;

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);           // LED as output
  pinMode(sound, OUTPUT);         // buzzer as output
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT); //sets the const int buttonPIN as an input
  pinMode(trigPin, OUTPUT);  // sets trigger as output
  pinMode(echoPin, INPUT);   // sets echo as input
}

void loop()                        // run the program continuously
{
  buttonState = digitalRead(buttonPin); //digitally reads the button state
  while (buttonState == HIGH) // if button pressed
  {
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, HIGH);        // turn the LED on
    tone(sound, 1000, 250);         // play a tone with 1000 Hz for 250 ms
    delay(1000);                    // wait for 1 seconds
    digitalWrite(ledPin, LOW);         // turn the LED off
    tone(sound, 6000, 250);         // play a tone with 6000 Hz for 250 ms
    delay(2000);                    // wait for 2 seconds
    myservo.write(90);              // tell servo to go to position in variable
    delay(500);                    // wait for 0.5 seconds
    myservo.write(0);              // neutral
    delay(500);                    // wait for 0.5 seconds
    myservo.write(90);              // neutral
    delay(500);                    // wait for 0.5 seconds
    myservo.write(0);              // neutral
    delay(500);                    // wait for 0.5 seconds
    myservo.write(90);              // neutral
    delay(500);                    // wait for 0.5 seconds
    myservo.write(0);              // neutral
    buttonState = digitalRead(buttonPin);
  }
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance_cm = (duration * 0.034) / 2.0;

  if (distance_cm >= 10 || distance_cm <= 0) // if motion detected with 10cm, device is turned off
  {
    digitalWrite(ledPin, LOW); // LED off
    myservo.write(0);  // servo off
    noTone(sound);  // buzzer off
  }
}

You can refer to this How to use button to start/stop the loop

You need to remove delay() from your programming vocabulary (for ever).

Research ‘State Machine programming’ and BWD ‘Blink Without delay’.

When your switch changes state to “pressed”, you set a ‘flag’ variable.

As long as that ‘flag’ is set, you do your stuff.

When motion is detected with 10 cm range, reset the flag variable and turn the device off.

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What "dual ultrasonic sensor"?
I only see pinouts for one ultra-sonic sensor unit.

Tom... :slight_smile: