LolShield

I am currently trying to run a loop that displays a 2X2 box moving horizontally across the LOLshield (a dark region moving in front of a lit background–see gif for clarification).
However, at the end of the loop the whole screen turns black. I would like all LEDs to remain lit when the loop repeats. How should I modify or change my code to make this transition?

#include <avr/pgmspace.h>  //AVR library for writing to ROM
#include <Charliplexing.h> //Imports the library, which needs to be
                           //Initialized in setup.

//Sets the time each frame is shown (milliseconds)
const unsigned int blinkdelay = 200;
int incomingByte;  // a variable to read incoming serial data

PROGMEM const uint16_t Box_2X2[][9] = 
{
 //2X2box 
{16383,16383,16383,16380,16380,16383,16383,16383,16383},
{16383,16383,16383,16377,16377,16383,16383,16383,16383},
{16383,16383,16383,16371,16371,16383,16383,16383,16383},
{16383,16383,16383,16359,16359,16383,16383,16383,16383},
{16383,16383,16383,16335,16335,16383,16383,16383,16383},
{16383,16383,16383,16287,16287,16383,16383,16383,16383},
{16383,16383,16383,16191,16191,16383,16383,16383,16383},
{16383,16383,16383,15999,15999,16383,16383,16383,16383},
{16383,16383,16383,15615,15615,16383,16383,16383,16383},
{16383,16383,16383,14847,14847,16383,16383,16383,16383},
{16383,16383,16383,13311,13311,16383,16383,16383,16383},
{16383,16383,16383,10239,10239,16383,16383,16383,16383},
{16383,16383,16383,4095,4095,16383,16383,16383,16383},
{16383,16383,16383,8191,8191,16383,16383,16383,16383},
{16383,16383,16383,16383,16383,16383,16383,16383,16383},
{18000}
};

void setup() 
{
  Serial.begin(9600);
  LedSign::Init(DOUBLE_BUFFER | GRAYSCALE);  //Initializes the screen
}
void loop()
{
  uint8_t Box_2X2_intensity=6;
  DisplayBox_2X2(Box_2X2_intensity);  
}  

void DisplayBox_2X2(uint8_t grayscale)
{
  boolean run=true;    //While this is true, the screen updates
  byte frame = 0;      //Frame counter
  byte line = 0;       //Row counter
  unsigned long data;  //Temporary storage of the row data
  unsigned long start = millis();

  while(run == true)
  {

    for(line = 0; line < 9; line++)
    {

      //Here we fetch data from program memory with a pointer.
      data = pgm_read_word_near (&Box_2X2[frame][line]);
      
      //Kills the loop if the kill number is found
      if (data==18000)
      {       
        run=false;        
      }
      
      //This is where the bit-shifting happens to pull out
      //each LED from a row. If the bit is 1, then the LED
      //is turned on, otherwise it is turned off.
      else for (byte led=0; led<14; ++led) 
      {
        if (data & (1<<led)) 
        {
          LedSign::Set(led, line, grayscale);
        }
        else 
        {
          LedSign::Set(led, line, 0);
        }
      }
    }

    LedSign::Flip(true);   
    
    unsigned long end = millis();
    unsigned long diff = millis() - start;
    if ( start && (diff < blinkdelay) )
        delay( blinkdelay - diff );
    start = millis();

    frame++;  
  }
}

Here is a GIF for clarification. http://makeagif.com/0yHWwa

It looks to me as though the problem is caused by the fact that the

while (run == true)

loop is not executed immediately the kill value of 18000 is found so write junk to the display Try replacing

      if (data == 18000)
      {
        run = false;
      }

with

      if (data == 18000)
      {
        return;
      }

If that works you can restructure the code to remove the need for the run variable completely.

That did the job! Thanks for the help UKHeliBob.