Can somebody help me with code to Lottery machine (only LEDs)

Hey guys Im doing project for kids in camp. So they want me to build Lottery machine and I dont know if I can do it my self. I made simple code and with "I made" I mean I sort of edited somebodys code. So I want to press button and choose random 5 LEDs to light up out of 40. Ill use Arduino Mega 2560. Problem with my code is most of the time when I press button only 3 or 4 LEDs will light up because I just make it to choose (on my prototype) 1LED out of 7 and make that 5 time so most of the time only 3 will light up cuz of they were "picked" multiple times. Can somebody edit or remake my code? I have no idea what Im doing guys please help. I dont know how to code. Next year in school they will teach us something but I need it faster not after holidays. HELP!!!

Adding profile mode would be nice aswell (with a button sellect how make winners (LEDs) will light up at the same time like 1out of 40 2out of 40 3out of 40 and 4out of 40)

int timeShowRandom = 2500; //this is how long picking will be
int timeShowDecision = 5000; //this will set up how ling "winners" light will be ON
int timeBlink = 15;
int buttonPin = 3; //Here I want to add 4 other options wich will set up how many "winners" will be choosed

int buttonPress = false;
int randomNumber;
int previousNo = 1;
int timePassed = 0;

void setup()
{
// Set button pin
pinMode(buttonPin, INPUT);
// Set output pins
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT); ////Now I use only 7 LEDs but soon I will have to add up to 40
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
}

void getRandomNo()
{
int rand = random(6, 13); //This show pins where are LEDs its (fistLED,lastLED+1) so last LED is 12
if (rand == previousNo)
{
getRandomNo();
}
else
{
randomNumber = rand;
previousNo = randomNumber;
}

}

void loop()
{
// Check if button is pressed
if (digitalRead(buttonPin) == HIGH && buttonPress == false)
{
buttonPress = true;
}
if (buttonPress == true && timePassed <= timeShowRandom)
{
getRandomNo(); // Get random pin number
digitalWrite(randomNumber, HIGH);
delay(timeBlink);
digitalWrite(randomNumber, LOW);
delay(timeBlink);
timePassed = timePassed + (timeBlink * 2);
}
else if (buttonPress == true)
{
digitalWrite(random(6, 13), HIGH); // Set random pin on
digitalWrite(random(6, 13), HIGH); // Set random pin on
digitalWrite(random(6, 13), HIGH); // Set random pin on
digitalWrite(random(6, 13), HIGH); // Set random pin on
digitalWrite(random(6, 13), HIGH); // Set random pin on
delay(timeShowDecision); // For x seconds
buttonPress = false; // Set button to be enabled again
timePassed = 0;

}
else
{
// Reset all output pins
digitalWrite(6, LOW); //Now I use only 7 LEDs but soon I will have to add up to 40
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}
}

arduino_sundh_random_leds1.pngI just added 4 more LEDs

Quick pointers:

1.You are not debouncing the button press so it is likely to read as multiple instances..
2. Generating four random number in a group of 7 has more than likely to repeat the same number- that is number 6 might come up twice for instance
3. Instead of that 40 LEDs how about a matrix display or board?
4. 40 LEDs are likely to lead to multiple

Please reply to my PM or here if you want code specifics..

Create an array for the 40 pins.

Make a random number generator that produces five numbers - check for duplicates in the process. Switch off all the LEDs (just do them all, don't bother figuring out which were on), then light up the 5 selected LEDs.

So basically:

const byte ledPins = {1, 2, 3, ..., 40}; // The pin numbers.
const byte nLeds = 40;
const byte buttonPin = 41; // The pin the button is connected to - button between pin and GND.

void setup() {
  for (byte i = 0; i < nLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  if (digitalWrite(buttonPin == LOW) {
    byte randLeds[5];
    // Populate this array with 5 different random numbers from 0-39.
    for (byte i = 0; i < nLeds; i++) {
      digitalWrite(ledPins[i], LOW); // Switch them all off.
    }
    for (byte i = 0; i < 5; i++) {
      digitalWrite(ledPins[randLeds[i]], HIGH); // Switch the five on.
    }
    delay(100);
  }
}

You do the pin numbers and the random function.
As long as the button is pressed it will show a new code 10x a second, as if running through codes. More fun and easier than doing state change and debouncing. Change the delay() for faster/slower. It stops when the button is released.

Key here is the use of arrays. Now you get it working with 7 LEDs, after that just change nLeds and the list of pin numbers and you're done.

Awesome reply for me.. But it could loose a beginner who first has to figure how arrays work...

wvmarle:
Create an array for the 40 pins.

Make a random number generator that produces five numbers - check for duplicates in the process. Switch off all the LEDs (just do them all, don't bother figuring out which were on), then light up the 5 selected LEDs.

So basically:

const byte ledPins = {1, 2, 3, ..., 40}; // The pin numbers.

const byte nLeds = 40;
const byte buttonPin = 41; // The pin the button is connected to - button between pin and GND.

void setup() {
  for (byte i = 0; i < nLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  if (digitalWrite(buttonPin == LOW) {
    byte randLeds[5];
    // Populate this array with 5 different random numbers from 0-39.
    for (byte i = 0; i < nLeds; i++) {
      digitalWrite(ledPins[i], LOW); // Switch them all off.
    }
    for (byte i = 0; i < 5; i++) {
      digitalWrite(ledPins[randLeds[i]], HIGH); // Switch the five on.
    }
    delay(100);
  }
}




You do the pin numbers and the random function.
As long as the button is pressed it will show a new code 10x a second, as if running through codes. More fun and easier than doing state change and debouncing. Change the delay() for faster/slower. It stops when the button is released.

Key here is the use of arrays. Now you get it working with 7 LEDs, after that just change nLeds and the list of pin numbers and you're done.

mugambi:
Awesome reply for me.. But it could loose a beginner who first has to figure how arrays work...

Then it's very much time for that beginner to learn about arrays, it's a pretty basic thing. There is no other sensible way of doing this without arrays, unless you really like code that repeats the same thing 40 times, and that becomes completely unmaintainable in the process.

First, what wvmarle said - you are going to have to learn about arrays. It's a basic thing that all programming languages do (even BASIC), and it's not that hard.

Second, there are many, many better ways to do this than attempting to wire up 40 pins to 40 LEDs. The easiest I know of is using Adafruit NeoPixels. It has an arduino library, I have used them several times. The next easiest might be using five 8-bit shift registers.

Having said that, if you don't want to use arrays, you'll need something like:

int led1, led2, led3, led4, led5;

led1 = random(1, 40);
do {
  led2 = random(1,40);
} while(led2 == led1);
do {
  led3 = random(1,40);
} while(led3 == led1 || led3 == led2);
do {
  led4 = random(1,40);
} while(led4 == led1 || led4 == led2 || led4 == led3);
do {
  led5 = random(1,40);
} while(led5 == led1 || led5 == led2 || led5 == led3 || led5 == led4);

PaulMurrayCbr:
Second, there are many, many better ways to do this than attempting to wire up 40 pins to 40 LEDs. The easiest I know of is using Adafruit NeoPixels. It has an arduino library, I have used them several times. The next easiest might be using five 8-bit shift registers.

Requires more soldering than 40 LEDs to individual pins... after all you still have to wire each individual LED, but you throw some extra ICs in the mix. Also code gets a bit more complex. OP has a Mega already, so why not use the readily available outputs?
Power is also no issue as there will be no more than 5 LEDs on at the same time (the Mega can't power all of them at the same time).

The random code, I'm sure this can be done easier and more flexible using two nested for loops. And definitely you should use byte rather than int as type, the numbers are guaranteed in the 0-255 range.

Here is a slightly different code; it might be slightly easier to understand although it wastes a few bytes (compared to wvmarle's code) because it keeps track of the state of every led. Code assumes that HIGH switched LED on. It again uses arrays :wink:

// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// array holding the led pins; adjust to what you need
const byte ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// array holding the state of each led for a cycle
// the size is automatically calculated at compile time based on the number of led pins defined above
byte ledStates[sizeof(ledPins)];

void setup()
{
  // adjus baudrate to needs
  Serial.begin(57600);

  // safety check
  if (NUMLEDS > sizeof(ledStates))
  {
    Serial.println("Can't have more leds ON than there are leds");
    for (;;);
  }

  // configure led pins
  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // set led pins OFF
    ...
    ...
    // set led pins to output
    ...
    ...
  }
}

void loop()
{
  // clear all led states
  memset(ledStates, LOW, sizeof(ledStates));
  // a counter to count the number of leds that will be ON
  byte onCount = 0;

  // select NUMLEDS leds to be on
  while (onCount != NUMLEDS)
  {
    // get number of a led that must be ON
    int randomNumber = random(0, sizeof(ledStates));
    // if it was not ON yet in the array
    if (ledStates[randomNumber] != HIGH)
    {
      // indicate that it must be on
      ledStates[randomNumber] = HIGH;
      // increment the number of leds that is on
      onCount++;
    }
  }

  // display the leds
  for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
  {
    // debug
    Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
    // set the leds
    //digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
  }
  // debug; new line
  Serial.println();

  delay(5000);
}

That looks like a pretty good way of making sure you have exactly five LEDs lit in the end. Indeed can probably be optimised for memory, but not worth the effort here.

sterretje:
Here is a slightly different code; it might be slightly easier to understand although it wastes a few bytes (compared to wvmarle's code) because it keeps track of the state of every led. Code assumes that HIGH switched LED on. It again uses arrays :wink:

// number of leds that need to be ON in every cycle

#define NUMLEDS 5
// array holding the led pins; adjust to what you need
const byte ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// array holding the state of each led for a cycle
// the size is automatically calculated at compile time based on the number of led pins defined above
byte ledStates[sizeof(ledPins)];

void setup()
{
  // adjus baudrate to needs
  Serial.begin(57600);

// safety check
  if (NUMLEDS > sizeof(ledStates))
  {
    Serial.println("Can't have more leds ON than there are leds");
    for (;;);
  }

// configure led pins
  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // set led pins OFF
    ...
    ...
    // set led pins to output
    ...
    ...
  }
}

void loop()
{
  // clear all led states
  memset(ledStates, LOW, sizeof(ledStates));
  // a counter to count the number of leds that will be ON
  byte onCount = 0;

// select NUMLEDS leds to be on
  while (onCount != NUMLEDS)
  {
    // get number of a led that must be ON
    int randomNumber = random(0, sizeof(ledStates));
    // if it was not ON yet in the array
    if (ledStates[randomNumber] != HIGH)
    {
      // indicate that it must be on
      ledStates[randomNumber] = HIGH;
      // increment the number of leds that is on
      onCount++;
    }
  }

// display the leds
  for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
  {
    // debug
    Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
    // set the leds
    //digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
  }
  // debug; new line
  Serial.println();

delay(5000);
}

Thats look good. WOW

sterretje:
// configure led pins
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
// set led pins OFF
... // <------------ I just have no idea what to put here
... // <------------ I just have no idea what to put here
// set led pins to output
... // <------------ I just have no idea what to put here
... // <------------ I just have no idea what to put here
} // I mean I know I have to set led pinds nums but I have no idea how
}

You know pinMode()? If not, look at wvmarle's code.
You know digitalWrite()? If not, look a little further down in my code (I commented it out) or in wvmarle's code.

Although I used two lines with ... for each, it's just one for each.

Another thing: make sure you actually understand the pieces of code we posted here. Your reply tells me you don't understand much at all from what we posted, or what it is actually doing. Without this understanding no chance for you to fill in the blanks or modify the code to fit your precise needs.

Im trying Just got this Just need to add trigger and set some times and make LEDs light up. Any Ideas

So I Edited code as you said and serial port works with my 7LEDs Now I need to add button to fire thing up for 1 round and force LEDs to light up cuz now they dont work. So what now. And one more time thx for help

// počet LEDs co musí být ON každý cyklus
#define NUMLEDS 5
// Piny LEDs pak změnit na 40!!!!!
const byte ledPins[] = {6, 7, 8, 9, 10, 11, 12};
// Udržení stavu LEDs
// velikost je automaticky vypočtena v době kompilace na základě počtu vodičů definovaných výše^^
byte ledStates[sizeof(ledPins)];

void setup()
{
// Rychlost přenosu
Serial.begin(57600);

// check
if (NUMLEDS > sizeof(ledStates))
{
Serial.println("Can't have more leds ON than there are leds");
for (;;);
}

// konfigurace led pinů
for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
{
// Nasravit LEDs piny OFF
//set the leds
digitalWrite(ledPins[LOW], ledStates[LOW]);
// Nastavit LED piny na výstup
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
}

void loop()
{
// clear všech LEDs
memset(ledStates, LOW, sizeof(ledStates));
// a counter to count the number of leds that will be ON
byte onCount = 0;

// Vybrat NUMLEDS LEDs na on
while (onCount != NUMLEDS)
{
// Číslo LED co musí být ON
int randomNumber = random(0, sizeof(ledStates));
// if it was not ON yet in the array
if (ledStates[randomNumber] != HIGH)
{
// indicate that it must be on
ledStates[randomNumber] = HIGH;
// increment the number of leds that is on
onCount++;
}
}

// Zobrazit LEDs
for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
{
// debug
Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
// set the leds
//digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
}
// debug; 2
Serial.println();

delay(5000);
}

  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // Nasravit LEDs piny OFF
    //set the leds
digitalWrite(ledPins[LOW], ledStates[LOW]);
    // Nastavit LED piny na výstup
 pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
   pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
     pinMode(10, OUTPUT);
      pinMode(11, OUTPUT);
       pinMode(12, OUTPUT);
  }

Those pinModes are not as wvwarle demonstrated.Why would you set a pin to output N times? Once is enough. The digitalWrite is also not correct.

In setup ()

for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // Nasravit LEDs piny OFF
    //set the leds
    digitalWrite(ledPins[ledCnt], LOW);
    // Nastavit LED piny na výstup
    pinMode(ledPins[ledCnt], OUTPUT);
  }

You also have nit activated the actual switching on/off in the for-loop in loop ().

In your original code you were well on your way with the button stuff and doing things if the button is pressed; put everything that I presented inside loop() now inside the { and } of your if that rekstes to the button press,

sterretje:

  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)

{
    // Nasravit LEDs piny OFF
    //set the leds
digitalWrite(ledPins[LOW], ledStates[LOW]);
    // Nastavit LED piny na výstup
pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
      pinMode(11, OUTPUT);
      pinMode(12, OUTPUT);
  }



Those pinModes are not as wvwarle demonstrated.Why would you set a pin to output N times? Once is enough. The digitalWrite is also not correct.

In setup ()


for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // Nasravit LEDs piny OFF
    //set the leds
    digitalWrite(ledPins[ledCnt], LOW);
    // Nastavit LED piny na výstup
    pinMode(ledPins[ledCnt], OUTPUT);
  }




You also have nit activated the actual switching on/off in the for-loop in loop ().

In your original code you were well on your way with the button stuff and doing things if the button is pressed; put everything that I presented [u]inside[/u] loop() now inside the { and } of your if that rekstes to the button press,

I just dont understand wich part of that code of wvwarle should I add in? I tried to replace all of my 7 pinoutputs to ˘˘

void setup() {
for (byte i = 0; i < nLeds; i++) {
pinMode(ledPins*, OUTPUT);*

  • }*
  • pinMode(buttonPin, INPUT_PULLUP);*
    }
    [/quote]
    But it does not work like that. :frowning: Im sad guys. You are trying to help me and I just dont understand

Mikey999:
I just dont understand wich part of that code of wvwarle should I add in? I tried to replace all of my 7 pinoutputs to ˘˘

Which part of my code you don't understand?

Mikey999:
I just dont understand wich part of that code of wvwarle should I add in? I tried to replace all of my 7 pinoutputs to ˘˘

I did not say to copy wvmarle's code in; I asked to compare what you created in your last attempt with what wvmarle posted in reply #2.

So let's try to start from the beginning. An array is like a set of drawers.

You have 40 drawers for led pins. Put a sticker on the holding enclosure of the set of drawers with the name ledPins.
2)
Put a piece of paper with the number of the led pin in each of the drawers.

In code (for 9 drawers)

const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};

Next you want to set those led pins to output, so you open the first drawer (computers start counting from 0 so the first drawer is number 0). Read the pin number from the piece of paper and set that pin to output.

In code

pinMode(ledPins[0], OUTPUT);

Close the drawer (no code) and open the second drawer (drawer numbered 1), read the pin number from the piece of paper and set that pin to output.
In code

pinMode(ledPins[1], OUTPUT);

You can do that for all 40 drawers manually. You can also use a for-loop telling the Arduino to open all drawers in sequence; that for-loop will start at 0 and end at 39. The for-loop uses a variable to keep track of which drawer to open to read the pin number.

In code (the variable is called ledCnt in my code, i in wvmarle's code)

// drawer set with led pin numbers; add more pin numbers separated by commas if needed
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};

void setup()
{
  // open each drawer of the 40, read the pin number and set the pin with that pin number to output
  // remember that the Arduino counts from 0 so 0 is the first drawerr and 39 is the last drawer.
  // ledPins is the set of drawers, ledCnt is the number of the drawer to be opened
  for (byte ledCnt = 0; ledCnt < 40; ledCnt++)
  {
    // set the pin, found by reading the paper in drawer ledCnt, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}

Now there is a slight problem; we only have 9 out of 40 pins defined for the leds, not all 40. You can use the sizeof operator to determine the size (in bytes) of the array. Because the array contains bytes, the number will be the number of elements in the array.

To demonstrate, the below code

// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};

void setup()
{
  // serial setup for debugging
  Serial.begin(57600);

  // debug: print the number of drawers in the set of drawers
  Serial.print("Number of led pins = "); Serial.println(sizeof(ledPins));

  // open each drawer in the set of drawers, read the pin number and set the pin with that pin number to output
  // ledPins is the drawer set, ledCnt is the number of the drawer
  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // set the pin, found by reading the paper in drawer ledCnt, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}
8)

You also want to guarantee that all leds will be off when the program starts. For that you use digitalWrite. The code that I presented does this before you set the pin to output to prevent possible accidental flashes.

In code

// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};

void setup()
{
  // serial setup for debugging
  Serial.begin(57600);

  // debug: print the number of drawers in the set of drawers
  Serial.print("Number of led pins = "); Serial.println(sizeof(ledPins));

  // open each drawer in the set of drawers, read the pin number
  // ledPins is the drawer set, ledCnt is the number of the drawer
  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // set the pin, found by reading the paper in drawer ledCnt, LOW
    digitalWrite(ledPins[ledCnt], LOW);

    // set the pin, found by reading the paper in drawer ledCnt, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}

Do you now understand how that part works? And what you're supposed to do?

Now you create a second set of drawers that will hold the state of the 40 leds. Because the above example only has 9 and we don't want to modify the code in tens (or hundreds) of places every time that we add a led, we again use the sizeof operator

// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};
// set of drawers (array) holding the state of each led for a cycle
// the size (number of drawers) is automatically calculated at compile time based on number of drawers in the ledPins drawer set
byte ledStates[sizeof(ledPins)];

In setup, we will build a safety check so the code does not crash if the number of random leds that needs to be on. First we add the number of leds that can be on at the top of the code.

// number of leds that need to be ON in every cycle
#define NUMLEDS 5

And we add the check (complete code)

// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};
// drawer set (array) holding the state of each led for a cycle
// the size (number of drawers) is automatically calculated at compile time based on number of drawers in the ledPins drawer set
byte ledStates[sizeof(ledPins)];

void setup()
{
  // serial setup for debugging
  Serial.begin(57600);

  // debug: print the number of drawers in the set of drawers
  Serial.print("Number of led pins = "); Serial.println(sizeof(ledPins));

  // safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
  if (NUMLEDS > sizeof(ledStates))
  {
    Serial.println("Can't have more leds ON than there are leds");
    // hang forever
    for (;;);
  }

  // open each drawer in the set of drawers, read the pin number
  // ledPins is the drawer set, ledCnt is the number of the drawer
  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // set the pin, found by reading the paper in drawer ledCnt, LOW
    digitalWrite(ledPins[ledCnt], LOW);

    // set the pin, found by reading the paper in drawer ledCnt, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}

void loop()
{
}

Next we can build loop(). We will put papers on every drawer indicating that all leds are off (LOW)so we're sure each drawer is empty at the beginning of loop(). We also add a counter (onCount)that counts the number of leds that is set to on (HIGH).
In code

  // put a pieces of paper with LOW written on it in every drawer of the led states
  memset(ledStates, LOW, sizeof(ledStates));
  // a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
  byte onCount = 0;

Next we remove from 5 (NUMLEDS) randomly selected drawers in the drawer set ledStates the paper and replace it with one of the pieces of paper with HIGH written on it. We only do this if the randomly selected drawer contains a piece of paper that has LOW written on it, else we will retry with another randomly selected drawer. Every time that we replace a piece of paper, we increment the counter. We use a while-loop() to check if the number of drawers that has a piece of paper with HIGH on it has reached the number of drawers that must have a piece of paper with HIGH on it.

In code

  // select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
  while (onCount != NUMLEDS)
  {
    // open a random drawer in ledStates
    int randomNumber = random(0, sizeof(ledStates));
    // if the paper in there indicated that the led must be off (not on (HIGH))
    if (ledStates[randomNumber] != HIGH)
    {
      // take the LOW paper out and put HIGH paper in
      ledStates[randomNumber] = HIGH;
      // increment the number of drawers that have a piece of paper with HIGH on it
      onCount++;
    }
  }

Once we have 5 (NUMLEDS) drawers filled with a piece of paper with HIGH on it, we can use a for-loop to loop through all drawers in the ledStates drawer set and set the pin number in each drawer in the ledPins drawer set to the state that was found in the corresponding drawer (same number, drawer 0 <-> drawer 0, drawer 1 <-> drawer 1 etc) in the ledStates drawer set.

In code

  // display the leds
  // open each drawer in the ledStates drawer set
  for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
  {
    // debug
    Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
    // set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
    //  to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
    digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
  }
  // debug; new line
  Serial.println();

Below the complete code

// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};
// set of drawers (array) holding the state of each led for a cycle
// the size (number of drawers) is automatically calculated at compile time based on number of drawers in the ledPins drawer set
byte ledStates[sizeof(ledPins)];

void setup()
{
  // adjus baudrate to needs
  Serial.begin(57600);

  // safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
  if (NUMLEDS > sizeof(ledStates))
  {
    Serial.println("Can't have more leds ON than there are leds");
    // hang forever
    for (;;);
  }

  // open each drawer in the set of drawers, read the pin number
  // ledPins is the drawer set, ledCnt is the number of the drawer
  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // set the pin, found by reading the paper in drawer ledCnt, LOW
    digitalWrite(ledPins[ledCnt], LOW);

    // set the pin, found by reading the paper in drawer ledCnt, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}

void loop()
{
  // put a pieces of paper with LOW written on it in every drawer of the led states
  memset(ledStates, LOW, sizeof(ledStates));
  // a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
  byte onCount = 0;

  // select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
  while (onCount != NUMLEDS)
  {
    // open a random drawer in ledStates
    int randomNumber = random(0, sizeof(ledStates));
    // if the paper in there indicated that the led must be off (not on (HIGH))
    if (ledStates[randomNumber] != HIGH)
    {
      // take the LOW paper out and put HIGH paper in
      ledStates[randomNumber] = HIGH;
      // increment the number of drawers that have a piece of paper with HIGH on it
      onCount++;
    }
  }

  // display the leds
  // open each drawer in the ledStates drawer set
  for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
  {
    // debug
    Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
    // set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
    //  to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
    digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
  }
  // debug; print a new line
  Serial.println();

  // wait a bit
  delay(5000);
}

Understand how it works?

So now we can run this only when a button is pressed; I have copied parts of your original code (in the opening post).

int timeShowRandom = 2500;      //this is how long picking will be
int timeShowDecision = 5000;    //this will set up how ling "winners" light will be ON
int timeBlink = 15;
int buttonPin = 3;              //Here I want to add 4 other options wich will set up how many "winners" will be choosed

int buttonPress = false;
int randomNumber;
int previousNo = 1;
int timePassed = 0;


// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 41, 42};
// set of drawers (array) holding the state of each led for a cycle
// the size (number of drawers) is automatically calculated at compile time based on number of drawers in the ledPins drawer set
byte ledStates[sizeof(ledPins)];

void setup()
{
  ...
  ...
}


void loop()
{
  ...
  ...
}

And in setup, add setting the pin to input

void setup()
{
    // Set button pin
    pinMode(buttonPin, INPUT);

    // other code from setup() in reply #17 is here
    ...
    ...
}

And in loop() we use your button code

void loop()
{
  // put a pieces of paper with LOW written on it in every drawer of the led states
  memset(ledStates, LOW, sizeof(ledStates));
  // a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
  byte onCount = 0;


  // Check if button is pressed
  if (digitalRead(buttonPin) == HIGH && buttonPress == false)
  {
    buttonPress = true;
  }

  if (buttonPress == true && timePassed <= timeShowRandom)
  {
    // I suppose that this is where you want to show a random led pattern
    ...
    ...
  }
  else if (buttonPress == true)
  {
    // no idea what needs to be done here
    ...
    ...
  }
  else
  {
    // Reset all output pins
    ...
    ...
  }
}

You need to give a proper description of the 3 parts; I understand the 'reset all pins' but not what the other parts exactly have to do. Let's start with the else

  else
  {
    // Reset all output pins
    for(byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
    {
      digitalWrite(ledPins[ledCnt], LOW);
    }
  }

And next where the loop() code from reply #17 fits in (if I'm correct).

  if (buttonPress == true && timePassed <= timeShowRandom)
  {
    // I suppose that this is where you want to show a random led pattern

  // select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
  while (onCount != NUMLEDS)
  {
    // open a random drawer in ledStates
    int randomNumber = random(0, sizeof(ledStates));
    // if the paper in there indicated that the led must be off (not on (HIGH))
    if (ledStates[randomNumber] != HIGH)
    {
      // take the LOW paper out and put HIGH paper in
      ledStates[randomNumber] = HIGH;
      // increment the number of drawers that have a piece of paper with HIGH on it
      onCount++;
    }
  }

  // display the leds
  // open each drawer in the ledStates drawer set
  for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
  {
    // debug
    Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
    // set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
    //  to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
    digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
  }
  // debug; print a new line
  Serial.println();

  // wait a bit
  delay(5000);


  }

Not tested, I hope it helps. I will not more than likely have time till tomorrow to help you further.

WOW lot of things so I tried to edit code as you said. Here is it. It can light up can pick "random" LEDs but button doesnt work. :confused: I tried what you said but I dont rly know what are you doing so I traing. And I must say thx for help Soon it will work. Or atleast I hope. Bad part of that is: Its not random. Every time I restart Arduino its same pattern if you understand. Can you fix it?

int timeShowRandom = 2500;      //this is how long picking will be
int timeShowDecision = 5000;    //this will set up how ling "winners" light will be ON
int timeBlink = 15;
int buttonPin = 3;              //Here I want to add 4 other options wich will set up how many "winners" will be choosed

int buttonPress = false;
int randomNumber;
int previousNo = 1;
int timePassed = 0;

 
// number of leds that need to be ON in every cycle
#define NUMLEDS 5
// drawer set with led pin numbers
const byte ledPins[] = { 6, 7, 8, 9, 10, 11, 12};
// set of drawers (array) holding the state of each led for a cycle
// the size (number of drawers) is automatically calculated at compile time based on number of drawers in the ledPins drawer set
byte ledStates[sizeof(ledPins)];




void setup()
{
  // adjus baudrate to needs
  Serial.begin(57600);

  // safety check; check if we try to fill more drawers with peices of paper than there actually are drawers
  if (NUMLEDS > sizeof(ledStates))
  {
    Serial.println("Can't have more leds ON than there are leds");
    // hang forever
    for (;;);
  }

  // open each drawer in the set of drawers, read the pin number
  // ledPins is the drawer set, ledCnt is the number of the drawer
  for (byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
  {
    // set the pin, found by reading the paper in drawer ledCnt, LOW
    digitalWrite(ledPins[ledCnt], LOW);

    // set the pin, found by reading the paper in drawer ledCnt, to be an output
    pinMode(ledPins[ledCnt], OUTPUT);
  }
}

void loop()
{
  // put a pieces of paper with LOW written on it in every drawer of the led states
  memset(ledStates, LOW, sizeof(ledStates));
  // a counter to count the number of pieces of paper with HIGH on it (leds that will be on)
  byte onCount = 0;

  if (buttonPress == true && timePassed <= timeShowRandom)
  {

     }  else
  {
    // Reset all output pins
    for(byte ledCnt = 0; ledCnt < sizeof(ledPins); ledCnt++)
    {
      digitalWrite(ledPins[ledCnt], LOW);
    }
  }
    // I suppose that this is where you want to show a random led pattern

  // select drawers in ledStates drawer set to fill with peice of paper with HIGH on it
  while (onCount != NUMLEDS)
  {
    // open a random drawer in ledStates
    int randomNumber = random(0, sizeof(ledStates));
    // if the paper in there indicated that the led must be off (not on (HIGH))
    if (ledStates[randomNumber] != HIGH)
    {
      // take the LOW paper out and put HIGH paper in
      ledStates[randomNumber] = HIGH;
      // increment the number of drawers that have a piece of paper with HIGH on it
      onCount++;
    }
  }

  // display the leds
  // open each drawer in the ledStates drawer set
  for (byte ledCnt = 0; ledCnt < sizeof(ledStates); ledCnt++)
  {
    // debug
    Serial.print(ledStates[ledCnt] == LOW ? " OFF" : " ON ");
    // set the pin found in the drawer (indicated by ledCnt) in the ledPins drawer set
    //  to the value on the piece of paper in the corresponding drawer (indicated by [i]ledCnt[/i]) in the ledStates drawer set
    digitalWrite(ledPins[ledCnt], ledStates[ledCnt]);
  }
  // debug; print a new line
  Serial.println();

  // wait a bit
  delay(5000);


  }