Arduino loop with buzzer

basically, I have my void loop in my program, and I create another loop outsit of the void loop( loop that contains a buzzer to be on for 10 second) what I want is, to put a condition for the buzzer loop that makes the buzzer stop if some condition changed in (void loop)

In void loop
each time the void loop is executed the (count) is changing, the limit is (5)

  if (count > limit){ 
  buzzer_on();
  }

so when the count is over 5, it's going to void buzzer_on() function which contains the loop
what I want is, want the buzzer to be on while compiling the void loop (other code out side the buzzer loop)


void buzzer_on(){
  
for(int x=0;x<20;x++)
    {
      digitalWrite(buzzer,HIGH);
      delay(150);//wait for 1ms
     digitalWrite(buzzer,LOW);
      delay(100);//wait for 1ms
    }
}

Check out my tutorial on How to write Timers and Delays in Arduino which has various examples starting and stopping timers.

it didn't help me a lot -

Have a look at my Multi-tasking in Arduino tutorial
You loop code sets a flag that is checked in the buzzer task.
The BasicRepeatingDelay.ino gives you a buzzer. To start it start the timer, to stop it have a flag to prevent the timer restarting.

multitaskingDiagramSmall

1 Like

yeah, I get you,
but the thing is my buzzer_on function contain a loop, and when my compiler get into the loop it's stuck there and it can get out to execute the rest of the code,
I hope you understand my problem

Something like

//https://forum.arduino.cc/t/arduino-loop-with-buzzer/868057/4

// based on  RepeatingMillisDelay.ino

// download SafeString V4.1.5+ library from the Arduino Library manager or from
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
// for the millisDelay class
#include <millisDelay.h>

int buzzerPin = 13; // or buzzer
// Pin 13 has an LED connected on most Arduino boards.
bool buzzerOn = false; // keep track of the buzzerPin state

millisDelay buzzerDelay;
unsigned long BUZZER_DELAY_MS = 125;

millisDelay countDelay;
unsigned long COUNT_DELAY_MS = 500;

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
  // initialize the digital pin as an output.
  pinMode(buzzerPin, OUTPUT);   // initialize the digital pin as an output.
  digitalWrite(buzzerPin, LOW); // turn buzzerPin off
  buzzerOn = false;

  // start delay
  countDelay.start(COUNT_DELAY_MS);
}

int buzzerCounter = 21; // > 20 so buzzer not running
void runBuzzer() {
  if (buzzerCounter >= 20) {
    buzzerDelay.stop();
    return; // stop buzzer
  } // else run buzzer
  // check if delay has timed out
  if (buzzerDelay.justFinished()) {
    buzzerDelay.restart(); //
    buzzerCounter++;
    // toggle the buzzerPin
    buzzerOn = !buzzerOn;
    if (buzzerOn) {
      digitalWrite(buzzerPin, HIGH); // turn buzzerPin on
    } else {
      digitalWrite(buzzerPin, LOW); // turn buzzerPin off
    }
  }
}

int count = 0;
int limit = 10;
void checkStartBuzzer() {
  if (countDelay.justFinished()) {
    countDelay.repeat();
    count++;
    if (count > limit) {
      Serial.println(F("start buzzer"));
      buzzerCounter = 0; // start buzzer
      buzzerDelay.start(BUZZER_DELAY_MS);
      count = 0; // repeat in 5sec
    }
  }
}

void loop() {
  checkStartBuzzer();
  runBuzzer();
}

1 Like

this is awesome bro,
i get you, and understand that I can give my loop time to finish, and during this time I can let my program execute something else,
but in my case,
the counter should continue the adding and removing,
and i have limit when i arrive that limit i want my buzzer work and also the counting continue,

Okay, you're on track but your code won't let you get there from here.

byte  alarmBeeps  = 0;  //  global variable

void alarm( )
{
    static unsigned long startMs = 0;
    static unsigned long intervalMs = 150;    // can be almost 50 days
    static byte buzz = 0;
   static byte alarmOn = 0;

    if ( alarmBeeps > 0 )
    {
        if ( millis() - startMs < intervalMs )    return;     // time is not up, do something else

        if ( alarmOn == 0 )
        {
            startMs = millis();
            alarmOn = 1;
        }
        else     startMs += intervalMs;

        buzz ^= 1;         // toggles bit 0  
        if ( buzz > 0 )    // start of sound pulse
        {
            digitalWrite(buzzer,HIGH);
            alarmBeeps--;
        }
        else
        {
            digitalWrite(buzzer,LOW);
        }
     }
    else    //  alarmBeeps == 0
    {
        digitalWrite(buzzer,LOW);
        alarmOn = 0;
        buzz = 0;
    }
}


void loop()
{
  if ( trigger condition )            alarmBeeps = 20;
  else if ( stop condition )        alarmBeeps = 0;
       
  alarm( );  // ALWAYS RUNS, Control through value in alarmBeeps.
}