int potPin = A0; // Replace A0 with the analog pin you connected the potentiometer to
int buzzerPin = 13; // Replace 13 with the digital pin you connected the buzzer to
int potValue;
int frequency;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
potValue = analogRead(potPin);
frequency = map(potValue, 0, 1023, 200, 2000); // Map the potentiometer value to a frequency between 200 and 2000 Hz
if (potValue == 0) {
noTone(buzzerPin); // Stop playing the tone if the potentiometer is at minimum voltage
} else if (potValue == 1023) {
tone(buzzerPin, frequency, 100); // Play the tone for a short duration if the potentiometer is at maximum voltage
} else if (potValue < 512) {
tone(buzzerPin, frequency, map(potValue, 0, 511, 500, 2000)); // Map the potentiometer value to a duration between 500ms and 2000ms, and play the tone
} else {
tone(buzzerPin, frequency, map(potValue, 512, 1023, 500, 2000)); // Map the potentiometer value to a duration between 500ms and 2000ms, and play the tone
}
delay(10);
}