#define vrPin 14 // VR connected to analog pin 14 on ESP32
#define relayPin 17 // Relay control connected to digital pin 17
#define buzzerPin 23 // Buzzer connected to digital pin 23
int vrValue = 0; // Variable to store VR reading
int relayState = LOW; // Current state of relay
void setup() {
// Set the pin modes for the relay and buzzer
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(115200);
}
void loop() {
// Read the VR value (0 to 1023 on analog pins)
vrValue = analogRead(vrPin);
// Print the VR value for debugging
Serial.print("VR Value: ");
Serial.println(vrValue);
// Control the relay state based on the VR value
if (vrValue > 50 && relayState == LOW) { // Turn on relay if VR is above threshold
digitalWrite(relayPin, HIGH); // Turn on the relay (LED powered on)
relayState = HIGH; // Save the relay state
}
else if (vrValue <= 50 && relayState == HIGH) { // Turn off relay if VR is below threshold
digitalWrite(relayPin, LOW); // Turn off the relay (LED powered off)
relayState = LOW; // Save the relay state
}
// Buzzer alert at max or min VR positions
if (vrValue < 50 || vrValue > 1000) { // Thresholds for min and max VR positions
digitalWrite(buzzerPin, HIGH); // Activate buzzer
delay(100); // Brief alert duration
digitalWrite(buzzerPin, LOW); // Deactivate buzzer
}
delay(100); // Small delay for stability
}