I need help getting lights and sound to go simultaneously

I’m doing this project in school and I’m creating a replica police light and siren, and I’m having a problem getting the siren and the lights to play at the same time. Right now my code attached has the siren going first but when I press the button the lights wait until the siren is done making the sound and I’m not sure why because I thought sound and light are 2 separate outputs and should work at the same time

test.ino (1.51 KB)

delay (500);

If you want those things to both happen, then you can't tell the processor to stick its head in the sand for half a second and do nothing. Look at the "Blink Without Delay" example or the sticky thread with the example for several things at the same time. Either of those will show you how to check if it is time to do a thing instead of waiting until it is time to do it. That way you can keep doing other things while the one thing is running.

I'm no expert but I do believe that if you want two actions going on at the same time, you should avoid the use of delay() [Like delta said] as it tends to freeze up other processes. Being a tad more specific, I personally using the millis() function. Just define when you want something to happen:

end_time = millis()+my_delay;

and in the main loop use:
if(millis() > end_time) { start the lights up }

end_time = millis()+my_delay;

and in the main loop use:
if(millis() > end_time) { start the lights up }

Don't do it that way. That code has a bug in it. Millis gives you an unsigned long, so it eventually rolls back over to zero. With this code, you will have a huge problem when that happens.

Always use subtraction to calculate time periods. So that the rollover happens in the math too and things still work even when millis goes back to zero.

startTime = millis();

  ....

if(millis() - startTime > howLongToKeepDoingIt){

I'm very new to Arduino and I've briefly heard about but I couldn't really understand how I could implement my exact part into it so if you could show me that would be great

DangerDolan:
I'm very new to Arduino and I've briefly heard about but I couldn't really understand how I could implement my exact part into it so if you could show me that would be great

How do you expect to understand something that you've only "briefly heard"
and not studied at all? This is the top number one lesson in Arduino. It's the main one everyone runs into. There are literally hundreds of thousands of tutorials on this topic available. Why should someone rewrite all of that just especially for you? Are you too good to go read the ones that are out there?

my apologies I just have spent 3 class' trying to figure out how to do this

DangerDolan:
my apologies I just have spent 3 class' trying to figure out how to do this

What have you read so far?

I understand the link without delay and how it works but the part that I don't know how to do is add my tone because technically I don't think it's a normal tone code, if you didn't look it uses i think about 1300 hundred tones but just goes up and down at around 1 millisecond so I don't know how the Millis() works with tones

millis() just gives you the time. The blink without delay style is a way of figuring out how long it has been since you did something to figure out if it is time to do something. It doesn't matter what the thing you want to do is. It works exactly the same to turn an LED on and off or a tone on and off or a relay on and off or to send a line out on Serial every so often or to do a calculation every so often or anything else you could possibly dream up. The key concept is to note the starting time for an action and compare to the current time to figure out how long it has been.

tone(speakerpin, freq, time);

You're using the blocking version of tone. This is just like using a delay.

So I understand that I have to get rid of the delays and I think I would know how to add the millis but I think the thing that i was running into before was that I'm not sure how to get rid the the tone time so it sounds like it's supposed to, I get how to use the millis with LEDs I'm still a little confused on how to keep the tone going and the sway between the tone change and add the millis

https://www.arduino.cc/en/reference/tone

See the two argument version? That just turns a tone on. Then there is a noTone() function to turn it off again. Use it just like the led in the BWoD example.

Ok so setup my for tone code like the led in blink w/o delay and add a no tone, so if I do that will I not need to add a delay between the tones

Was that a question? If you do it right, you will never ever ever "need" a delay anywhere in any Arduino code.

I don't understand that if I get rid the delay after my tone code how will the arduino know how quickly to play each tone

Because you are checking against the time returned by millis to decide when to call noTone and turn it off.

Ok I will try it in the morning and update u on if it works

So I finished the LED code but for the blink without delay with tones I’ve haven’t found the way to make my tone of
for(freq = 150; freq < 1800; freq += 1)
{
tone(speakerpin, freq, time);
}
for(freq = 1800; freq > 150; freq -= 1)
{
tone(speakerpin, freq, time);
}
I’m not sure where to add this line, I tried setting up 2 toneStates where i put the freq and then but I don’t know what to add to the if and else statements

Use_sketch_for_project.ino (1.76 KB)

for loops block. You're not going to use a for loop.

You can't write this so that one section in loop plays the whole thing all the way through. Code it so that on each pass of loop it checks to see if it is time to play the next tone and if so play it and if not get out and let something else happen.

So would I format the code so it's basically like the LED one so I add a tone state and an If or else and get rid of the for loop