Need help with fading in lights at the same time randomly

John, I want to thank you (again) for suggesting arrays; thanks to this idea I've got my head around them and reduced the latest version of my "twinkle" code to have dramatically less lines of code:

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

Now take a look at arrays of struct. The pin, brightness and target brightness could be grouped together that way.

1 Like

Thanks Bill. I've just had a quick read up on them now - I think I understand them.

My projects to date have probably all been too simple to benefit from them but they're something I'll definitely keep in mind as I build up to more complex things.

You are mistaken!

Its only a tiny slip from structs to the world of classes, inheritance and all that fun stuff. Once you get your head around the idea that you are the thing your writing code about. A whole new world will open up for you.

Or not.. Everyone's different.

-jim lee

I'm sure I am.

Not sure how many classes I'd have time for, but I definitely like the sound of inheritances! :wink: