Hi all!
I am trying to control relays by using the millis() function. After a preset time period a relay will fire. I am using two currently. The program works fine for about 30 seconds, then stops. Any ideas? Here is the code:
int Pins = 13;
int relay1 = 250;
int relay2 = 750;
int relayState1 = LOW;
int relayState2 = LOW;
long previousMillis1 = 0;
long previousMillis2 = 0;
unsigned long currentMillis = 0;
void setup()
{
for (int i=1; i < Pins; i++)
{
pinMode (i, OUTPUT);
}
}
void loop()
{
//digitalWrite(1, HIGH);
currentMillis = millis();
if (currentMillis - previousMillis1 > relay1)
{
previousMillis1 = currentMillis;
Pin1(currentMillis);
//previousMillis1 = x;
}
if (currentMillis - previousMillis2 > relay2)
{
previousMillis2 = currentMillis;
Pin2(currentMillis);
}
}
void Pin1(int x)
{
previousMillis1 = x;
if (relayState1 == LOW)
//{
relayState1 = HIGH;
//}
else
//{
relayState1 = LOW;
//}
digitalWrite(1, relayState1);
}
void Pin2(int y)
{
previousMillis2 = y;
if (relayState2 == LOW)
//{
relayState2 = HIGH;
//}
else
//{
relayState2 = LOW;
//}
digitalWrite(2, relayState2);
}
Thanks in advance.
Tom