Go Down

Topic: Alarm buzzer, use delay or cycles? (Read 2 times) previous topic - next topic

Boopidoo

Jan 04, 2013, 05:51 pm Last Edit: Jan 04, 2013, 08:15 pm by Boopidoo Reason: 1
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.
Code: [Select]
void alarm() {
 tone(5,840);                        
 digitalWrite(screen_backlight, HIGH);
 delay(500);
 noTone(5);
 digitalWrite(screen_backlight, LOW);
 delay(500);
}


Code example using cycles.
Code: [Select]
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;
 }
}

marco_c

Quote
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.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Boopidoo

#2
Jan 04, 2013, 11:04 pm Last Edit: Jan 04, 2013, 11:08 pm by Boopidoo Reason: 1
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.

marco_c

#3
Jan 04, 2013, 11:41 pm Last Edit: Jan 05, 2013, 03:36 am by marco_c Reason: 1
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:
Code: [Select]
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.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Boopidoo

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?

Go Up