I'm trying to use the adruino uno to control a timer,
Basically, I am trying to control a relay. On 3 minutes, off 30 minutes. To continue this cycle indefinitely.
With my code, It works at first( the first cycle) and then randomly it switches itself to 3 minutes off and 30 minutes on, and then switches back. Sometimes it doesn't even come on at all.
Keep in mind, i am a noob with this, I have worked on it for days with trying to learn the proper way to code this. Realistically this is my only need right now with the adruino. ( well the ability to change the on/off times accordingly)
Here is what i'm using, I found it somewhere on one of the adruino forum posts.
int ledPin = 7;
int ledState = LOW;
unsigned long previousMillis = 0;
long OnTime = 120000;
long OffTime = 1800000;
void setup()
{
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis;
digitalWrite(ledPin, ledState);
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis;
digitalWrite(ledPin, ledState);
}
}