I would like to use an Arduino Uno board to control multiple (2 to 4) LEDs. I can set them up to blink independently at the desired rates, but I would like these blink rates to vary randomly on one LED and for the blink to be randomly given to one over the other.
Essentially, I have an LED mounted to the left (pin 10) and an LED mounted to the right (pin 11). 1 LED should always 'blink' at a perceived constant rate of 20 kHz and this should be randomly assigned to either the right or left. The second LED (on either the left or right, depending on the first assignment) should blink at randomly varying intervals from 10 - 100 Hz in 5 Hz increments.
The following code controls two LEDs independently at 10 Hz and 20 kHz but I do not know how to make the random assignments to each pin or randomly vary one LED's rate.
unsigned long previousMillisLED10 = 0; //millis() returns an unsigned long
unsigned long previousMillisLED11 = 0; //millis() returns an unsigned long
unsigned long intervalLED10 = 100; //time needed to wait
unsigned long intervalLED11 = 0.00005;
boolean LED10State = false; //state variable for LED
boolean LED11State = false;
int ledPin1 = 11;
int ledPin2 = 10;
void setup() {
// put your setup code here, to run once
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis(); //grab current time
//toggle LED10
if ((unsigned long)(currentMillis-previousMillisLED10)>= intervalLED10){
LED10State =!LED10State; //toggles the state
digitalWrite(ledPin1, LED10State); //sets the LED based on ledState
//save the 'current' time to pin 10's previousMillis
previousMillisLED10 = currentMillis;
}
//toggle LED11
if ((unsigned long)(currentMillis-previousMillisLED11)>= intervalLED11){
LED11State =!LED11State; //toggles the state
digitalWrite(ledPin2, LED11State); //sets the LED based on ledState
//save the 'current' time to pin 11's previousMillis
previousMillisLED11 = currentMillis;
}
}