# Random LED with weight attached

Hey there Guys / Girls,

Ok 1st off Im incredibly new to the ARDUINO and programming is a hobby i dable in nothing more.

I want a little assist here. Im making a little game for my Niece's ther basically 1 button and it will light a random LED, im using it for a stupid thing for a sweet for them (they are 2 and 3 so dont judge)

But anyways I want to make it a little more interesting by adding a weight to the LEDS so that say led 10 has less of a chance of being used than led 5 etc.

Any help will be greatly appreciated please.

Heres my code I have so far.

``````const int buttonPin = 2;                      // the number of the pushbutton pin
const int pause = 1000;                      // Set Delay time

// variables will change:
int buttonState = 0;                           // Variable for reading the pushbutton status
int ledPin = random(10);                     // Sets a Random LED as active

void setup() {
pinMode(ledPin, OUTPUT);     // Initialize the LED pin as an output:
pinMode(buttonPin, INPUT);   // Initialize the pushbutton pin as an input:
}

void loop(){
if (buttonState == HIGH) {                 // check if the pushbutton is pressed.
digitalWrite(ledPin, HIGH);              // turn LED on:
}
else {
digitalWrite(ledPin, LOW);               // turn LED off:
}
}
``````

Thanks in advance for any help.

You could create an array with pin numbers in it. Each pin number would appear a different number of times - one time for each weight value.

Then, create a random number between 0 and the size of that array. Light the pin at the random position on the array.

that may work....

breaks out the book Time to learn Arrays.

A simpler way would just be to create a random number between say 1 and 100, and then just select the LED based on a range of values, e.g.

``````if (random < 10){
whichLed = 0;
} else if (random < 12){
whichLed = 1;
} else if (random < 30){
whichLed = 2;
} else if... etc.
``````

in that case, led0 would have a probability of 10/100, led1 would have p=2/100, led2 would have p=18/100.

Well that would Work AWESOMENESS thanks dude.

Just a Update for those that may be wondering about it.
I got the code to work.
However I am looking at cleaning it up using a while statment(but it not working when i try it.).
Basically what this does is this.
Button is pressed, program goes through a Random generator thread and lights up a random LED. and will keep it on until the button is released.

``````const int buttonPin = 9;             // the number of the pushbutton pin

// variables will change:
int buttonState = 0;                  // Variable for reading the pushbutton status
int ledPin = 10;
int loopwait = 0;
//int ledPin = random(10);         // Sets a Random LED as active

void setup() {
pinMode(ledPin, OUTPUT);       // Initialize the LED pin as an output:
pinMode(buttonPin, INPUT);     // Initialize the pushbutton pin as an input:
}

void loop(){
if (loopwait == 0) {
if (buttonState == HIGH) {                 // check if the pushbutton is pressed.
// turn LED on:
if (random(100) < 20){
ledPin = 0;
} else if (random(100) < 40){
ledPin = 1;
} else if (random(100) < 60){
ledPin = 2;
} else if (random(100) < 70){
ledPin = 3;
} else if (random(100) < 80){
ledPin = 4;
} else if (random(100) < 90){
ledPin = 5;
} else if (random(100) < 94){
ledPin = 6;
} else if (random(100) < 98){
ledPin = 7;
} else if (random(100) < 101){
ledPin = 8;}
digitalWrite(ledPin, HIGH);
loopwait = 1;}
else {
if (buttonState == LOW){
loopwait = 0;
digitalWrite(ledPin, LOW);       // turn LED off:
}
}
}
else {
if (buttonState == LOW){
loopwait = 0;
digitalWrite(ledPin, LOW);       // turn LED off:
}
}
}
``````

There are a couple of things that spring to mind. Firstly, you have 9 led pins, but dont set any of them to be outputs. I added a for loop to the setup to achieve this, along with an array to allow you to choose which pins you want the LEDs to be connected to (arrays are very simple!). I think this is your problem.

The second thing, although it is not wrong, is you have some redundant if statements. Where you are checking whether the button is high, and whether the led has already been turned on (loopwait), you can simplify if by checking the button first, so I changed that.

The final thing which wont help at all is that in the if{}else if{}else if{} tree, you are generating a new random number for each else statement, you need to use the same one otherwise it will be inconsistent. I added a variable which is set a random number, and then the variable is used in the if{}else if{} statemnts.

``````//Save some memory, use bytes and booleans rather than ints for pin numbers and pin states. (pin numbers are <255, so an int isnt needed)
const byte buttonPin = 9;             // the number of the pushbutton pin

// variables will change:
boolean buttonState = 0;                  // Variable for reading the pushbutton status
byte ledPin = 0;
boolean loopwait = 0;

#define maxLEDs 9 //This replaces anything which say 'maxLEDs' with the number 9
byte ledPins[maxLEDs] = {0,1,2,3,4,5,6,7,8}; //set your LED pins here

void setup() {
//pinMode(ledPin, OUTPUT);       // Initialize the LED pin as an output:
pinMode(buttonPin, INPUT);     // Initialize the pushbutton pin as an input:
for (byte i = 0; i < maxLEDs; i++){
pinMode(ledPins[i], OUTPUT); //Set all of the different LEDs to outputs.
}
}

void loop(){
if (buttonState == HIGH) {                 // check if the pushbutton is pressed.
if (loopwait == 0) {
// turn LED on:
byte randomNumber = random(100); //You want all statements to check against the same number, not generate a new one for each elseif() statement
if (randomNumber < 20){
ledPin = 0;
}
else if (randomNumber < 40){
ledPin = 1;
}
else if (randomNumber < 60){
ledPin = 2;
}
else if (randomNumber < 70){
ledPin = 3;
}
else if (randomNumber < 80){
ledPin = 4;
}
else if (randomNumber < 90){
ledPin = 5;
}
else if (randomNumber < 94){
ledPin = 6;
}
else if (randomNumber < 98){
ledPin = 7;
}
else if (randomNumber < 101){
ledPin = 8;
}
digitalWrite(ledPins[ledPin], HIGH);
loopwait = 1;
}
} else {
loopwait = 0;
digitalWrite(ledPins[ledPin], LOW);       // turn LED off:
}
}
``````

thanks TCWORLD. Def a cleaner code. and shot on the assist.

now for some reason my stupid thing not working when wired up =(

Little emulator 100% actually wiring. No dice. just not happy.

In what way is it not working? in other words, what is it doing?

OK currently I have it connected as in the link below:

What its doing is soon as power is connected the leds randomize constantly lighting up and off all the time.
On the button press it stops. at a random led.
But I know when looking at the code its led off unless button press

side note thanks to everyone for the help, i really left learning my electronics and programming to a late point in life

EDIT:[/b] plus a extra 10ohm resistance from button to pin 9 sorry

On the button press it stops. at a random led.
But I know when looking at the code its led off unless button press

This suggests, then, that your switch is wired incorrectly.

You are not using the internal pullup resistor. Why not?

This means that you need an external pullup or pulldown resistor. Do you have one?

How IS the switch wired?

......... um if I remove the restor its the same.
as for using the internal. ..... I'm still learning this hahaha. i did basic lil flashy things and the basic button on light on thing. to get a feel for it.

I is the UBER noob with these devices.

The simplest, most fool-proof method of wiring a switch is to connect one side to ground. Connect the other side to the digital pin, and turn the pullup resistor on for that pin (using digitalWrite(pinNumber, HIGH); after declaring the pin as an INPUT pin (in setup()).

The resistor doesn't go between the button and the arduino pin, it goes from the arduino pin and button at one end to +5v at the other. as PaulS said, you can use the built in pullup instead, it is easier.

Also you should have resistors between the LEDs and ground, otherwise you will slowly damage the IC. These can be about 470 to 3k Ohm.

mm ok

ill upgrade the resistor I have from LEDS to 500 Ohms.

If I add the line it then becomes a "on normally closed switch" and on circuit open it does the randomization.

this works now i just can understand why