Hello shauntherailroadguy
Take a view to this small and simple LED timer switch.
The configuration is made here:
//------snip
// configure the Led Timer System here
constexpr uint8_t OutputPins[] {9, 10, 11, 12};
constexpr uint32_t RandomsTimes[sizeof(OutputPins)][2]
{
{1000, 2000}, // min/max random time @ led 1
{2000, 3000}, // min/max random time @ led 2
{3000, 4000}, // min/max random time @ led 3
{4000, 5000}, // min/max random time @ led 4
};
//-----------------------------------------
// -- snip
and the complete sketch:
//https://forum.arduino.cc/t/using-arduino-to-light-leds-randomly/1357654/1
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "Using Arduino to light LED's randomly"
#define NotesOnRelease "Arduino MEGA tested"
// make names
enum TimerEvent {NotExpired, Expired};
enum TimerControl {Halt, Run};
enum OnOff {Off, On};
enum MinMax {Min, Max};
// make variables
uint32_t currentMillis = millis();
//-----------------------------------------
// configure the Led Timer System here
constexpr uint8_t OutputPins[] {9, 10, 11, 12};
constexpr uint32_t RandomsTimes[sizeof(OutputPins)][2]
{
{1000, 2000}, // min/max random time @ led 1
{2000, 3000}, // min/max random time @ led 2
{3000, 4000}, // min/max random time @ led 3
{4000, 5000}, // min/max random time @ led 4
};
//-----------------------------------------
// make structures
//-----------------------------------------
struct TIMER
{
uint32_t interval;
uint16_t control;
uint32_t now;
uint8_t expired(uint32_t currentMillis)
{
uint8_t timerEvent = currentMillis - now >= interval and control;
if (timerEvent == Expired) now = currentMillis;
return timerEvent;
}
};
//-----------------------------------------
struct LEDTIMERSWITCH
{
uint8_t pin;
uint32_t tMin;
uint32_t tMax;
TIMER dimmer;
void make(uint8_t pin_, uint32_t tMin_, uint32_t tMax_)
{
pin = pin_;
tMin = tMin_;
tMax = tMax_;
Serial.print("LED test @ pin "), Serial.print(pin);
pinMode (pin, OUTPUT);
digitalWrite(pin, On);
delay(1000);
digitalWrite(pin, Off);
delay(1000);
Serial.println(" done");
dimmer.control = Run;
dimmer.interval = random(tMin, tMax);
}
void run ( uint32_t currentMillis)
{
if (dimmer.expired(currentMillis) == Expired)
{
dimmer.interval = random(tMin, tMax);
digitalWrite(pin, digitalRead(pin) ? Off : On);
}
}
};
LEDTIMERSWITCH ledTimerSwitches[sizeof(OutputPins)];
//-----------------------------------------
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
static bool setUp = false;
if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
Serial.begin(115200);
for (uint8_t n = 0; n < 32; n++) Serial.println("");
Serial.print("Source: "), Serial.println(__FILE__);
Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
uint8_t index = 0;
for (auto &ledTimerSwitch : ledTimerSwitches)
{
ledTimerSwitch.make(OutputPins[index], RandomsTimes[index][Min], RandomsTimes[index][Max]);
index++;
}
delay(2000);
Serial.println(" =-> and off we go\n");
}
void loop()
{
currentMillis = millis();
heartBeat(LED_BUILTIN, currentMillis);
for (auto &ledTimerSwitch : ledTimerSwitches) ledTimerSwitch.run(currentMillis);
}
Have a nice day and enjoy coding in C++.