Gouda, The Netherlands
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« on: February 09, 2013, 09:43:11 am » |
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
Tesla Member
Karma: 97
Posts: 6383
|
 |
« Reply #1 on: February 09, 2013, 09:58:05 am » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9429
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: February 09, 2013, 10:50:41 am » |
pseudo code for (int i=0; i<9; i++) { if (random(2) == 1) led[i] on else led[i] off }
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 97
Posts: 6383
|
 |
« Reply #3 on: February 09, 2013, 11:03:58 am » |
pseudo 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
|
|
|
|
|
Gouda, The Netherlands
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #4 on: February 09, 2013, 11:35:46 am » |
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: 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
Brattain Member
Karma: 277
Posts: 25556
Solder is electric glue
|
 |
« Reply #5 on: February 09, 2013, 11:55:14 am » |
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
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #6 on: February 09, 2013, 12:04:49 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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #7 on: February 09, 2013, 12:10:39 pm » |
you need to set a random value like in this sketch. #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
Brattain Member
Karma: 277
Posts: 25556
Solder is electric glue
|
 |
« Reply #8 on: February 09, 2013, 12:16:21 pm » |
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
Tesla Member
Karma: 97
Posts: 6383
|
 |
« Reply #9 on: February 09, 2013, 12:21:59 pm » |
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: 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
|
|
|
|
|
Gouda, The Netherlands
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #10 on: February 09, 2013, 12:40:00 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.
|
|
|
|
« Last Edit: February 09, 2013, 12:42:20 pm by Mubanga »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #11 on: February 09, 2013, 01:00:29 pm » |
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
|
|
|
|
|
Johannesburg UTC+2
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #12 on: February 09, 2013, 01:10:28 pm » |
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
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Gouda, The Netherlands
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #13 on: February 09, 2013, 01:56:58 pm » |
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
Tesla Member
Karma: 97
Posts: 6383
|
 |
« Reply #14 on: February 09, 2013, 02:20:23 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. 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
|
|
|
|
|
|