Pages: [1] 2   Go Down
Author Topic: [SOLVED] Random LED's  (Read 1048 times)
0 Members and 1 Guest are viewing this topic.
Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: February 10, 2013, 09:35:36 am by Mubanga » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pseudo code

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

Rob Tillaart

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pseudo code
Code:
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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31543
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

#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

}

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31543
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 09, 2013, 12:42:20 pm by Mubanga » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Retired from Arduino.
PMs are set to ignore all.

Gouda, The Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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]
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1] 2   Go Up
Jump to: