I am using a relay on my Arduino Uno and i want it to be on LOW for a calculated interval of time, let's say ftime=7 seconds, and after that i need it to be set as HIGH (no power through relay). My code below makes no changes to the relay state. How would that be possible? I tried to use "delay()" too (which i know that is generally a bad idea), but had no success.
unsigned int interval = ftime*1000; //ftime in ms
do
{
digitalWrite(Relay1, LOW);
}
while(timeElapsed < interval);
digitalWrite(Relay1, HIGH);
Also, if i try without:
digitalWrite(Relay1, HIGH);
relay is LOW (motor running) but it never returns to HIGH obviously.
timeElapsed
is updated. But according to your functional description, why shouldn't it work to pullRelay1
low, wait forinterval
milliseconds, then pull it high again? Code is likedigitalWrite(Relay1, LOW); delay(interval); digitalWrite(Relay1, HIGH);
.delay()
is to be avoided because, while it is delaying, the Arduino cannot do any useful work (other than interrupts). In particular, if it has any kind of user interface, it will not respond to user input while delaying. However, if your Arduino really has nothing else to do, thendelay()
is fine, and it's the simplest solution.