LED custom (win) machine

I yester day I was asking to help with my homework. My problem is still same. I dont know how to code and I trying to learn how to code but its hard. So I got 40LEDs I have to randomly choose 5 out of 40. They cant be the same and thats my problem. I have prototype with 7 LEDs and Im picking 5 of them but sadly most of the time only 3 or 4 light up. So I need help with adding something in the code. Any with idea please help and if you can edit code yourself because I dont even speak english that well to understand you not to mention that I can not code.

The task is. After pressing the button, the random sequence selects 5 winners and can not be repeated. (So there will always be 5 lights on)

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

Let me show you a magic trick: pick a card.

Now that you have that card in your hand, what is the probability that the next card you pick will have the same value?

Your code needs to keep track of which cards have already been 'picked'.

I know sir. Sadly I dont know how to do it. Can you help me?

Sadly I dont know how to do it.

You don't know how to create a variable with suitable scope? Then, programming as a career is probably not in your future.

Mikey999:
I know sir. Sadly I dont know how to do it. Can you help me?

Say you have an array of picked cards. Then with each card you picked, check in the array, if the card is already in there, discard the card and repeat. Otherwise, store the card in the array. After you finish picking the card (filling up the array), you are done.

You can read the digital pins in order to figure out if they are on or off.

byte getRandomPin()
{
  byte res = random(x, y);
  if (digitalRead(res)) return getRandomPin();
  return res;
}

This would require you to create an array that keeps track of the "on" pins in order to turn them off after new pins have been turned on.

You can read the digital pins in order to figure out if they are on or off.

You can do it without recursion, too. Be better that way.

PaulS:
You don't know how to create a variable with suitable scope? Then, programming as a career is probably not in your future.

arduino_new:
Say you have an array of picked cards. Then with each card you picked, check in the array, if the card is already in there, discard the card and repeat. Otherwise, store the card in the array. After you finish picking the card (filling up the array), you are done.

Danois90:
You can read the digital pins in order to figure out if they are on or off.

byte getRandomPin()

{
  byte res = random(x, y);
  if (digitalRead(res)) return getRandomPin();
  return res;
}




This would require you to create an array that keeps track of the "on" pins in order to turn them off after new pins have been turned on.

PaulS:
You can do it without recursion, too. Be better that way.

Guys Im not programer and They will teach me basic in school after holidays. I need to make this done. Can you edit that so "ON pin tracker" will be inside my code? I do understand what code do and I can edit parts but I have no idea where to add that "tracker" part. Guys if you edit code for me I will be so much happy. Thank you

Study, learn, do.. Don't expect ppl to make your homework, you have gotten it for a reason.

Its not homework from school its from "camp" where I will work. They think Im some kind of engineer if I know basic of arduino. But I dont know and I dont want them to be disappointment :confused:

Then you should go to gigs and collabs and pay someone to do it for you :slight_smile:

Mikey999:
Its not homework from school its from "camp" where I will work. They think Im some kind of engineer if I know basic of arduino. But I dont know and I dont want them to be disappointment :confused:

Then instead of trying to fool them by turning in our work you should go tell them that it is beyond you at the moment. If you turn in our work then they'll really think you know what you're doing and they'll expect you to turn in more and more of our work. We won't just sit here and do all your work for free while you get paid. That is totally unfair to us.

So go tell the boss that this is beyond your ability and you need more classes or time. But don't just cheat. That's despicable.