Running Two Consecutive Loops with a Counter?

Hi All,

Would sincerely appreciate some advice. I'm slowly teaching myself basic Arduino programming. For my latest project I'm combinging an 11 I/O Larson Scanner (LEDs) with a 4 I/O flickering LED setup. Both are sample code from others that I'm slowly using to help myself learn. Both work fine individually. The flicker has a random delay at the end of the loop which pauses the larson scanner. I was thinking I should look to replace the random delay with a random integer/counter. The idea would be I add a variable that counts 1 per rotation through the loop at at the random delay mark (50/150) executes the flicker. I'm digging around for sample code to do it, and just really wanted to ask if I was on the right track or if there was a better way to do it.

Here is the sample code that I am thinking would work within another loop if I changed the random delay to a random integer that counted per rotation:

int LED_Pin0 = 0; // Use Pin 0 for the LED. (Any PWM Pin will work for this.) 
int howBright0;   // Intensity of the LED: [0 = Off], [255 = Brightly Lit] 
int LED_Pin1 = 1; // Use Pin 1 for the LED. (Any PWM Pin will work for this.) 
int howBright1;   // Intensity of the LED: [0 = Off], [255 = Brightly Lit] 
int LED_Pin2 = 2; // Use Pin 2 for the LED. (Any PWM Pin will work for this.) 
int howBright2;   // Intensity of the LED: [0 = Off], [255 = Brightly Lit] 
int LED_Pin3 = 3; // Use Pin 2 for the LED. (Any PWM Pin will work for this.) 
int howBright3;   // Intensity of the LED: [0 = Off], [255 = Brightly Lit] 

void setup()
{ 
  pinMode(LED_Pin0, OUTPUT); 
    pinMode(LED_Pin1, OUTPUT); 
    pinMode(LED_Pin2, OUTPUT);
    pinMode(LED_Pin3, OUTPUT); 
}

void loop() 
{ 
  howBright0 = random(128,255);     // Change brightness to something between 128 and 255 
    howBright1 = random(128,255);     // Change brightness to something between 128 and 255
    howBright2 = random(128,255);     // Change brightness to something between 128 and 255
    howBright3 = random(125,255);     // Change brightness to something between 128 and 255
  analogWrite(LED_Pin0, howBright0); // Illuminate the LED with the brightness picked 
  analogWrite(LED_Pin1, howBright1); // Illuminate the LED with the brightness picked 
  analogWrite(LED_Pin2, howBright2); // Illuminate the LED with the brightness picked
  analogWrite(LED_Pin3, howBright3); // Illuminate the LED with the brightness picked 
  delay(random(50,150));           // Makes LED seem to flicker when on for a random time 
}

There is a better way to do it. Read about millis() in one of the four locked posts at the top of this forum.

I don't see any kind of scanner in that code.

Learning about arrays might be useful at this stage.

This is the other sketch for the Larson Scanner that I'm attempting to integrate the code from above into. I can bring them together into one loop statement, but I need to change the way the flicker handles the delay, or it pauses the scanner.

//                             11
//                             1098765432    
const word dimbits[] = { 0b0000000000111100,  
0b0000000000111000,
0b0000000000110000, 
0b0000000000100000, 
0b0000000000100000, 
0b0000000000100000, 
0b0000000000100000, 
0b0000000000100000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 11
//                             1098765432                                 
0b0000000001111000,  
0b0000000001110000,
0b0000000001100000, 
0b0000000001100000, 
0b0000000001000000, 
0b0000000001000000, 
0b0000000001000000, 
0b0000000001000000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 23
//                             1098765432                                 
0b0000000011110000,  
0b0000000011100000,
0b0000000011000000, 
0b0000000011000000, 
0b0000000010000000, 
0b0000000010000000, 
0b0000000010000000, 
0b0000000010000000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 35
//                             1098765432                                 
0b0000000111100000, 
0b0000000111000000,
0b0000000110000000, 
0b0000000110000000, 
0b0000000100000000, 
0b0000000100000000, 
0b0000000100000000, 
0b0000000100000000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 47
//                             1098765432                                 
0b0000001111000000, 
0b0000001110000000,
0b0000001100000000, 
0b0000001100000000, 
0b0000001000000000, 
0b0000001000000000, 
0b0000001000000000, 
0b0000001000000000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 59
//                             1098765432                                 
0b0000001110000000, 
0b0000001100000000,
0b0000001100000000, 
0b0000001100000000, 
0b0000000100000000, 
0b0000000100000000, 
0b0000000100000000, 
0b0000000100000000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 71
//                             1098765432                                 
0b0000001110000000, 
0b0000001110000000,
0b0000000110000000, 
0b0000000110000000, 
0b0000000010000000, 
0b0000000010000000, 
0b0000000010000000, 
0b0000000010000000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 83
//                             1098765432                                 
0b0000001111000000, 
0b0000000111000000,
0b0000000011000000, 
0b0000000011000000, 
0b0000000001000000, 
0b0000000001000000, 
0b0000000001000000, 
0b0000000001000000, 
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000000000000000,  // 95
//                             1098765432                                 
                         0b0000000111100000, 
                         0b0000000011100000,
                         0b0000000001100000, 
                         0b0000000001100000, 
                         0b0000000000100000, 
                         0b0000000000100000, 
                         0b0000000000100000, 
                         0b0000000000100000, 
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,  // 107
//                             1098765432                                 
                         0b0000000011110000, 
                         0b0000000001110000,
                         0b0000000000110000, 
                         0b0000000000110000, 
                         0b0000000000010000, 
                         0b0000000000010000, 
                         0b0000000000010000, 
                         0b0000000000010000, 
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,  // 119
//                             1098765432                                 
                         0b0000000001111000, 
                         0b0000000000111000,
                         0b0000000000011000, 
                         0b0000000000011000, 
                         0b0000000000001000, 
                         0b0000000000001000, 
                         0b0000000000001000, 
                         0b0000000000001000, 
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,  // 131
//                             1098765432                              
                         0b0000000000111100, 
                         0b0000000000011100,
                         0b0000000000001100, 
                         0b0000000000001100, 
                         0b0000000000000100, 
                         0b0000000000000100, 
                         0b0000000000000100, 
                         0b0000000000000100, 
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,  // 143
//                             1098765432                              
                         0b0000000000011100, 
                         0b0000000000001100,
                         0b0000000000001100, 
                         0b0000000000001100, 
                         0b0000000000001000, 
                         0b0000000000001000, 
                         0b0000000000001000, 
                         0b0000000000001000, 
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,  // 155
//                             1098765432                              
                         0b0000000000011100, 
                         0b0000000000011100,
                         0b0000000000011000, 
                         0b0000000000011000, 
                         0b0000000000010000, 
                         0b0000000000010000, 
                         0b0000000000010000, 
                         0b0000000000010000, 
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000,
                         0b0000000000000000};  // 167
                         
unsigned long duration = 0;
unsigned long times_out = 2500; 
                    
int idx;
byte framepointer = 0;

void setup ()
{
  //pinMode(13,OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}
void loop ()
{
  // framepointer limit == number of frames * 12
  // it's the total of words in the dimbits[] array 
  for (framepointer = 0; framepointer < 168; framepointer = (framepointer + 12))
  // 0-11,12-33,24-35,36-47,48-59,60-71,72-83,84-95,96-107,
  // 108-119,120-131,132-143,144-155,156-167
  {
    for(duration=0; duration<times_out; duration++)
    // times_out is the number of
    // frame repetitions
    {
      for(idx = framepointer; idx < (framepointer + 12); idx++)
      {
        commitPORTs();
      }
    }
  }
  framepointer = 0;
  //delay(500);
}
void commitPORTs ()
{
  PORTD = dimbits[idx];
  PORTB = dimbits[idx]/256;
}