I need help with random LEDs pick in someting like a Draw Machine HELP!!!!

Hey guys. Im working on homework and I have to build something like a draw machine. I have 40 leds and I have to make 5 profiles (only 1 out of 40), (only 2 out of 40), (only 3 out of 40), (only 4 out of 40), (only 5 out of 40). I want to use some buttons to "set" profile. I will use Arduino Mega 2560. Im working on prototype and Im close but I need help. Problem with my code is I cant choose profile so I have to put in there comand "light up random LED" multiple times and problem with that is most of the time only 3 or 4 LED light up because random number generator choosed for example 4 two times. Take a look at my code and help me to add "profile mode" and eleminate repetition so every time I choose amount of LEDs to light up they will. Guys I really need help do what ever you want and then send me code. Thank you for your time and help.


____________________________________ CODE _________________________________________


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 5 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 I used this to make more (5) LED light up but most of the
digitalWrite(random(6,13), HIGH); // Set random pin on time one or two out of five get same number in random
digitalWrite(random(6,13), HIGH); // Set random pin on number generator so only 4 of them will light up.
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 5 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);
}
}

While blue text is very sexy, it doesn't help somebody to select/copy/paste into an editor. Somebody that may wish to help you. Please use code tags and if you don't know what those are, read the post at the beginning of the forum.

You do realise that with 40 LEDs you'll quickly run out of juice to turn any but a handful of them on. Even with working code you may never know.

  pinMode(10, OUTPUT);                       ////Now I use only 5 LEDs but soon I will have to add up to 40
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);

Don't plan on copy/pasting 35 more pinMode() calls. Learn to use arrays!

   digitalWrite(random(6,13), HIGH); // Set random pin on           I used this to make more (5) LED light up but most of the
    digitalWrite(random(6,13), HIGH); // Set random pin on           time one or two out of five get same number in random
    digitalWrite(random(6,13), HIGH); // Set random pin on           number generator so only 4 of them will light up.
    digitalWrite(random(6,13), HIGH); // Set random pin on
    digitalWrite(random(6,13), HIGH); // Set random pin on
    delay(timeShowDecision); // For x seconds

Instead of hardcoding values for the random() limits, use const variables (yes, they are a misnomer) so that you only have to change one line of code, instead of hunting through ALL of the code looking for the magic numbers.

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

 
}

Recursion is neither necessary nor recommended.

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

NOTHING follows a } on the same line.

Sorry but there are a million things to fix in your code.

Like use int getRandNo() and the return statement.

I always have a million consts in my code.

Also, can we see a picture of a schematic.

There is schematic. Sir you can change what ever you want if you make it work. Please help me if you can. I just know nothing about coding we will have it next year in school.

arduino_sundh_random_leds1.png

Sadly I know nothing about that :confused: Can you somehow help me I know schematics but not coding. I dont even speak english that good I will understand what were you talking about. About power teoreticly there is only 1 LED active at the time but I can use arduino mega clones they can provide about 800mA per pin. So can you help me with coding? Do what ever you what if its work at the end.

PaulS:

  pinMode(10, OUTPUT);                       ////Now I use only 5 LEDs but soon I will have to add up to 40

pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);



Don't plan on copy/pasting 35 more pinMode() calls. Learn to use arrays!



digitalWrite(random(6,13), HIGH); // Set random pin on          I used this to make more (5) LED light up but most of the
    digitalWrite(random(6,13), HIGH); // Set random pin on          time one or two out of five get same number in random
    digitalWrite(random(6,13), HIGH); // Set random pin on          number generator so only 4 of them will light up.
    digitalWrite(random(6,13), HIGH); // Set random pin on
    digitalWrite(random(6,13), HIGH); // Set random pin on
    delay(timeShowDecision); // For x seconds



Instead of hardcoding values for the random() limits, use const variables (yes, they are a misnomer) so that you only have to change one line of code, instead of hunting through ALL of the code looking for the magic numbers.



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

}



Recursion is neither necessary nor recommended.



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



NOTHING follows a } on the same line.

I've reformatted your code

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 5 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           I used this to make more (5) LED light up but most of the
        digitalWrite(random(6, 13), HIGH); // Set random pin on           time one or two out of five get same number in random
        digitalWrite(random(6, 13), HIGH); // Set random pin on           number generator so only 4 of them will light up.
        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 5 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);
    }
}

For the time being, why don't you cut out those pins that you're not using.

Thank you so much that will hopefully help somebody to help me <3

DKWatson:
I've reformatted your code

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 5 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          I used this to make more (5) LED light up but most of the
        digitalWrite(random(6, 13), HIGH); // Set random pin on          time one or two out of five get same number in random
        digitalWrite(random(6, 13), HIGH); // Set random pin on          number generator so only 4 of them will light up.
        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 5 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);
    }
}



For the time being, why don't you cut out those pins that you're not using.

In this block of code:

    digitalWrite(random(6, 13), HIGH); // Set random pin on           I used this to make more (5) LED light up but most of the
    digitalWrite(random(6, 13), HIGH); // Set random pin on           time one or two out of five get same number in random
    digitalWrite(random(6, 13), HIGH); // Set random pin on           number generator so only 4 of them will light up.
    digitalWrite(random(6, 13), HIGH); // Set random pin on
    digitalWrite(random(6, 13), HIGH); // Set random pin on

you're getting random numbers without checking that the same one is not returned twice.

You could do something like this to make sure you are not trying to turn on the same LED twice:

    // light 5 leds
    for (int ledCount = 0; ledCount < 5; ledCount++)
    {
      int pinNo = random(6, 13);

      // get new pin # if already on
      while (digitalRead(pinNo) == HIGH ) pinNo = random(6, 13);

      digitalWrite(pinNo, HIGH);

    }

but it would probably impress your teacher more if you learned how to use arrays and stored unique randomly generated numbers in one.

Assumes LEDs connected on pins 1 to 40

// Fill this in. You can do that.

void setup() {
  // Fill in. You can do that too.
}

void loop() {
  if (digitalRead(theDrawButtonConnected) == HIGH) {
    byte rand = random(1, 40)
    digitalWrite(rand, HIGH);
    delay(yourDelay);
    digitalWrite(rand, LOW);
  }
delay(1);
}

Don't use serial