Arduino, C++, ESP, Programming
Random LED blinking – ESP / Arduino
Blinking the LED diode at random time intervals in the defined bounds. An example of use the random () function.
You can set the number of repetitions, the length of the diode lighting and the bounds for the number generator.
Can be used for example to verify the pulse counter module.
Circuit example
Code
#define LED D1 // LED pin ( in NodeMCU we use GPIO5 (D1) )
long blinkRepeats = 100; // number of the LED flashes
long ledRuntimeMs = 250; // led lighting time
long minDelay = 100; // lower bound of delay time
long maxDelay = 800; // upper bound of delay time
// -----------------------------------------------------
long randomDelayTime = 0; // random delay variable
long loopIteration = 0; // iteration counter
void setup() {
pinMode(LED, OUTPUT); // LED pin as output.
randomSeed(analogRead(0)); // shuffle the random function
}
void loop() {
if(loopIteration < blinkRepeats) {
digitalWrite(LED, HIGH); // turn the LED ON
delay(ledRuntimeMs); // defined lighting time
digitalWrite(LED, LOW); // turn the LED OFF
randomDelayTime = random(minDelay, maxDelay); // generates pseudo-random numbers in given bounds
delay(randomDelayTime); // wait random time
loopIteration++; // increment
}
}

