int selectionBtnPin = 25; // Pin for the selection button
int potPin = 33; // Pin for the potentiometer
int selectionBtnVal = 0; // Current button state
int lastBtnVal = 0; // Last button state to detect state change
int selectedMode = 0; // Currently selected mode
int potValue = 0; // Current potentiometer value
int modes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // Array to store mode states (ON/OFF)
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(selectionBtnPin, INPUT_PULLUP); // Set the button pin as input with pull-up
}
void loop() {
// Read the button state
selectionBtnVal = digitalRead(selectionBtnPin);
// If button state changes from HIGH to LOW (button press)
if (selectionBtnVal == LOW && lastBtnVal == HIGH) {
modes[selectedMode] = !modes[selectedMode]; // Toggle the selected mode
Serial.print("Mode ");
Serial.print(selectedMode + 1); // Mode number (1 to 8)
Serial.print(" is now ");
Serial.println(modes[selectedMode] ? "ON" : "OFF");
delay(2000); // Debounce delay
}
// Update the last button state
lastBtnVal = selectionBtnVal;
// Read the potentiometer value
potValue = analogRead(potPin);
// Map the potentiometer value to the mode number (0 to 7)
selectedMode = map(potValue, 0, 4095, 0, 7);
// Print the selected mode number
Serial.print("Selected Mode: ");
Serial.println(selectedMode + 1); // Mode number (1 to 8)
delay(100); // Delay to reduce serial output rate
}