relay control with sensor

alright first post here, and i’ve looked through basically everything! Maybe I’m blind maybe I’m just in dummy mode, its a toss up. What I’m trying to do is this;

-when sensor reaches certain value it closes the contacts of a relay for say 5 seconds then reopens
-regardless of sensor value i don’t want this to repeat any sooner than 15 minutes (if possible)

thats pretty much it! seems simple but regardless of what i try i can only get the contacts to stay closed (which won’t work in my application), or it just continues to open and close the relay (like a mad man) until the value is below the set point. Now i’m sure this is going to be a bang my head on the wall when i figure it out situation I’m just stuck! heres what i have currently (this obviously just closes the contacts when the value is reached.

const int analogInPin = A0;
const int ledPin = 2;

int sensorValue = 0;

void setup(){
pinMode(ledPin, OUTPUT);

void loop() {
sensorValue = analogRead(analogInPin);
if (sensorValue >= 120)
digitalWrite(ledPin, HIGH);
if (sensorValue <= 120)
digitalWrite(ledPin, LOW);

Serial.print(“Level =”);


The millis() function allows you to get the time that the relays closed as milliseconds since the last board reset. You could record that value and compare with the current value it returns to determine when 15 minutes has elapsed. See the Blink Without Delay example in the IDE for some inspiration.