const int buttonPin = A2;
const int buzzerPin = 10;
int toneFrequency = 100; // in Hz
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // button input, using internal pull-up resistor
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Button is active LOW due to INPUT_PULLUP
if (digitalRead(buttonPin) == LOW) {
toneFrequency++;
tone(buzzerPin, toneFrequency);
Serial.println(toneFrequency);
if(toneFrequency == 0) {
toneFrequency = 1;
}
} else {;
tone(buzzerPin, toneFrequency);
toneFrequency--;
Serial.println(toneFrequency);
if(toneFrequency == 0) {
toneFrequency = 1;
}
}
}