const int loopDelay = 100; // Sets the overall loop speed in uS (higher is slower)
const int randMin = 0; // minimum and maximum star brightness
const int randMax = 255;
const long odds = 2999; // Odds to beat or we stay off
const long coinTosses = 3000; // Out of
long didIGetLucky; //
const int stars = 6; // Number of LEDs ("stars")
int star[] = {3, 5, 6, 9, 10, 11}; // Pin assignments of "stars"
int si = 0; // Star Index - ie Current "star" we're working with
int starBright[stars]; // "Star" brightness
int starTarget[stars]; // "Star" target brightness
void setup()
{
for (si = 0; si < stars; si++)
{
pinMode(star[si], OUTPUT);
}
}
void loop()
{
for (si = 0; si < stars; si++) // Loop for as many times as there are "stars"
{
if (starBright[si] == starTarget[si]) // If we've reached the previously set target then we need to set a new target
{
starTarget[si] = random(randMin, randMax); //Set new target brightness
didIGetLucky = random(coinTosses); // Did I beat the odds?
if (didIGetLucky < odds) // Usually not - so ...
{
starTarget[si] = 1; // Set a target of almost nothing (so it still gets something to do)
starBright[si] = 0; // Set current brightness to off
}
}
if (starBright[si] != starTarget[si]) // If star brightness change required then ...
{
if (starBright[si] < starTarget[si]) // Do this if we need to increase star brightness
{
analogWrite(star[si], starBright[si]); // Make the change
starBright[si]++; // Increase star brightness for next time
}
if (starBright[si] > starTarget[si]) // Do this if we need to decrease star brightness
{
analogWrite(star[si], starBright[si]); // Make the change
starBright[si]--; // Decrease star brightness for next time
}
}
delayMicroseconds(loopDelay); // Sets overall loop speed
}
}