Sunday, January 27, 2013

LoL Shield: first own sketch

Next in LoL Shield history: a sort of "growth simulation":

  1. Place a particle (say: light a LED) at a random place on the shield.
  2. start a new particle at (0, 0).
  3. move this particle randomly; north, south, east or west.
  4. if the particle hits a placed one, fix it at its last free place.
  5. repeat from 2. until (0, 0) is occupied.
  6. then blink the result, clear the screen and start at 1.
If the movement crosses a border then move to the other side. Mathematically spoken: we are on a torus.

First a short video (sorry for the poor quality - it seems that google reduces quality extremely):



And here is the sketch:


/*  growth01 - simulating a sort of growth.
 *
 *  23 Jan 2013   danimath   created
 *
 **********************************************************************/

#include 
boolean used [14][9];      // array of "used" points
int     x, y;              // actual point
int     xold, yold;        // last point

const int BLINKNUM   =  10; // number of blinks
const int BLINKDELAY = 300; // ms; blink speed
const int MOVEDELAY  =  50; // ms; move speed

/* ****************************************************************** */
void setup ()
{
    LedSign::Init ();
    randomSeed (analogRead(0));
    for (int j = 0;   j < 14;   j++)
    {
        for (int k = 0;   k < 9;   k++)
        {
            used [j][k] = false;
        }
    }
    x = random (14);
    y = random (9);
    LedSign::Set (x, y, 1);
    used [x][y] = true;
    x = y = 0;
}

/* ****************************************************************** */

void loop ()
{
    int dir = random (4);
/*
 *  1. light my fire
 *  ================
 */
    for (int j = 0;   j < 14;   j++)
    {
        for (int k = 0;   k < 9;   k++)
        {
                LedSign::Set (j, k, used [j][k]);
        }
    }
/*
 *  2. get next point
 *  =================
 */
    xold = x;
    yold = y;
    switch (dir)
    {
        case 0:
            x--;
            if (x < 0) x = 13;
            break;
        case 1:
            x++;
            if (x > 13) x = 0;
            break;
        case 2:
            y--;
            if (y < 0) y = 8;
            break;
        case 3:
            y++;
            if (y > 8) y = 0;
            break;
    }
/*
 *  3. check, if used
 *  =================
 */
    if (used [x][y])
    {
        used [xold][yold] = true;
        for (int j = 0;   j < BLINKNUM;   j++)
        {
            LedSign::Set (0, 0, 0);
            LedSign::Set (xold, yold, 0);
            delay (MOVEDELAY);
            LedSign::Set (0, 0, 1);
            LedSign::Set (xold, yold, 1);
            delay (MOVEDELAY);
        }
        x = y = 0;
    }
    LedSign::Set (x, y, 1);
    delay (MOVEDELAY);
/*
 *  4. nothing more possible
 *  ========================
 */
    if (used [0][0])
    {
/*
 *      4.1. blink result
 *      -----------------
 */
        for (int i = 0;   i < BLINKNUM;   i++)
        {
            LedSign::Clear (0);
            delay (BLINKDELAY);
            for (int j = 0;   j < 14;   j++)
            {
                for (int k = 0;   k < 9;   k++)
                {
                    LedSign::Set (j, k, used [j][k]);
                }
            }
            delay (3 * BLINKDELAY);
        }
/*
 *      4.2. clear all
 *      --------------
 */
        for (int j = 0;   j < 14;   j++)
        {
            for (int k = 0;   k < 9;   k++)
            {
                used [j][k] = false;
            }
        }
        x = random (14);
        y = random (9);
        LedSign::Set (x, y, 1);
        used [x][y] = true;
        x = y = 0;
    }
}