Buzzer works for the first minute, then starts clicking

I have a program written to control a buzzer at 3 different frequencies (500hz, 1000hz, 1500hz) based off of where a pot is set. the program runs fine (arduino nano clone... elegoo) and the buzzer works fine for the first aprox 30-60 seconds, then it starts clicking at what sounds like the same tone, and intermittenly beeping on the 1500hz. is it a bad buzzer? or is it the arduino current supply? any help is appreciated thank you! new to this forum btw.

edit: just realized the title said "Buzzer is works for..." lol corrected

There isn't enough information to even begin to give you an answer.

Read the following, to get a clue why: http://forum.arduino.cc/index.php?topic=149015.0

Cookieman101: I have a program written to control a buzzer at 3 different frequencies (500hz, 1000hz, 1500hz) based off of where a pot is set. the program runs fine (arduino nano clone... elegoo) and the buzzer works fine for the first aprox 30-60 seconds, then it starts clicking at what sounds like the same tone, and intermittenly beeping on the 1500hz. is it a bad buzzer? or is it the arduino current supply? any help is appreciated thank you! new to this forum btw.

Perhaps your program is buggy? You haven't posted it so we can't help.

Apologies, I should’ve fully read the guide rather than skimming. Here’s some more info:

I had this problem when powered off a (self powered) USB hub, and also from a 12 v wall wart wired to an lm7805, so idk if it’s the power source or not

Here’s the code, I know it could be cleaner but this is my first REAL C++ program that actually does anything useful to speak of haha!

#include “Servo.h”
using namespace std;

int potPin= A0;
int readValue;
float writeValue;
Servo esc;
int buzzerpin = 5;
int buzzerontime = 100;
unsigned long currentmillisb = millis();
unsigned long offsetmillis;
int previousmillisb = 0;
int a = 1;

//Setting up functions START

void reads() {
readValue = analogRead(potPin);
//writeValue = (pwmmax/potmax) * (readValue) / (pwmmax) * (12.75) + (12.75);
//writeValue = (pwmmax/potmax) * (readValue);
writeValue = map(readValue, 0, 1020, 60, 150);
}
void writes() {
esc.write(writeValue);
}

void main2() {
}

//Setting up functions END

void setup() {
pinMode(potPin, INPUT);
esc.attach(9);
writeValue = 60;
esc.write(writeValue);
delay(5000);
Serial.begin(9600);
Serial.println(millis());
}

void loop() {
reads();
writes();

//START OF LOW BUZZER CODE…

if (writeValue == 60) { //if pot is low
tone(buzzerpin, 500); //set buzzer ON
Serial.println(“Set 500 ON”);
previousmillisb = millis(); //write down time
Serial.print(previousmillisb);
while(a == 1) { //loop
Serial.println(“im in the 60 while loop”);
if (millis() - previousmillisb >= buzzerontime) { //waits for time to pass
noTone(buzzerpin); //sets buzzer OFF
Serial.println(“set 500 OFF”);
a = 0; //at the end of the loop it will break
}
reads();
writes();
}
a=1;
while(writeValue == 60) { //loops reads and writes until pot is moved
reads();
writes();
Serial.println(“Reading and writing”);
}
}
//END OF LOW BUZZER CODE…
//START OF MED BUZZER CODE…

if (writeValue == 105) { //if pot is med
tone(buzzerpin, 1000); //set buzzer ON
Serial.println(“Set 1000 ON”);
previousmillisb = millis(); //write down time
Serial.print(previousmillisb);
while(a == 1) { //loop
Serial.println(“im in the 105 while loop”);
if (millis() - previousmillisb >= buzzerontime) { //waits for time to pass
noTone(buzzerpin); //sets buzzer OFF
Serial.println(“set 1000 OFF”);
a = 0; //at the end of the loop it will break
}
reads();
writes();
}
a=1;
while(writeValue >= 100 && writeValue <= 110) { //loops reads and writes until pot is moved
reads();
writes();
Serial.println(“Reading and writing”);
}
}

//END OF MED BUZZER CODE…
//START OF HIGH BUZZER CODE…

if (writeValue == 150) { //if pot is high
tone(buzzerpin, 1500); //set buzzer ON
Serial.println(“Set 1500 ON”);
previousmillisb = millis(); //write down time
Serial.print(previousmillisb);
while(a == 1) { //loop
Serial.println(“im in the 1500 while loop”);
if (millis() - previousmillisb >= buzzerontime) { //waits for time to pass
noTone(buzzerpin); //sets buzzer OFF
Serial.println(“set it OFF”);
a = 0; //at the end of the loop it will break
}
reads();
writes();
}
a=1;
while(writeValue == 150) { //loops reads and writes until pot is moved
reads();
writes();
Serial.println(“Reading and writing”);
}
}

//END OF HIGH BUZZER CODE…

}

Apologies, I should've fully read the guide rather than skimming.

Pity you did not read the bit about how to post your code. :(

We also need a schematic. And a link to the buzzer you used.

previousmillisb needs to be an unsigned long as does buzzerontime

if (writeValue == 60 ) - using an exact value from a pot is bad, you can almost never hit that value and noise will take it out.

You keep overwriting the value of previousmillisb when your time out hasn't happened yet.

OK, now you need to read the instructions, then go back and modify your post using the "More --> Modify" option you will find to the bottom right of the post, to mark up the code (but it always needs to be the complete code) as such so we can examine it conveniently and accurately. Please do not post a ".ino" file as an attachment - that would mean that you are expecting people to actually load it to their IDE to look at it and that is extra unnecessary labour. In fact, attachments do not always show properly on different operating systems.

If you do not mark it up as code, whatever code you do post could well be garbled and is certainly anything but easy to read. Yours seems to have survived so far but I didn't look too closely.

Note: Also mark up any data - as a separate section - in the same way. This includes error output that you get from the IDE.

And - before you post any code, use "Auto Format" in the Tools menu of the IDE to properly present the code. You can then use the "copy for forum" function in order to paste it into a message with the "code" tags already provided.

Try and avoid unnecessary white space (blank lines). You should only use these to separate functional blocks of code.

tone() expects a passive (piezo) speaker, not an active buzzer.

Try if you have an active or passive 'buzzer' by connecting the buzzer between 5volt and ground, If the 'buzzer' beeps by itself, then you can't use it with tone(). Leo..

Hey im really sorry about not properly formatting my post… and not reading the rules… however my question has been answered! Grumpy_Mike thank you, it was the need for unsigned long on previousmillisb! i totally didn’t realize how quickly millis() actually adds up. anyways thanks everyone and sorry.

So if you plan to use this resource in future, do what was suggested and fix your previous posts!

With a pot connected to an analog input, your code should always be checking ranges:

void loop()
{
  reads();
  writes();
  if (0 <= readValue && readValue < 1024/3)
    xxxxx ();
  if (1024/3 <= readValue && readValue < 1024*2/3)
    yyyy ();
  if (1024*2/3 <= readValue && readValue < 1024)
    zzzz (); 
}

ie use individual functions for each action (with names that make sense, not xxxxx, yyyy etc).
Keep loop() nice and simple if you can, just the checks and calls, not the business logic of the sketch.

Compare with the input values, so that when you rework how writeValues is calculated it doesn’t
break the input code. Separation of concerns is important.

You probably need to keep track of state too, so that you don’t repeatedly trigger actions when the
conditions haven’t changed from last time.