const int buttonPin = 9; // Pin connected to the button
int variable1 = 100;
int variable2 = 200;
int Vol;
bool setVariable1 = true;
void setup() {
// Initialize the button pin as an input
pinMode(buttonPin, INPUT_PULLUP);
// Initialize Serial communication
Serial.begin(9600);
// Set Vol to the initial value of variable1
Vol = variable1;
}
void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Debounce delay
delay(50);
if (digitalRead(buttonPin) == LOW) {
// Toggle between variable1 and variable2
setVariable1 = !setVariable1;
// Set Vol to the appropriate variable value
if (setVariable1) {
Vol = variable1;
} else {
Vol = variable2;
}
// Wait for the button to be released
while (digitalRead(buttonPin) == LOW);
}
}
// Print the value of Vol
Serial.print("Vol: ");
Serial.println(Vol);
// Small delay to avoid flooding the Serial Monitor
delay(500);
}