#define NUM_LED 3
//time translating//
#define sec * 1000
#define min * 60000
#define ms * 1
uint16_t millisOffset;
unsigned long currentMillis = millis();
//leds structure//
struct leds
{
unsigned long startTime; // The time this led is first active
unsigned long stopTime; // The time this led becomes inactive
unsigned long minPulse; // The minimum time the next pulse is generated
unsigned long maxPulse; // The maximum time the next pulse is generated
unsigned long nextPulseStart; // The time of the next pulse starts
unsigned long nextPulseStop; // The time of the next pulse stops
unsigned long pulseLength; // The on time of the pulse
uint8_t pin; // led GPIO pin number
};
leds myLeds[NUM_LED] = {0 min, 3 min, 3 sec, 12 sec, 0, 0, 100 ms, 2, //led num 0
1 min, 4 min, 3 sec, 12 sec, 0, 0, 100 ms, 3,//led num 1
2 min, 5 min, 3 sec, 12 sec, 0, 0, 100 ms, 4, //led num 2
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
for (uint8_t i = 0; i < NUM_LED; i++)
{
pinMode(myLeds[i].pin, OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
startSequence();
}
void startSequence()
{
currentMillis = millis() - millisOffset;
for (uint8_t x = 0; x < NUM_LED; x++)
{
// Is this solenoid active
if (myLeds[x].startTime < currentMillis && myLeds[x].stopTime > currentMillis)
{
// Do we need to calculate a next pulse time?
if (myLeds[x].nextPulseStop < currentMillis)
{
// Turn off solenoid
digitalWrite(myLeds[x].pin, LOW);
// Calculate nextPulseStart & nextPulseStop
long startOffset =random(myLeds[x].minPulse, myLeds[x].maxPulse);
myLeds[x].nextPulseStart = startOffset + currentMillis;
myLeds[x].nextPulseStop = myLeds[x].nextPulseStart + myLeds[x].pulseLength;
}
// Are we currently in the middle of a pulse?
if (myLeds[x].nextPulseStart < currentMillis &&
myLeds[x].nextPulseStop > currentMillis)
{
//turn on solenoid
digitalWrite(myLeds[x].pin, HIGH);
}
}
if (currentMillis > myLeds[x].stopTime)
{
digitalWrite(myLeds[x].pin, LOW);
}
}
}