Go Down

### Topic: [SOLVED] Random LED's (Read 2800 times)previous topic - next topic

#### Mubanga

##### Feb 09, 2013, 03:43 pmLast Edit: Feb 10, 2013, 03:35 pm by Mubanga Reason: 1
Hi,

Say I have 9 LED's connected to my arduino on pin 1 to 9. I want to be able to let a certain number of LED's go on, but which of the LED's go on has to be random. Any thoughts?

#### johnwasser

#1
##### Feb 09, 2013, 03:58 pm
I wouldn't use Pin 1 (Serial TX) for I/O since Serial.print() is so helpful for debugging.

I'd use an array of N pin numbers.  Pick some pairs of random numbers between 0 and N-1.  Swap those two elements of the array to shuffle the array.  Go through the array from 0 to N-1 turning on the desired number of LEDs and turning off the rest.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#### robtillaart

#2
##### Feb 09, 2013, 04:50 pm
pseudo code

Code: [Select]
`for (int i=0; i<9; i++){  if (random(2) == 1) led[i] on  else led[i] off}`
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### johnwasser

#3
##### Feb 09, 2013, 05:03 pm

pseudo code
Code: [Select]
`for (int i=0; i<9; i++){  if (random(2) == 1) led[i] on  else led[i] off}`

That will turn on a random combination of the 9 LEDs but will not "let a certain number of LED's go on, but which of the LED's go on has to be random."  By that I assume they meant that the number of lights turned on is known but the pattern is random.  That's why I recommended the shuffle.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#### Mubanga

#4
##### Feb 09, 2013, 05:35 pm

That will turn on a random combination of the 9 LEDs but will not "let a certain number of LED's go on, but which of the LED's go on has to be random."  By that I assume they meant that the number of lights turned on is known but the pattern is random.  That's why I recommended the shuffle.

That's right, I didn't fully understand your first post though but so far I've got this:

Code: [Select]
`int timer = 1000; //Sets timerint ledPins [] = {2, 3, 4, 5, 6, 7,}; //Sets pinsint pinCount = 6; //Sets the number of pinsint Number = 3; //Sets the amount of LEDs that will go onint thisPin; //Specificies a certain pin  void setup() {  Serial.begin(9600);  for (int thisPin = 0; thisPin < pinCount; thisPin++) {    pinMode(ledPins[thisPin], OUTPUT); //Sets all the ledPins as output  }}void loop(){  for (int i=0; i<=Number; i++){  if (i < Number) {   thisPin = random(pinCount);  digitalWrite(ledPins[thisPin], HIGH); }  //this will loop "Number" times  else {  delay(timer);  for (int thisPin = 0; thisPin < pinCount; thisPin++) {  digitalWrite(ledPins[thisPin], LOW); } //turns of all the pins  }  }}`

this almost works except sometimes there will be only 1 or 2 light burning instead of 3, because the pin has already been picked.

#### Grumpy_Mike

#5
##### Feb 09, 2013, 05:55 pm
Quote
sometimes there will be only 1 or 2 light burning instead of 3, because the pin has already been picked.

So instead of just picking a random number and using it you pick a random number and if it has already been chosen pick another one. A wile loop can be used to ensure that it only exits when a number not chosen before has been selected.

This is oddly prescriptive, is it homework?

#### Mubanga

#6
##### Feb 09, 2013, 06:04 pm
Thanks!

This is oddly prescriptive, is it homework?

No not at all, I just picked up Arduino as a hobby and I'm trying to rebuild a binary clock I saw a while back.

#### legendlcox

#7
##### Feb 09, 2013, 06:10 pm
you need to set a random value like in this sketch.

Code: [Select]
`#include <LiquidCrystal.h>int address;long randomvalue = 0; //random balue 1long countervalue = 0; // counter valueLiquidCrystal lcd(8, 9, 4, 5, 6, 7);void setup(){  lcd.begin(16,2);  lcd.print("COX Industries");  delay(1000);    countervalue = 1;  lcd.setCursor(0,0);  lcd.print("------START-----");  lcd.setCursor(0,1);  lcd.print("--CALCULATION---");  delay(1500);  lcd.clear();}void loop(){  lcd.setCursor(0,0);  randomvalue = random(1000000000);  lcd.print(countervalue);  lcd.setCursor(4,1);  lcd.print(randomvalue);  delay(500);  lcd.clear();  countervalue = (countervalue+1)%1000000;// increment the counter}`

#### Grumpy_Mike

#8
##### Feb 09, 2013, 06:16 pm
Quote
you need to set a random value like in this sketch.

And how is that going to stop there being a repeat number in any batch of random numbers?

#### johnwasser

#9
##### Feb 09, 2013, 06:21 pm

Quote
sometimes there will be only 1 or 2 light burning instead of 3, because the pin has already been picked.

So instead of just picking a random number and using it you pick a random number and if it has already been chosen pick another one. A while loop can be used to ensure that it only exits when a number not chosen before has been selected.

Like this:
Code: [Select]
`int timer = 1000; //Sets timerconst int ledPins [] = {  2, 3, 4, 5, 6, 7,}; //Sets pinsconst int pinCount = 6; //Sets the number of pinsint Number = 3; //Sets the amount of LEDs that will go onint thisPin;int alreadySet;void setup() {  Serial.begin(9600);  for (int thisPin = 0; thisPin < pinCount; thisPin++) {    pinMode(ledPins[thisPin], OUTPUT); //Sets all the ledPins as output  }}void loop(){  // Turn off all the LEDs  for (int thisPin = 0; thisPin < pinCount; thisPin++)    digitalWrite(ledPins[thisPin], LOW);  // Turn on "Number" randonm LEDs  for (int i=0; i<Number; i++) {    do {      thisPin = random(pinCount);      alreadySet = digitalRead(ledPins[thisPin]);      digitalWrite(ledPins[thisPin], HIGH);     }     while (alreadySet);  // If the LED was already on, try a different one  }  delay(timer);}`
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#### Mubanga

#10
##### Feb 09, 2013, 06:40 pmLast Edit: Feb 09, 2013, 06:42 pm by Mubanga Reason: 1
Thanks John! Although this way I won't be able to use shift registers won't I? because in my final project I will be using 27 LED's. But that's not to big of a problem, in that case I will use I2C I/O expanders.

#### PaulS

#11
##### Feb 09, 2013, 07:00 pm
Quote
No not at all, I just picked up Arduino as a hobby and I'm trying to rebuild a binary clock I saw a while back.

How does a clock use random numbers?

#### JimboZA

#12
##### Feb 09, 2013, 07:10 pm
Quote
How does a clock use random numbers?

Well, I was just reading this Instructable about someone whose sleep pattern might be better suited to 6x sleeps a week not the traditional 7..... maybe others would be happier on time system which wakes them up and sends them to bad randomly.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### Mubanga

#13
##### Feb 09, 2013, 07:56 pm

Quote
No not at all, I just picked up Arduino as a hobby and I'm trying to rebuild a binary clock I saw a while back.

How does a clock use random numbers?

Like this!

#### johnwasser

#14
##### Feb 09, 2013, 08:20 pm

Thanks John! Although this way I won't be able to use shift registers won't I? because in my final project I will be using 27 LED's. But that's not to big of a problem, in that case I will use I2C I/O expanders.

You can use an unsigned long (32-bit integer) to keep track of which LEDs you want on and off.
Code: [Select]
`int timer = 1000; //Sets timerconst int pinCount = 6; //Sets the number of pinsint Number = 3; //Sets the amount of LEDs that will go onunsigned long pattern;int thisPin;int alreadySet;void loop(){  // Turn off all the LEDs  pattern = 0;  // Turn on "Number" randonm LEDs  for (int i=0; i<Number; i++) {    do {      thisPin = random(pinCount);      alreadySet = pattern & (1<<thisPin));      pattern |= (1<<thisPin);    }     while (alreadySet);  // If the LED was already on, try a different one  }  //  Send 'pattern' to shift registers  delay(timer);}`
[/quote]
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up

Please enter a valid email to subscribe