Go Down

Topic: Randomly Alternate LED pins (Read 1 time) previous topic - next topic

tekjones9

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.

Code: [Select]

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;
}
}

PaulRB

#1
May 22, 2019, 09:21 pm Last Edit: May 22, 2019, 09:28 pm by PaulRB
Quote
. 1 LED should always 'blink' at a perceived constant rate of 20 kHz
Perceived by what? That's far too fast for a human to perceive.

Quote
The following code controls two LEDs independently at 10 Hz and 20 kHz
Your code might do the 10Hz thing, but it won't do the 20KHz thing.

Code: [Select]
unsigned long intervalLED11 = 0.00005;
An integer, even an unsigned long integer, can't hold a fraction.

tekjones9

Perceived by what? That's far too fast for a human to perceive.
Your code might do the 10Hz thing, but it won't do the 20KHz thing.

Code: [Select]
unsigned long intervalLED11 = 0.00005;
An integer, even an unsigned long integer, can't hold a fraction.
Not for humans. And it is supposed to be perceived as constant (not blinking). Basically, any value above 100 Hz will do. My problem is the randomization, not the blinking.

UKHeliBob

Quote
I do not know how to make the random assignments to each pin or randomly vary one LED's rate.
I assume that you are aware of the random() and randomSeed() functions
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

tekjones9

I assume that you are aware of the random() and randomSeed() functions
I tried using the random function and got errors. I tried:

Code: [Select]
int ledPin1 = random(10,12)

to select randomly between pins 10 and 11 but this did nothing. How (and where) would that function be implemented?

david_2018

You cannot use random() when you are declaring the ledPin1 and ledPin2 variables, because random() only works when the sketch is running on an arduino.  You would need to use random() in setup() to set the pin numbers assigned to the variables. It is also important to use randomseed() prior to using random(), otherwise you may get the same random number every time you run the sketch.




UKHeliBob

Quote
I tried using the random function and got errors. I tried:
Please post the complete program that you tried

I would put the pin numbers to be selected at random in an array and use random() to choose the array index and hence a random pin number
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

tekjones9

Here is the code that I attempted to run. The LED attached to pin 10 flashes at 10 hz and the other remains unlit...
Code: [Select]


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 (ms) - 10 Hz
unsigned long intervalLED11 = 1; //1 kHz

boolean LED10State = false; //state variable for LED
boolean LED11State = false;

int ledPin1;
int ledPin2;

void setup() {
  // put your setup code here, to run once
pinMode(ledPin1, OUTPUT);
//pinMode(ledPin2, OUTPUT); 

int myPins[]={10, 11};
randomSeed(2);

ledPin1 = myPins[random(10,12)];

if(ledPin1 = 10){
  int ledPin2 = 11;}
  else {
    int ledPin2 = 10;}
   
}

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;
}
}

PaulRB

Code: [Select]
ledPin1 = myPins[random(10,12)];
No, the array indexes are 0 and 1, not 10 and 11.

tekjones9

Code: [Select]
ledPin1 = myPins[random(10,12)];
No, the array indexes are 0 and 1, not 10 and 11.
Now neither LED illuminate after changing things so I'm not sure what I'm doing wrong here.





Code: [Select]
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 (ms) - 10 Hz
unsigned long intervalLED11 = 1; //1 kHz

boolean LED10State = false; //state variable for LED
boolean LED11State = false;

int ledPin1;
int ledPin2;

void setup() {
  // put your setup code here, to run once
pinMode(ledPin1, OUTPUT);
//pinMode(ledPin2, OUTPUT); 

int myPins[]={10, 11};
randomSeed(2);

ledPin1 = myPins[random(0,1)];

if(ledPin1 = 0){
  int ledPin2 = 1;}
  else {
    int ledPin2 = 0;}
   
}

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;
}
}

PaulRB

Code: [Select]
ledPin1 = myPins[random(0,1)];
Mistake there. When you wanted random to return 10 or 11, did you put "random(10,11)" ?

tekjones9

Code: [Select]
ledPin1 = myPins[random(0,1)];
Mistake there. When you wanted random to return 10 or 11, did you put "random(10,11)" ?
Am I supposed to use the array indices of 0 and 1 or the actual pin numbers 10 and 11?

UKHeliBob

The pin numbers are in an array
The array indices are 0 and 1
You need to generate a random number that is either 0 or 1 to use as the index to the array
Code: [Select]
random(0, 1);
does not generate a random number that is either 0 or 1.  Print it to see what it does generate
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

DrAzzy

The pin numbers are in an array
The array indices are 0 and 1
You need to generate a random number that is either 0 or 1 to use as the index to the array
Code: [Select]
random(0, 1);
does not generate a random number that is either 0 or 1.  Print it to see what it does generate
Or he could just read the reference documentation, specifically that for the max parameter...
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

UKHeliBob

Or he could just read the reference documentation, specifically that for the max parameter...
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
Agreed, but back in reply #7 he/she used the random() function correctly, albeit to generate the 2 wrong numbers
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up