// Find the most annoying buzzer frequency.
//
// Louder in Wokwi:
// 1: Change the volume in "diagram.json"
// Louder in real life:
// 1: Put a box around it with a hole for the buzzer sound.
// 2: Use the toneAC library.
const int buzzerPin = 11;
const int tunePin = A0;
const int fineTunePin = A1;
const int extraFinePin = A2;
void setup()
{
Serial.begin(115200);
}
void loop()
{
int tune = analogRead(tunePin);
int finetune = analogRead(fineTunePin);
int extrafine = analogRead(extraFinePin);
float t1 = (float) tune / 1023.0;
float t2 = (float) finetune / 1023.0;
float t3 = (float) extrafine / 1023.0;
// Use a logaritmic scale for the first one.
// Start at 35Hz, up to 10kHz.
// The values are found by trying.
// The lowest frequence for the Arduino Uno
// with the tone() function is 31.
//
// The values are found by trying.
float f1 = pow(sqrt(10000.0) * t1, 2.0) + 35.0;
float f2 = (1000.0 * t2) - 500.0;
float f3 = (50.0 * t3) - 25.0;
int frequency = (int) (f1 + f2 + f3);
Serial.print(frequency);
Serial.print(" Hz");
if(frequency <= 31)
{
Serial.print(" !");
noTone(buzzerPin);
}
else
{
tone(buzzerPin,frequency,200);
}
Serial.println();
delay(200);
}
TUNE
FINE TUNE
EXTRA FINE