Pages: [1]   Go Down
Author Topic: Alarm buzzer, use delay or cycles?  (Read 2306 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void alarm() {
  tone(5,840);                        
  digitalWrite(screen_backlight, HIGH);
  delay(500);
  noTone(5);
  digitalWrite(screen_backlight, LOW);
  delay(500);
}

Code example using cycles.
Code:
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;
  }
}
« Last Edit: January 04, 2013, 02:15:40 pm by Boopidoo » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1257
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 04, 2013, 05:08:41 pm by Boopidoo » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1257
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: January 04, 2013, 09:36:56 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1257
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

How accurate is this ms-counter?
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1257
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 - downloaded 17 times.)
« Last Edit: January 06, 2013, 02:47:14 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Edison Member
*
Karma: 64
Posts: 2488
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: