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