const int relayPin = 7;       // Relay control pin
const int potPin = A0;        // Potentiometer pin
const int buttonPin = 2;      // Button pin (using pin 2 for interrupt)
const unsigned long interval = 3600000;  // 1 hour in milliseconds
unsigned long previousMillis = 0;
volatile bool triggerRelay = false; // Flag to indicate relay should be triggered

void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);  // Ensure relay is off

  pinMode(buttonPin, INPUT_PULLUP); // Configure button pin as input with pull-up resistor
  attachInterrupt(digitalPinToInterrupt(buttonPin), triggerRelayFunction, FALLING); // Set up interrupt on falling edge

  Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}

void loop() {
  unsigned long currentMillis = millis();
  
  // Check if 1 hour has passed or if the button was pressed
  if (currentMillis - previousMillis >= interval || triggerRelay) {
    if (triggerRelay) {
      triggerRelay = false; // Reset the flag after triggering
    } else {
      previousMillis = currentMillis; // Reset the timer
    }

    // Read the potentiometer value (0-1023) and map it to 1-30 seconds
    int potValue = analogRead(potPin);
    int delayTime = map(potValue, 0, 1023, 1000, 30000); // Map to 1000ms (1s) to 30000ms (30s)
    int delaySeconds = delayTime / 1000; // Convert to seconds

    // Print potentiometer setting and minutes since last activation
    Serial.print("Potentiometer set to: ");
    Serial.print(delaySeconds);
    Serial.println(" seconds");

    unsigned long minutesSinceLastActivation = (currentMillis - previousMillis) / 60000; // Convert milliseconds to minutes
    Serial.print("Minutes since last relay activation: ");
    Serial.println(minutesSinceLastActivation);

    // Activate the relay
    digitalWrite(relayPin, HIGH);
    delay(delayTime);              // Keep the relay activated for the mapped time
    digitalWrite(relayPin, LOW);   // Deactivate the relay
  }
}

// Interrupt function to set the triggerRelay flag
void triggerRelayFunction() {
  triggerRelay = true; // Set flag to true to trigger relay
}
NOCOMNCVCCGNDINLED1PWRRelay Module