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);
}
}
I just added 4 more LEDs