Plan
Board
Made with Fritzing
Code
int pinSpeaker = 6;
int pinPot = 0;
int pinButton = 9;
int stateButton = 0;
int valPot = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinButton, INPUT);
}
void loop()
{
valPot = analogRead(pinPot);
valPot = map(valPot, 0, 1023, 0, 4000);
Serial.println(valPot);
stateButton = digitalRead(pinButton);
if (stateButton == HIGH) {
tone(pinSpeaker, valPot);
}
else{
noTone(pinSpeaker);
}
delay(10);
}
Download
Fritzing: buzzer_pot_button.fzz