DJ's light, loop in wait

Hello,
I did a DJ's light with a mirror, stepper motor and servo, I'm a beginner, it's my first program so it's not great, I preferred to start simple to not have too much trouble at first.

It's not the code of the century but it works almost as I would like, the almost is that I can not get out of the loop as I would like:

When the sound is detected (digital output) my loop starts well but when I cut the sound it finished all instructions (until that's normal) I tried to put a condition "if" with a "break", it It works too well so if I have a weak sound at some point I leave the loop; but instead of going out of the loop I would like to pause it with a delay that would avoid the false stops so let's not imagine for 1 second, the loop is paused and when it's back the loop restarts and then I stuck so if you have a little trick ...

Another thing is there a way to put all these instructions in a table or even two would be the ideal left and right seen facing the mirror (less than 300 and over 300 stepper) and randomly draw the instructions a shot in table 1 (left) and the next shot in table 2 (right), good for that I think there is a course to be taken that is far from beginners.

my english is very bad sorry
thank you

#include <VarSpeedServo.h>
#include <AccelStepper.h>
VarSpeedServo myservo; // create servo object to control a servo
                        // a maximum of eight servo objects can be created
 
const int servoPin = 10; // the digital pin used for the servo
// Define a stepper and the pines it will use
AccelStepper stepper (1,9,8); // Defaults to AccelStepper :: DRIVER 9, 8
void setup ()
{

  myservo.attach (servoPin); // attaches the servo on pin 9 to the servo object
  myservo.write (0.255, true); // set the intial position of the servo, as fast as possible, wait until done
    stepper.setMaxSpeed (7000.0);
    stepper.setAcceleration (7000.0);
    stepper.runToNewPosition (-600);
    stepper.setCurrentPosition (0);
}
void loop () {
while (digitalRead (2) == 1)
{
 
    stepper.runToNewPosition (10);
    myservo.write (10,75, true); // write (0-180 degrees, speed 1-255, wait to complete true-false)
    stepper.runToNewPosition (600);
    myservo.write (40,75, true);
    stepper.runToNewPosition (100);
    myservo.write (20,75, true);
    stepper.runToNewPosition (50);
    myservo.write (10,75, true);
    stepper.runToNewPosition (500);
    myservo.write (45,75, true);
    // if (digitalRead (2) == 0); {break;};
    stepper.runToNewPosition (450);
    myservo.write (55,75, true);
    stepper.runToNewPosition (550);
    myservo.write (30,75, true);
    stepper.runToNewPosition (150);
    myservo.write (15.75, true);
    stepper.runToNewPosition (200);
    myservo.write (30,75, true);
    stepper.runToNewPosition (0);
    myservo.write (10,75, true);
    // if (digitalRead (2) == 0); {break;};
    stepper.runToNewPosition (250);
    myservo.write (40,75, true);
    stepper.runToNewPosition (420);
    myservo.write (20,75, true);
    stepper.runToNewPosition (120);
    myservo.write (5.75, true);
    stepper.runToNewPosition (530);
    myservo.write (45,55, true);
    stepper.runToNewPosition (120);
    myservo.write (10,75, true);
    stepper.runToNewPosition (10);
    myservo.write (25,75, true);
    stepper.runToNewPosition (450);
    // if (digitalRead (2) == 0); {break;};
    }

if (digitalRead (2) == 0) {
    stepper.runToNewPosition (0);
    myservo.write (10,55, true);
  }
  }

When the sound is detected (digital output) my loop starts well but when I cut the sound it finished all instructions (until that's normal) I tried to put a condition "if" with a "break", it It works too well so if I have a weak sound at some point I leave the loop;

What sound sensor are you using that outputs a digital signal? If you really have such a thing, it should have some kind of adjustment to control how much sound is required to trigger the output.

And ... put a delay in , so the sound level must be low for say 5sec before it stops the lights . Quiet patches of music will then not stop it .

put a delay in , so the sound level must be low for say 5sec before it stops the lights . Quiet patches of music will then not stop it .

Putting a delay in would be stupid.

The proper solution is to determine when the pin changed state, and how long is has been since then. The blink without delay philosophy solves lots of problems that do not involve blinking LEDs.

PaulS:
What sound sensor are you using that outputs a digital signal? If you really have such a thing, it should have some kind of adjustment to control how much sound is required to trigger the output.

thanks for reply,

it's a KY-037 and yes there is a setting but that's not the problem, the setting is good, I'm just trying to pause the loop if a silence is in the music or if I cut the music myself, now if I cut the music the movement continues until the end of the loop.

a video here

in the code I put this: // if (digitalRead (2) == 0); {break;}, it works well to exit but not to put "pause" but I can not find any reference to "wait" or "do nothing"

but I can not find any reference to "wait" or "do nothing"

It's hidden in the cleverly (?) named delay() function.

PaulS:
It's hidden in the cleverly (?) named delay() function.

so I did not understand how "delay" works

I did not understand how to do with "delay" but I bypassed the problem by testing the sensor before each movement and this is the result I wanted.
I do not know if it's good to do like that but it works.

video here

#include <VarSpeedServo.h> 
#include <AccelStepper.h>
VarSpeedServo myservo;  // create servo object to control a servo 
                        // a maximum of eight servo objects can be created 
 
const int servoPin = 10; // the digital pin used for the servo
// Define a stepper and the pins it will use
AccelStepper stepper(1,9,8); // Defaults to AccelStepper::DRIVER 9, 8
void setup()
{ 

  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  myservo.write(30,255,true); // set the intial position of the servo, as fast as possible, wait until done  
    stepper.setMaxSpeed(7000.0);
    stepper.setAcceleration(7000.0);
    stepper.runToNewPosition(-600);
    stepper.setCurrentPosition(0);
}
void loop(){
  if (digitalRead(2)==1)
    {stepper.runToNewPosition(10);
    myservo.write(10,75,true);}// write(degrees 0-180, speed 1-255, wait to complete true-false)
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(600);
    myservo.write(40,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(100);
    myservo.write(20,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(50);
    myservo.write(10,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(500);
    myservo.write(45,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(450);
    myservo.write(55,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(550);
    myservo.write(30,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(150);
    myservo.write(15,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(200);
    myservo.write(30,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(0);
    myservo.write(10,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(250);
    myservo.write(40,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(420);
    myservo.write(20,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(120);
    myservo.write(5,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(530);
    myservo.write(45,55,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(120);
    myservo.write(10,75,true);}
   
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(10);
    myservo.write(25,75,true);}
    
    if (digitalRead(2)==1)
    {stepper.runToNewPosition(450);
    myservo.write(25,45,true);}
    }