Random light pattern code I found

So I found this code that will work perfectly for what I want but there are two issues. (well three but that issue is simply I'm learning this still and its hard when its just been 15 days).

  1. I have learned from Larryd (aka GOD) that code should ideally be in state machines. At least this code uses cases but its hard for me to edit/translate being so new which brings me to issue two.

  2. What I would like it to do is run these patterns for a random amount of time longer than a few seconds. Such as in the one listed as "One LED chases another LED front and back" I would like it to run for say 10 seconds one time and the next time the random generator in this code picks it to run I would like it run maybe 6 seconds. I know I need to define a variable with a counter (from my pseudocode perspective) that will cause one pattern to run longer or shorter but I can't figure out where especially if this needs rewritten.

Eventually I would want to add a button to take over from the random patterns and just make the lights breathe and then slowly chase but even that would be randomized for how long it would do those two but that's down the line. Hitting it again would go back into random patterns. I understand (I think) how to do that so much but...here is the code as I edited it to utilize 6 LEDS and not 18 as the header comment states.

On the plus side this is probably the last program I'm going to need help with as it is the last plastic model that will need something other than the other code I have (and understand more...I can read it, I understand what it does when I read it, but I can't modify well. Probably a good explanation is someone who can read Spanish, pick up what is written but if asked to add to it, it would be sloppy and not make much grammatical sense to a native speaker. )

EDITED SEE BELOW

If there is any relevance to a previous thread, please be courteous and provide a link, so that we can benefit from the answers that have already been given.

In many cases, starting a new thread about an ongoing issue in another thread, is opposite to the forum guidelines.

Please edit your code and remove all the empty lines, and format with ctrl-T or from the edit menu to provide standard code formatting. It is painful to read, as it is now.

This really isn’t relevant to any other thread but here is the code reformatted

Only reason I brought up the other thread was to state what was done for a different program. That thread is HERE

/* Arduino LED Patterns/Show Light with 18 LED */
//Setting Up the Pins for LEDs
void setup()

{
  for (int pin = 0; pin <= 7; pin++)

  {

    pinMode(pin, OUTPUT);

  }

}

//Main Loop - Switches different LED Patterns

void loop()


{

  int pickme = random(1, 20); // picks a random pattern of LED patterns

  switch (pickme)

  {

    case 1:

      onrun(random(20, 50));

      break;

    case 2:

      alternate(random(80, 100));

      break;

    case 3:

      offrun(random(20, 50));

      break;

    case 4:

      stack(random(30, 50));

      break;

    case 5:

      chaser(random(80, 100));

      break;

    case 6:

      fadealter(random(80, 100));

      break;

  }

}

void clearall()

{

  for (int pin = 2; pin <= 7; pin++)

  {

    digitalWrite(pin, LOW);

  }

}


void fillall()

{

  for (int pin = 2; pin <= 7; pin++)

  {

    digitalWrite(pin, HIGH);

  }

}


//One ON LED Run and all other OFF

void onrun(int delaytime)

{

  for (int pin = 2; pin <= 7; pin++)

  {

    clearall();

    digitalWrite(pin, HIGH);

    delay(delaytime);

  }

  for (int pin = 6; pin >= 2; pin--)

  {

    clearall();

    digitalWrite(pin, HIGH);

    delay(delaytime);

  }

}

//One OFF LED Run and all other OFF

void offrun(int delaytime)

{

  for (int pin = 2; pin <= 7; pin++)

  {

    fillall();

    digitalWrite(pin, LOW);

    delay(delaytime);

  }

  for (int pin = 6; pin >= 2; pin--)

  {

    fillall();

    digitalWrite(pin, LOW);

    delay(delaytime);

  }

}


//Flashing all LEDs ON and OFF

void flash(int delaytime)

{

  for (int i = 1; i <= 8; i++)

  {

    clearall();

    delay(delaytime);

    fillall();

    delay(delaytime);

  }

}


//Flashing LED in Fade manner

void fadeflash(int delaytime)

{

  clearall();

  int newdelay = delaytime / 5;

  for (int fade = 0; fade <= 255; fade += 5)

  {

    for (int pin = 2; pin <= 7; pin++)

    {

      analogWrite(pin, fade);

    }

    delay(newdelay);

  }



  for (int fade = 255; fade >= 0; fade -= 5)

  {

    for (int pin = 2; pin <= 7; pin++)

    {

      analogWrite(pin, fade);

    }

    delay(newdelay);

  }

}

//Alternatively Fade & Brightens

void fadealter(int delaytime)

{

  clearall();

  int newdelay = delaytime / 5;

  for (int fade = 0; fade <= 255; fade += 5)

  {

    for (int i = 2; i <= 6; i += 2)

    {

      analogWrite(i, fade);

    }

    for (int j = 3; j <= 7; j += 2)

    {

      analogWrite(j, 255 - fade);

    }

    delay(newdelay);

  }



  for (int fade = 255; fade >= 0; fade -= 5)

  {

    for (int i = 2; i <= 6; i += 2)

    {

      analogWrite(i, fade);

    }



    for (int j = 3; j <= 7; j += 2)

    {

      analogWrite(j, 255 - fade);

    }

    delay(newdelay);

  }

}

//Alternate Flash - Similar to Flash but alternate LEDs

void alternate(int delaytime)

{

  for (int n = 1; n <= 5; n++)

  {

    clearall();

    for (int i = 2; i <= 6; i += 2)

    {

      digitalWrite(i, HIGH);

    }

    delay(delaytime);

    clearall();

    for (int j = 3; j <= 7; j += 2)

    {

      digitalWrite(j, HIGH);

    }

    delay(delaytime);

  }

}

//Putting all LEDs one by one in a stack

void stack(int delaytime)

{

  int stack = 0;

  while (stack < 6)

  {

    for (int pos = 2; pos <= (7 - stack); pos++)

    {

      clearall();

      digitalWrite(pos, HIGH);

      drawstack(stack);

      delay(delaytime);

    }

    stack++;

  }

}

//Subfunction of the stack function

void drawstack(int stack)

{

  for (int n = 7; n > (7 - stack); n--)

  {

    if (n >= 2)

    {

      digitalWrite(n, HIGH);

    }

  }

}


//One LED chases another LED front and back

void chaser(int delaytime)

{

  int div = 40;

  int flashtime = delaytime / div;

  int A = random(2, 7);

  int B = random(7, 12);

  int Av = 1;

  int Bv = 1;

  if (random(0, 2))

  {

    Av *= -1;

  }

  if (random(0, 2))

  {

    Bv *= -1;

  }

  for (int time = 1; time < 100; time++)

  {

    if (abs(A - B) == 1 && (Av * Bv) == -1)

    {

      for (int f = 1; f < round(div / 4); f++)

      {

        clearall();

        delay(flashtime);

        digitalWrite(A, HIGH);

        digitalWrite(B, HIGH);

        delay(flashtime);

      }

      Av *= -1;

      Bv *= -1;

      A += Av;

      B += Bv;

    }

    else

    {

      clearall();

      digitalWrite(A, HIGH);

      digitalWrite(B, HIGH);

      A += Av;

      B += Bv;

      delay(delaytime);

    }

    if (A < 2)

    {

      A = 3;

      Av *= -1;

    }

    if (B > 7)

    {

      B = 6;

      Bv *= -1;

    }

    if (A >= B)

    {

      A = B - 1;

    }

  }

}

So: What I see as what I think can be done is adding another variable that is defined by a random number that would be in an for() loop to control when the case ends. My idea in the code is:

void loop()


{

  int pickme = random(1, 20); // picks a random pattern of LED patterns
  int k = 0;
  int randomnum = random(25,500); 
  
  switch (pickme)

  {

    case 1:
      for (k=0; k<=randomnum; k++){
      onrun(random(20, 50));
      }
      if (k >= randomnum)
      {
        randomnum = 0;
      }
      else
      {
      break;
      }

I’m fairly certain this is incorrect though. It would eliminate the rest of the cases below.

Oh well

santiago82:
Oh well

The forum has members around the globe - they are in different time zones, and half of them are asleep at any given time. Don’t expect instant responses.

Your idea is close but rather than a for() loop, just track elapsed time the the BlinkWithoutDelay example shows.

unsigned long startTime;
unsigned long duration;
int pickme;

void loop()
{
  if( millis() - startTime >= duration) {
    // time to pick a new pattern
    pickme = random(1, 20); // picks a random pattern of LED patterns
    duration = random(6,11) * 1000;  // seconds to run the pattern
    startTime = millis();
  }
  switch (pickme)
  {
    case 1:
      onrun(random(20, 50));
      break;
    case 2:
    //...
  }
}

You will have to initialize duration and pickme inside setup()

  {

    for (int i = 2; i <= 6; i += 2)

    {

      analogWrite(i, fade);

    }

is there a purpose to all this white space ?

blh64:
Your idea is close but rather than a for() loop, just track elapsed time the the BlinkWithoutDelay example shows.

unsigned long startTime;

unsigned long duration;
int pickme;

void loop()
{
  if( millis() - startTime >= duration) {
    // time to pick a new pattern
    pickme = random(1, 20); // picks a random pattern of LED patterns
    duration = random(6,11) * 1000;  // seconds to run the pattern
    startTime = millis();
  }
  switch (pickme)
  {
    case 1:
      onrun(random(20, 50));
      break;
    case 2:
    //...
  }
}




You will have to initialize duration and pickme inside setup()

It works but there was a weird delay at the start. I thought it was broken. But that's exactly what I was after :slight_smile:

santiago82:
It works but there was a weird delay at the start. I thought it was broken. But that's exactly what I was after :slight_smile:

You will have to initialize duration and pickme inside setup()

did you initialize those variable like I advised? I would advise intializating duration to a very small amount (0?) so you immediately pick a new pattern