// Define the pins for the potentiometer and speaker
int potPin = A0;
int speakerPin = 9;
void setup() {
// Set the speaker pin as an output
pinMode(speakerPin, OUTPUT);
}
void loop() {
// Read the value of the potentiometer
int potValue = analogRead(potPin);
// Map the potentiometer value (0-1023) to a range of speaker volumes (0-255)
int volume = map(potValue, 0, 1023, 0, 255);
// Set the speaker volume to the mapped value
analogWrite(speakerPin, volume);
// Add a delay to prevent rapid volume changes
delay(10);
}