Firefly/ Random effect code for LEDs (how to control more LEDs)

Hi!
I'm a beginner playing around with Arduino for a school project. I have a code that works for lighting LEDs randomly but for now it only controls 5. I need to control 10. I don't know how to change the code without losing it's randomness. Could really use some help. :slight_smile:

The code:

< int value;
int pwmPin = 3; // light connected to digital pin 11-- I just chose an initial value
//int ledpin2 = 9;
long time=0;
int period = 500;
int x = 0;
long blink_delay = 300; // these must be declared as long due to the random() operation
long blink = 3;
long random_led = 55;
const byte pwmPins [] = {3, 5, 6, 9, 10, 11};

void setup()
{ //nothing to setup
}

void loop()
{
choose_firefly();
fade();

blink_delay = random(50, 500);
delay(blink_delay);
blink = random(2, 12); //Orginal number wasd (2, 5)
//Using 10 instead of 5 gives longer
//possible blink times

}
void fade()
{
for(x=0; x<255;)
{
time = x;
value = abs(-127+127cos(4PI/period*x)); //the -127 value shifts the cosine curve negative with a zero initial value; abs shifts everything positive
analogWrite(pwmPin, value); // sets the value (range from 0 to 255)
delay(blink);
x++;
}
}

void choose_firefly()
{
pwmPin = pwmPins [random (0, 6)];

} >

Thanks!

Hi,

Please edit your post above and put in code tags around your code using the </> icon. This makes it easier for us to read.

You say that it controls 5 leds but it seems to me that it controls 6, their pin numbers are in this array:

const byte pwmPins [] = {3, 5, 6, 9, 10, 11};

However, this line picks a random number which could be 6, causing an unintended pin to be used because the array of pin numbers does not have an index for 6, only 0 to 5:

  pwmPin = pwmPins [random (0, 6)];

Your firefly effect uses PWM pins, and there are only 6 of these on an Uno which is what i assume you are using?

So to have 10 leds you will have to do things in a slightly different way. Right now, your leds are connected between a PWM pin and ground (via a series resistor). You will need to connected them between two pins. One pin must be a PWM pin and you can use the same PWM pin for all 10 leds. The other pin will be one of 10 digital pins which can be any Arduino digital pin. These pins will "sink" the current from the led instead of connecting the led to ground. You will still need a series resistor.

Can you draw a circuit diagram showing how you would connect everything up as i described? Then i can help you with the code changes.

Paul

Dear Paul,

Thank you for your answer.

The thing is, since I am such a beginner I don't know much, bordering on nothing.

From what i gathered, there will be a main supply from the 9PWM pin, that will then have a resistor (is330ohms enough?) that will connect to a chain of LEDs? The end of which will be connected to the 8 digital pin. Is that correct?
If so, may I just ask, how can I control them as separate entities when they are connected together (so that they flash randomly)? Or am I not understanding anything at all?

I am attaching a drawing, not sure if correct of how I understood what you said. Your help would be greatly appreciated as I have no idea what to do. Also in terms of connecting all of it up...

Looking forward to your answer.
And thank you again for all of your help!

All the best,
Julia

Hi Julia. I will be delighted to help but please do as i asked because it is forum rules which you should have read before posting.

Paul

Thanks!
I did a new drawing. Is this what you had in mind?

Please let me know what else I can do.

oftheforest:
I did a new drawing. Is this what you had in mind?

No, that's not it. Read back.

Right, here's what you do:

Go and read the instructions, then go back and modify your first post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

Attachments such as images do not work properly on this forum so where at all possible, you should put information in the posting itself using the appropriate links. There are tricks for making them more-or-less visible; here is your second drawing:


That clearly will not work as you would need vastly more voltage than the Arduino is capable of providing to operate that number of LEDs in series (20 V even for red LEDs, more for the other colours) and connecting them between two outputs provides less voltage, not more.

Sounds like you need to read up on basic electronics before embarking on Arduinox.

Feeling generous. And please, hold your applause and gracious statements of my masterfully drawn schematic. So thank you, and please tip your moderator when hitting the back button.

Either way is fine, but I like the first one as it takes less risks.