Help with calling a function to beep a buzzer for 200ms

I wrote a code that has multiple voids and in one that reads button input, when the button is pressed i want that the buzzer beeps for 200ms, but i don't want to use delay because it stops the uc, so i made a idk exactly what's called but a function that switches the state every 200ms and returns value which i then write to buzzer pin, but i'm not sure how to make it so the function only executes when a button is pressed

Would really appreciate some tips

int buzzer()
{
    currentMillisBuzzer = millis();

    if ((currentMillisBuzzer - previousMillisBuzzer) >= intervalBuzzer)
    {
        previousMillisBuzzer = currentMillisBuzzer;
        if (buzzer_state == LOW)
        {
            buzzer_state = HIGH;
        }
        else
        {
            buzzer_state = LOW;
        }
    }
    return buzzer_state;
}
on_off_button_state = digitalRead(ON_OFF); // reads button state and saves it in a variable

    if (on_off_button_state == 1) // if button on_off is pressed
    {
        on_off_count++;
        digitalWrite(BUZZER, buzzer_state);
        Serial.print("ON/OFF BUTTON PRESSED!");

        if (on_off_count == 1 && on_off_button_state == 1)
        {
            on_off_count = 0;
        }
    }

pin variables are declared in a separate file

They're called "function"s, not "void"s.

If you need to not use the delay function, check out the blink without delay example in the IDE.

You should know by now to post ALL of your code.

The call of the void.

I'm certain I've seen this concept completely coded in other posts on this forum. OP, have you spent time searching for it here? You have a low recent read time.

fill the void nao!

The whole code is pretty long >800lines and is split into multiple files

I changed the code to

 set_button_state = digitalRead(SET); // reads button state and saves it in a variable

    if (set_button_state == 1) // if button set is pressed
    {
        set_count++;
        Serial.print("SET BUTTON PRESSED!");
        buzzer();

        if (set_count == 1 && set_button_state == 1)
        {
            set_count = 0;
        }
    }
void buzzer()
{
    currentMillisBuzzer = millis();

    if ((currentMillisBuzzer - previousMillisBuzzer) >= intervalBuzzer)
    {
        previousMillisBuzzer = currentMillisBuzzer;
        if (buzzer_state == LOW)
        {
            buzzer_state = HIGH;
        }
        else
        {
            buzzer_state = LOW;
        }
        digitalWrite(BUZZER, buzzer_state);
    }
}

Compiler gives me buzzer was not declared in this scope error
Any idea what's the problem?

You didn't declare buzzer somewhere in the code you didn't post.
Or you didn't read the error message correctly

But where should i declare it?
If i declare it in void loop it will just constantly be executed

That one is easy to answer.

You can declare a function (provide a function prototype) in another function but you can't define a function in another function.

Ok, gonna do some more research on this

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.