#define BUTTON_PIN D2 // Push button pin
#define POT_PIN A0 // Potentiometer pin
#define LED_PIN D7 // LED pin
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button as input with pull-up
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize Serial communication
}
void loop() {
// Read the button state (LOW when pressed)
bool buttonPressed = digitalRead(BUTTON_PIN) == LOW;
// Read the potentiometer value
int potValue = analogRead(POT_PIN);
// Calculate the potentiometer percentage
float potPercentage = (potValue / 1023.0) * 100.0; // 0 to 100%
// Check if the potentiometer is at least halfway
bool potHalfway = potValue >= 512; // 512 is approximately 50% of 1023
// Turn on the LED if the button is pressed and the potentiometer is halfway
if (buttonPressed && potHalfway) {
digitalWrite(LED_PIN, HIGH); // Turn LED ON
} else {
digitalWrite(LED_PIN, LOW); // Turn LED OFF
}
// Print button status and potentiometer percentage
Serial.print("Button: ");
Serial.print(buttonPressed ? "ON" : "OFF");
Serial.print(" | Potentiometer: ");
Serial.print(potPercentage);
Serial.println("%");
delay(100); // Update every 100 milliseconds
}