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?
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)
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.
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.