Parallel program functions

Hello,

I have the following issue:

My program reads sensor values and depending on a servo motor.
If there is a boolean condition true, a function called sirene() must be started.

That means.
If the condition is true, the function sirene() will start. And after that the servo gets the position parameter. But the sirene() function has got delays in it. The sirene() function is responsible for controlling a little buzzer, that generates an alarm sirene.
That means if the condition is true, I have to wait 4 seconds to start the setServo() function.

Is there a better way to start both functions parallel?

#include <Servo.h>

int ServoPos = 0;
int RawPosition = 0;

int siren = 50;
int highNote = 660;
int lowNote = 440;

void setup()
{

  pinMode(siren, OUTPUT);
  myservo.attach(51);

}


void loop()
{

	ServoPos = map(RawPosition, 500, 1500, 55, 125);		//min. angle 55° max angle 125°
	
	if (CONDITION)
		{
		sirene(true);
		setServo(ServoPos);
		}
	else
		{
		sirene(false);
		setServo(90);
		}

}


void setServo(int position)
{

	myservo.write(position);

}



void sirene(boolean value)
{


	if (value)
	{
		tone(siren, lowNote, 1000);      // wait for a second
		delay(1000);                          // wait for a second
		tone (siren, highNote, 1000);    // wait for a second
		delay(1000);                          // wait for a second
	}
}

Don't use delays.

Thank you for your reply.

But I don't get it, how to change my code.
The only thing is the delay in my sirene() function.

But I don't get it, how to change my code.

How would YOU make the buzzer go off and on, while also doing other things? You have a watch, a pencil. and some paper.

When you can explain how you would do it, then writing the code is trivial, especially with the blink without delay example as a clue-by-four.

julianpe:
Thank you for your reply.

But I don't get it, how to change my code.
The only thing is the delay in my sirene() function.

Read my link. In your main loop you could periodically call the sirene() function, and it could turn the tone on and off.

Look, in your motor car, the whole car doesn't stop when you activate the indicators, does it? How do you imagine they achieved that?

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking.

...R