i'm trying to make a sketch where my led blink with random delay. I have 2 functions: ledBlink() and randomBlink(). the second functions get as delay parameter a random number "r" generated from 1 to 499 and pass this value to the ledBlink function to made the led blink with a delay according the random number generated. But the problem is that the led is blinking really fast and seems it doesn't consider the 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