I am new i Arduino. I need to know how kan use mills() when i use tone/speaker . I have used delay and this is working but I need to do this whit-out delay. The condition is that the speaker has to sound 3 times (beep, beep, beep ) and then it does not have to sound more.
I am new i Arduino. I need to know how kan use mills() when i use tone/speaker . I have used delay and this is working but I need to do this whit-out delay. The condition is that the speaker has to sound 3 times (beep, beep, beep ) and then it does not have to sound more.
const byte TONE_PIN = 4;
const int ALARM_BEEP_1 = 4186;
const int ALARM_BEEP_2 = 4699;
const int ALARM_TONE_LENGTH = 200;
const int ALARM_TONE_PAUSE = 800;
const int ALARM_TONE_REPEAT = 6;
void setup()
{
}
void loop()
{
alarmSound();
}
void alarmSound() {
static unsigned long next = millis();
static byte count = 0;
if (millis() > next) {
next += ALARM_TONE_LENGTH;
count++;
if (count == ALARM_TONE_REPEAT) {
next += ALARM_TONE_PAUSE;
count = 0;
}
tone(TONE_PIN, (count % 2) ? ALARM_BEEP_1 : ALARM_BEEP_2, ALARM_TONE_LENGTH);
}
}