Hello Calado, and welcome to the forum.
If I understand you correctly, you want a program that will randomly blink 1 LED from a maximum selection of 10, and at some point, will randomly blink 2 or more ? If I have that right, then we can proceed.
I'm not sure of your level of understanding of the programming language that Arduino uses, but before we approach your code, I need to bring up a couple of points:
-
First of all, you need to read up on declaring variable types. I have no idea why people keep declaring pin allocations as int. The int type is a HUGE number between negative 32768 and positive 32768. Your pin allocations only use numbers between 2 and 11, so why not declare them as byte, which, in large
programs will save you a lot of memory. There's nothing wrong with using byte LED_1 = 2;
Also, you declared randomNumber as a long. I'm pretty sure it would have been better declared as a byte, it's only going to hold a small number < 255.
-
Read up on using Arrays. Typing all those pinMode allocations is the long way round. Look at the code
that I provided below, and see how declaring the pin allocations in an array is much shorter code, and I think easier to read. Also, there's no need to declare and initialise LEDs to pin allocations as you have. The Array and subsequent setup() code takes care of it. You'll see that, in the code I provide, there's no reference at all to LED_2 or similar. No need for it in this sketch.
-
User defined functions help. These are functions that you write yourself which are mainly used to execute a portion of code that is referenced often in a program. So if you find yourself using the same bit
of code over and over, think about writing a user function for it. You'll notice in the sketch I provide, I have written 2 user defined functions after the void() function. One handles a single LED being lit, the other handles 2 LEDs being lit at the same time. You can expand the amount, and content, of a user
function depending on your needs. If you wanted 3 random LEDs to come on at the same time, write a function for it and call the function from your main code.
-
I'm a bit confused as to why you have sensorValue and sensorPin in there. Maybe my brain
isn't working at full capacity as it's late and I'm very tired, but I got rid of them from the example sketch
I provided.
-
You can alter how fast the LEDs come on and go off via the various delay() functions in the user defined function calls. Play around with it. I don't usually use delay() unless I absolutely have to, but I
used it here to make the code as simple as I can.
Ok, now for the code. I just ran it on my breadboard and it works just fine. Please be sure that, if you use this sketch, your syntax is exactly the same. People often mistake a comma for a period, and I still mistake a square bracket with a curly one. The sketch does work, I'm running it right now, so if you get errors, check spelling and pay attention to the alerts at the bottom of your UI.
As a warning, I space out my code to that it's more readable, I hate bunched up code that makes it difficult to read and understand. Second, I comment / annotate my code heavily. Sorry about that, but it's the way I learned and I'm too old to get out of the habit. Play around with the code as you see fit, and enjoy your experience with the Arduino.
/* TITLE: Random Blinking
* A sketch to randomly blink a single LED from a selection of 10, then at random times to
* blink 2 LEDs together.
*
* CIRCUIT: Ten LEDs are consecutively connected to Arduino output pins 2 to 11 to the
* Positive side of each LED on a breadboard. The Negative side of each LED is connected to
* Ground via it's own resistor ( I used 320 ohm ). A common ground wire is connected from
* Arduino GND to a common ground on the breadboard.
*
* DESCRIPTION: The program obtains a random number, and based upon the value of that number,
* branches to one of two user defined functions. The first function will flash a random LED
* on and off depending on the value of delay(). The second function obtains 2 random numbers
* and flashes 2 LEDs together which are chosen via the values of the random numbers.
*/
// Declare Variables
byte randomNumber;
byte randomNumberTwo;
// Declare and initialise an Array and it's elements to represent the pin allocations
byte pinArray [ ] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
void setup()
{
randomNumber = 0; // not absolutely necessary, but I like to initialise in setup()
randomNumberTwo = 0;
for ( byte allocate = pinArray [ 0 ]; allocate < sizeof pinArray; allocate ++ )
{
pinMode ( pinArray [ allocate ], OUTPUT ); // loop through each pin number and put to OUTPUT
digitalWrite ( pinArray [ allocate ], LOW ); // initialise all the led pins to LOW / OFF
}
randomSeed ( analogRead ( A0 ) ); // initialise the Random Number Generator
} // end of setup()
void loop()
{
// Firstly, get a random number, arbitrarily between 1 and 12, whose result will determine
// whether just one or two ( or more if you expand the code ) LEDs are lit up
randomNumber = random ( 1, 13 ); // get a number between 1 and 12
if ( randomNumber <= 6 ) // if it's 6 or less
{
turnOnSingle(); // jumps to a user defined function written below the main loop
}
else if ( randomNumber > 6 ) // but if the number is greater than 6..
{
turnOnMulti(); // jumps to a user defined function written below the main loop
}
} // end of loop()
void turnOnSingle()
{
randomNumber = random ( 2, 12 ); // choose an output pin to which an led is connected
digitalWrite ( randomNumber, HIGH ); // and turn on the relevant LED
delay ( 200 ); // wait for half a second...
digitalWrite ( randomNumber, LOW ); // and turn it off again.
delay ( 200 );
} // end of turnOnSingle
void turnOnMulti()
{
randomNumber = random ( 3, 12 ); // choose an output pin to which an LED is
// connected. Leave pin 2 for now
randomNumberTwo = random ( 3, 12 ); // choose another one but leave pin 2 free
if ( randomNumberTwo == randomNumber ) // make sure the same number isn't called
{
randomNumberTwo = 2; // if it is, allocate pin 2 which was freed up
}
digitalWrite ( randomNumber, HIGH ); // turn on the relevant LEDs
digitalWrite ( randomNumberTwo, HIGH );
delay ( 200 ); // wait half a second
digitalWrite ( randomNumber, LOW ); // and turn them off again
digitalWrite ( randomNumberTwo, LOW );
delay ( 200 );
} // end of turnOnMulti