Web interface (relay control) and a timer.

Repost as internet connection was lost.

I've got a Mega with an Ethernet Shield running a basic website to turn on and off a relay.

The relay is to turn on for 15 minutes then switch off.

Unfortunately the delay pauses the website responding so I can not override the relay. Is there another way to have the delay run while allowing the website to stay up?

void relayTimeOn() { * digitalWrite(RELAY1,HIGH);* * Serial.println("");* * Serial.println("Relay ON via Web");* * Serial.println("");* * delay(900000);* * digitalWrite(RELAY1,LOW);*

}

Yes, don't use delay. Use Blink without delay style program instead. See the example in the IDE under Examples:Digital

For some reason I've bungled it up and this doesn't work: Any ideas?

const int ledOnboardPin =  LED_BUILTIN;
int ledState = LOW; 
unsigned long previousMillis = 0;
const long ledInterval = 900000;

void setup {
  pinMode(ledOnboardPin, OUTPUT);
}

void relayTimeOn() {

               digitalWrite(RELAY1,HIGH);
               Serial.println("");
               Serial.println("Relay ON via Web");
               Serial.println("");


     unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= ledInterval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledOnboardPin, ledState);
  }
               digitalWrite(RELAY1,LOW); 
}

For some reason I've bungled it up and this doesn't work: Any ideas?

THe usual one, when people trot out the incredibly lame "it doesn't work" mantra. The code does something. You expect it to do something. Tell us what each thing is.

Before you post code again, use Tools + Auto Format, so your code doesn't look like it was typed by a drunken monkey. Put EVERY { and EVERY } on lines by themselves.

True - must've been quite tired when I posted it. I used an example somewhere to try to use blink rather than delay (since delay holds up all processes till completion).

Effectively I need a function that will turn on the relay for 15 minutes (digitalWrite(RELAY1,HIGH); then at the end of 15 minutes turn the relay off (digitalWrite(RELAY1,LOW);) I notice my code didn't even include a timer period (i.e. 900000)

Effectively I need a function that will turn on the relay for 15 minutes

That way of thinking is what is leading you to have problems.

"I need to turn a relay off 15 minutes after I turn it on". See how that statement contains NOTHING that says how it should be done?

Now, think about how you might explain to a 5 year old how to accomplish that simple task.

When I tell you turn the relay on, turn it on AND write down the time.

Periodically, see if the relay is on, and has been on for 15 minutes. If that is the case, turn the relay off.

Broken out like that, you can see that there are two completely independent activities that need to be performed.

Now, you take a stab at implementing them, using the blink without delay example for inspiration.

I blindly missed the rest of the code to make the function make sense.

Hopefully the extra code makes more sense. The example at

I blindly missed the rest of the code to make the function make sense.

Hopefully the extra code added to this topic makes it more obvious.

The example at https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay is for a loop rather than a single run. I know I'm way off track and it should make simple sense as to how it all works but for some reason can't get my head around it. :/

The example at https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay is for a loop rather than a single run.

It shows how to use millis() to record when an event happens, and how to use the difference between now and then to determine if it is time to do something.

Show your code, and explain where you record when the relay is turned on.

Explain where you, independently get now, and compare it to then to determine if it is time to turn the relay off.