Combing Sketches

I'm trying to combine two sketches for my LED coat.
I'm new to programing and found these two sketches.
I've tried combining them and keep getting errors.

It has 20 leds connected to an arduino lillypad. I want the first sketch to run, then the 2nd to run for a bit (couple minutes?) then back to the first. Here are the two sketches:

Sketch 1: LED Chase · GitHub

Sketch 2: Random LED Fade · GitHub

Thank you so much!!!!

  1. Both links you provided do not work.
  2. You said you created both programs, if so, why are you asking us to perform this work?

You said you created both programs, if so, why are you asking us to perform this work?

I'm new to programing and found these two sketches.

Combining sketches

OP changed post after my comment.

Now you just need to decide how many times to call loop1() and how many times to call loop2(). I put in 10 for each as a starting point. Adjust to taste.

Note: The Chase sketch is using Pin 20, which I don’t think exists.
Note: Because setup2() starts a timer ISR you may have to stop the MsTimer2 clock while loop1() is running, otherwise the ISR and loop1() will probably both try to control the LEDs. Be sure to turn the timer on again before running loop2().

#include <MsTimer2.h>

const byte BASE = 0 ; // the I/O pin for the first LED
const byte NUM = 20; // number of consecutive I/O pins for LEDs
const byte OUT_MIN = BASE; // Lowest pin used
const byte OUT_MAX = BASE + NUM - 1;  // Highest pin used

byte luminance[NUM];
byte lumi[NUM] = {};

void setup()
{
  setup1();
  setup2();
}

void loop()
{
  for (int i = 0; i < 10; i++)
    loop1();

  for (int i = 0; i < 10; i++)
    loop2();
}

////////////// LED Chase
#define SPEED_MS  100


int ledDelay(25);// Delay
int ledDelay2(10);// 2nd Delay

void setup1()
{
  for (int i = BASE; i < BASE + NUM; i++)
  {
    pinMode(i, OUTPUT); // set I/O pins as output
  }

}
void loop1()
{

  for (int j = 1; j < OUT_MAX; j++)
  {
    for (int i = BASE; i < BASE + NUM - j; i ++)
    {
      digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one.
      delay(ledDelay2);
      digitalWrite(i + 1, HIGH);
      digitalWrite(i, LOW);
      delay(ledDelay2); // delay
    }
    
    for (int i = OUT_MAX - j; i < OUT_MAX; i++)
      digitalWrite(i, HIGH);
  }

  for (int i = BASE + NUM; i >= BASE; i --)
  {
    digitalWrite(i, LOW);
    delay(ledDelay * 2);
  }
  
  delay(ledDelay * 3);
  int total = BASE * 2 + NUM - 1;

  for (int i = BASE; i < BASE + NUM / 2; i ++)
  {
    digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one
    digitalWrite(total - i, HIGH);
    delay(ledDelay); // delay
    digitalWrite(i, LOW);
    digitalWrite(total - i, LOW);
  }
  for (int i = BASE + NUM / 2; i >= BASE; i --)
  {
    digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one
    digitalWrite(total - i, HIGH);
    delay(ledDelay); // delay
    digitalWrite(i, LOW);
    digitalWrite(total - i, LOW);
  }


  for (int i = BASE; i < BASE + NUM / 2; i ++)
  {
    digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one
    digitalWrite(total - i, HIGH);
    delay(ledDelay); // delay
  }
  for (int i = BASE; i < BASE + NUM / 2; i ++)
  {
    digitalWrite(i, LOW); // set I/O pins as “high”, turn on LEDs one by one
    digitalWrite(total - i, LOW);
    delay(ledDelay); // delay
  }

  for (int i = BASE + NUM / 2; i >= BASE; i --)
  {
    digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one
    digitalWrite(total - i, HIGH);
    delay(ledDelay); // delay
  }

  for (int i = BASE + NUM / 2; i >= BASE; i --)
  {
    digitalWrite(i, LOW); // set I/O pins as “high”, turn on LEDs one by one
    digitalWrite(total - i, LOW);
    delay(ledDelay); // delay
  }
  for (int i = BASE; i < BASE + NUM; i ++)
  {
    digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
  }
  delay(ledDelay * 5);
  for (int i = BASE; i < BASE + NUM; i ++)
  {
    digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one by one.
  }
  delay(ledDelay * 5);
  for (int i = BASE; i < BASE + NUM; i ++)
  {
    digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
  }
  delay(ledDelay * 5);
  for (int i = BASE; i < BASE + NUM; i ++)
  {
    digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one by one.
    //Serial.println(i);// Show val variable.
    //delay(ledDelay); // delay
  }
  delay(ledDelay * 5);
  for (int i = BASE; i < BASE + NUM; i ++)
  {
    digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
  }
  delay(ledDelay * 5);
  for (int i = BASE; i < BASE + NUM; i ++)
  {
    digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one by one.
  }
  delay(ledDelay * 5);
  for (int i = 0; i < 3; i++)
  {
    for (int i = BASE; i < BASE + 2; i ++)
    {
      digitalWrite(i, HIGH);
      digitalWrite(i + 2, HIGH);
      digitalWrite(i + 4, HIGH);
      digitalWrite(i + 6, HIGH);
      digitalWrite(i + 8, HIGH);
      digitalWrite(i + 10, HIGH);
      digitalWrite(i + 12, HIGH);
      digitalWrite(i + 14, HIGH);
      digitalWrite(i + 16, HIGH);
      digitalWrite(i + 18, HIGH);


      delay(ledDelay * 15); // delay
      digitalWrite(i, LOW);
      digitalWrite(i + 2, LOW);
      digitalWrite(i + 4, LOW);
      digitalWrite(i + 6, LOW);
      digitalWrite(i + 8, LOW);
      digitalWrite(i + 10, LOW);
      digitalWrite(i + 12, LOW);
      digitalWrite(i + 14, LOW);
      digitalWrite(i + 16, LOW);
      digitalWrite(i + 18, LOW);

    }

  }
}


//////////// LED Fade

void setup2()
{
  int i;

  for (i = OUT_MIN; i <= OUT_MAX; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }

  for (i = OUT_MIN; i <= OUT_MAX; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }

  for (i = OUT_MIN; i <= OUT_MAX; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }

  for (i = OUT_MIN; i <= OUT_MAX; i++)
  {
    digitalWrite(i, HIGH);
    delay(50);
  }
  for (i = OUT_MIN; i <= OUT_MAX; i++)
  {
    digitalWrite(i, LOW);
    //delay(50);
  }
  delay(100);

  MsTimer2::set(1, int_pwm);
  MsTimer2::start();
}

void int_pwm()
{
  static int f_wait[20];
  static int f_max_time[20];
  static int f_speed[20];
  static int f_ct[20];
  static long f_interval[20];

  static int ch;

  for (ch = OUT_MIN; ch <= OUT_MAX; ch++)
  {
    if (f_wait[ch] == 0)
    {
      f_wait[ch] = f_speed[ch] + 1;

      if ( (f_ct[ch] < 255) )
        if (luminance[ch] != 255)
          luminance[ch] ++;

      if (f_ct[ch] == 255)
      {
        if (f_max_time[ch] == 0)
          f_max_time[ch] = 0; // On Max Time
        else
          f_max_time[ch] --;
      }

      if ( (f_ct[ch] > 255) && (f_ct[ch] < 511) && (f_max_time[ch] == 0))
        if (luminance[ch] != 0)
          luminance[ch] --;

      if (f_ct[ch] >= 511)
      {
        if (f_interval[ch] == 0)
        {
          f_interval[ch] = 10 * random(100); // fading seed
          f_speed[ch] = random(10); // fading speed
          f_ct[ch] = 0;
        }
        else
          f_interval[ch] --;
      }
      else
        f_ct[ch] ++;
    }
    f_wait[ch] --;
  }
}

void loop2()
{
  static byte counter = 0;
  static boolean prev_off[20];
  int i;

  for (i = OUT_MIN; i <= OUT_MAX; i++)
  {
    if (lumi[i] == 0)
    {
      if (prev_off[i] == HIGH)
      {
        digitalWrite(i, LOW);
        prev_off[i] = LOW;
      }
    }
    else
      lumi[i] --;
  }

  counter --;

  if (counter == 0)
  {
    for (i = OUT_MIN; i <= OUT_MAX; i++)
    {
      if (luminance[i])
      {
        digitalWrite(i, HIGH);
        prev_off[i] = HIGH;
      }
      lumi[i] = luminance[i];
    }
  }
}

@johnwasser That was very kind of you to sort the problem out. However, I do question you doing all the work.

To many times I see this, where a complete solution is provided and while the OP gets working code, it does nothing to help the user in future projects. What the OP has learned is someone will provide them with free code with little to no work on thier part.

Thank you so much John! I sincerely appreciate the help. Romonaga- I've been fussing over this code for a couple months now. I've been adjusting it and trying to work on a solution. I came here as a last resort. I do appreciate your thoughts & you are correct- I need to work harder to learn/understand this code. I have a basic grasp, but I have a long way to go. I will look over what John has done and hopefully this will help to get me that much closer.
-Corey

surfin009:
Thank you so much John! I sincerely appreciate the help. Romonaga- I've been fussing over this code for a couple months now. I've been adjusting it and trying to work on a solution. I came here as a last resort. I do appreciate your thoughts & you are correct- I need to work harder to learn/understand this code. I have a basic grasp, but I have a long way to go. I will look over what John has done and hopefully this will help to get me that much closer.
-Corey

Yep, spending several months banging head is no fun. At least you made an attempt, many here see a cool idea and want it, but have no desire to learn. I see you do not fall into that category.

@johnwasser

The code is definitely getting hung up on the first sketch for some reason. The only way it’ll move onto the “random Blink” sketch 2 is by making

for (int i = 0; i < 0; i++)

I’ve tried switching the sketches. So the Random Blink is now sketch 1 and the chase sketch is 2nd. It now skips the first sketch and heads towards the 2nd one. Weird. I’m reading up on how to add a MSTimer interrupt.

A friend suggested making it print things at different parts so that I could find the loop it gets trapped in? Something like Serial.Print() ??

surfin009:
The code is definitely getting hung up on the first sketch for some reason. The only way it’ll move onto the “random Blink” sketch 2 is by making

for (int i = 0; i < 0; i++)

Try commenting out the calls to setup2() and loop2(). Does the first sketch then work as before? If not, something is wrong with the sketch1 code.

void setup()
{
  setup1();
//  setup2();
}


void loop()
{
  for (int i = 0; i < 10; i++)
    loop1();


//  for (int i = 0; i < 10; i++)
//    loop2();
}

If I comment out Setup1() and Loop1() then setup 2 and loop 2 play. If I coment out setup2 and Loop 2 then Setup1 and Loop 1 Play! So thats good! Still dont know why its getting hung up on setup1 and not going to setup2.

surfin009:
If I comment out Setup1() and Loop1() then setup 2 and loop 2 play. If I coment out setup2 and Loop 2 then Setup1 and Loop 1 Play! So thats good! Still dont know why its getting hung up on setup1 and not going to setup2.

I thought you said it was getting stuck on loop1()? If that is the case, there might be something in setup2() that is causing loop1() to hang.

Hi,

My suggested approach to combining sketches, which has worked for several people is here:

https://arduinoinfo.mywikis.net/wiki/CombiningArduinoSketches