Alarm buzzer, use delay or cycles?

In my project I'll have an alarm buzzer that activates whenever some criterions are true. Me being an Arduino novice I've read that if I use the delay function the processor can't continue with other tasks. Is this true?

In any case I have made two exemples and I would like to know if any of these is preferred if I'd like the Arduino to continue with other tasks while the alarm is activated?

Code example using delay.

void alarm() {
  tone(5,840);                         
  digitalWrite(screen_backlight, HIGH); 
  delay(500);
  noTone(5);
  digitalWrite(screen_backlight, LOW);
  delay(500);
}

Code example using cycles.

void alarm() {
  if(warning_delay < 25) {                  
    warning_delay = warning_delay + 1;
  } else {
    digitalWrite(screen_backlight, HIGH);
    tone(5,840);
    warning_delay = warning_delay + 1;
  }
  if(warning_delay > 50) {
    digitalWrite(screen_backlight, LOW);
    noTone(5);
    warning_delay = 0;
  }
}

the delay function the processor can't continue with other tasks. Is this true?

YES

Look at the 'blink without delay' example as a way of doing things without stopping the rest of the functions from running. Your method can work but you may find that it runs through very very fast and you may not notice the flashing, etc. Relating things back to a clock or a timer will provide better control.

I already tried this and the blinking is actually not fast at all, a rough guess is about 500ms for each blink.

Unless the actual tone making isn't too resource demanding I guess it's a quite neat way to solve it without having a clock. Anyway, thanks, I won't use delay then.

As there is no timing information in the function you have supplied, the rest of the code must be calling this function with a frequency that gives you your 500ms time. What if there was different code, like:

void loop(void)
{
  alarm();
}

Timing would then be different and you need to adjust the loop constants to compensate. Worse, still, if this was in a library, you would not expect it to work differently depending on the speed of the CPU or how busy it is, right?

What I mean by a clock or timer giving you more control is that you get a consistent result no matter what code is surrounding this function and a lot less dependencies between different blocks of code. In the long run that makes for more maintainable code.

Yes I understand. In my project the Arduino will make the same operation over and over so it's not a problem. I'll only set the correct no of cycles for my given application and it will be the same since he tasks are always the same. Small variations in duration doesn't matter. It's more important that the Arduino can keep on doing other stuff in the meantime while the alarm is activated.

For my project the Arduino monitors systems and presents this information and that's basically it, when some values drop outside approved intervals the alarm is activated. And yes this function is called from the loop.

Anyway, is there a way to base duration from any kind of timer in the Arduino or do I need an external chip for this?

Use the millis() function to give you the number of milliseconds since the arduino was turned on.

The techniques is demonstrated in the blink without delay example, but basically you save a copy of the millis() to a variable and then check the current value of millis() until the time is expired. Once that happens, do what you need to do and then save the current value of millis() as the baseline for next time.

In your case you may also need to keep track of where you are in the process (eg, blinking display, displaying message, etc), if you have a sequence of actions.

Super, that's exactly what I need. I can also make a timer function from this.

How accurate is this ms-counter?

Good enough. I have not timed it but when used in multiples (500, 750, 2000) seems to be right. there is also a micros() function for microseconds if you need more accuracy, but I think that is only accurate to within 4 microseconds or so.

In case it helps to get the concept better, I have attached a small library that I wrote to handle the switches connected to the analog pin on my LCD display. The detection time and the repeat rate use this timing technique.

MD_AButton.zip (4.49 KB)

Just a note for others: "alarm buzzer" (inductive/mecanical) don't use with solid state.
Used sonalerts.

The timer will be something I wanted to use in order to keep track of mAh used as well as a separate timer to turn on/off and keep track of time in between. maybe even with a sound alert every 5min or so. When using goggles to fly the UAV I might have to use sound alerts for some functions since I won't be able to look at the display.