I'm trying to make a sketch where my led blinks with random delay. I have 2 functions: ledBlink() and randomBlink(). The randomBlink use as delay parameter a random number "r" generated from 1 to 499 and pass this value to the ledBlink function to make the led blink with a delay according the random number generated. But seems like that led doesn't consider the random value of blinkDelay. Any help is appreciated, thank you so much!
#include <JC_Button.h>
int ledPin = 11;
int ledState = LOW;
unsigned long blinkDelay;
unsigned long lastBlinkMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void ledBlink()
{
unsigned long currentMillis = millis();
if (currentMillis - lastBlinkMillis >= blinkDelay)
{
// save the last time you blinked the LED
lastBlinkMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState = !ledState;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
void randomBlink()
{
unsigned long r = random(500);
blinkDelay = r;
ledBlink();
}
void loop() {
randomBlink();
}
busy
flag at begining ofledBlink()
... clear it inside of theif
block ... callrandomBlink()
only if not busy