SOLVED - Replacing delay function with something else (variable value)

I have 3 servo's that will go up and down, this happens after each other with a delay:

  • first the first servo will go down
  • after a while the first servo will go up and the second servo will go down
  • after a while the second servo will go up and the third servo will go down
  • etc..

the delay is depending on a input that I get from a volume sensor (this values are being send every x seconds).
I do not want to use the delay function to do this because it will pause my sketch.
What is the best replacing option to use? (the interval/delay is variable, updated every x seconds during the sketch is running).

volume_2 = analogRead(A0);
delay_movement = map(volume_2, 400 ,960, 1300,100);

movement_value = 100 - 20;
  
servo_1.write(movement_value);
delay(delay_movement); 
servo_1.write(height_value);
servo_2.write(movement_value);
delay(delay_movement); 
servo_2.write(height_value);
servo_3.write(movement_value);
delay(delay_movement);
servo_3.write(height_value);

You can use the method for waiting without delay() function described in BlinkWithoutDelay example of Arduino IDE (Examples\02. Digital\BlinkWithoutDelay)

Anika, I'm new to Arduino but have been programming for a long time.
I'm getting into this delay-less programming and also posted elsewhere about it.

I think you may be looking for something like this:

void loop()
{
long currentTime = millis();     // Get the current time only ONCE per loop

// A decision to start the sequence has been made.  Calculate all movement times.
// A time value of 0 will mean it's already been done; do not keep doing it until re-enabled.

servo_1_movement_time = currentTime();
servo_1_height_time = servo_1_movement_time + delay_movement;
servo_2_movement_time = servo_1_height_time;
servo_2_height_time = servo_2_movement_time + delay_movement;
servo_3_movement_time = servo_2_height_time;
servo_3_height_time = servo_3_movement_time + delay_movement;

// Check all 6 event times and act if it's time has come.  Disable for next pass of loop.
if (servo_1_movement_time > 0  &&  currentTime > servo_1_movement_time)
{
     servo_1.write(movement_value);
     servo_1_movement_time = 0;
} 
if (servo_1_height_time > 0  &&  currentTime > servo_1_height_time)
{
     servo_1.write(height_value);
     servo_1_height_time = 0;
} 
if (servo_2_movement_time > 0  &&  currentTime > servo_2_movement_time)
{
     servo_2.write(movement_value);
     servo_2_movement_time = 0;
} 
if (servo_2_height_time > 0  &&  currentTime > servo_2_height_time)
{
     servo_2.write(height_value);
     servo_2_height_time = 0;
} 
if (servo_3_movement_time > 0  &&  currentTime > servo_3_movement_time)
{
     servo_3.write(movement_value);
     servo_3_movement_time = 0;
} 
if (servo_3_height_time > 0  &&  currentTime > servo_3_height_time)
{
     servo_3.write(height_value);
     servo_3_height_time = 0;
} 

// All your other code for loop goes below with no delays!
}  // end of loop

Thank you Techylah, that worked :wink:

Makes my day! You're quite welcome. Good luck!