Go Down

Topic: [SOLVED] Random LED's (Read 1 time) previous topic - next topic

Mubanga

Feb 09, 2013, 03:43 pm Last 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

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

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


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


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 timer
int ledPins [] = {2, 3, 4, 5, 6, 7,}; //Sets pins
int pinCount = 6; //Sets the number of pins
int Number = 3; //Sets the amount of LEDs that will go on
int 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

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

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.

you need to set a random value like in this sketch.

Code: [Select]


#include <LiquidCrystal.h>

int address;
long randomvalue = 0; //random balue 1
long countervalue = 0; // counter value
LiquidCrystal 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

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


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 timer
const int ledPins [] = {
  2, 3, 4, 5, 6, 7,}; //Sets pins
const int pinCount = 6; //Sets the number of pins
int Number = 3; //Sets the amount of LEDs that will go on
int 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 pm Last 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

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

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.
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

Mubanga


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!

http://www.youtube.com/watch?v=Vu0geKPNs4E

johnwasser


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 timer
const int pinCount = 6; //Sets the number of pins
int Number = 3; //Sets the amount of LEDs that will go on
unsigned 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