I hope the mods here don't mind (or it isn't frowned upon) to continue upon one of the topics from the read-only forums. I wasn't able to get an answer from elsewhere, so I thought this might be my only option (posting in the new forums about the old topic). The original topic is http://arduino.cc/forum/index.php/topic,7940.0.html.
I modified the code from this originally to utilize the TrueRandom library because I found when the program was powered up after being shut down it was actually not random at all but picking the exact same LEDs (meaning if I watched the pattern for 5 minutes and it was lighting LEDs 1, 4, 3, 2, 2, 5, etc, if I powered it down and powered it back up again it would light 1, 4, 3, 2, 2, 5, etc). I also tweaked the delay times to get shorter times between one LED blinking to another starting to blink. None of that is really the issue. Here's my modified code:
/*This sketch is intended to approximate the blinking of fireflies. It varies the delay between
blinks and varies the total blink time. I've used the random() function to vary which PWM output
is chosen for the next blink.
Hope you get some enjoyment out of it. I created it as a fun night light for my kids.
Chad Richardson -- Chad@ChadsCustomWood.net
*/
#include <TrueRandom.h> // Include TrueRandom Library
int value;
int pwmPin = 11; // light connected to digital pin 11-- I just chose an initial value
//int ledpin2 = 9;
long time=0;
int period = 500;
int i = 0;
long blink_delay = 1000; // 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(i=0; i<255;)
{
time = i;
value = abs(-127+127*cos(4*PI/period*i)); //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);
i++;
}
}
void choose_firefly()
{
pwmPin = pwmPins [TrueRandom.random (0, 6)];
}
Here's the issue I have.
The code works great as is. It blinks a random LED for a "random" length of time between two numbers and once it stops blinking it'll blink another LED. THAT is the problem. It only blinks one LED at a time! It waits for one LED to stop blinking before it'll initiate another LED blinking. The effect I'd like to have is that up to 3 LEDs could possibly blink at the same time (I say up to 3 because I'd generally not prefer that every LED start blinking at the same time constantly).
I know the problem lies with the "choose_firefly()" function. I've tried initializing pwmPin as an array but that gives me an error. I've tried eliminating choose_firefly from the code but that results in only the LED on pin 11 ever blinking (because pin 11 gets initialized first from the start of the code). I am unfortunately a novice at programming. I took an intro to Java course in college but that's as far as I've been able to get. I see the code above and I understand how it all functions together, but the problem is I'm just stuck on how to make more than one LED light up at a time without it looking like a disco room.
If anyone could offer advice and code on this I'd be very grateful. I'm trying to put this together as a little night light for my niece. The code is fine as is and I'll use it if need be, but I think it would more more realistic and better overall if more than one LED was capable of turning on at a time.
This is how the code functions currently: