Merging 2 Sketches to control normal leds and strip at the same time

Hi,
Totally new to all this.
I have 2 sketches that both work separately that I would like to combine.

int leds[7] = {6,7,8,9,10,11,12};

void setup(){
  for (int jj; jj<sizeof(leds)/sizeof(int);jj++){
    pinMode(leds[jj],OUTPUT);
    delay(5);
  }
}

void loop(){
  digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],HIGH);
  delay(random(20,200));
  digitalWrite(leds[random(0,sizeof(leds)/sizeof(int))],LOW);
}

Also...

//***************************************************************
// Breathing effect
// Color shifts from hueA to hueB as it pulses.
//
// Set A and B to the same hue if you don't want the color to
// change.  Saturation for the high and low can also be set.
//
// Marc Miller, 2015
// Updated Aug 2020 - removed delay, added dim8_video
//***************************************************************

#include "FastLED.h"
#define LED_TYPE    WS2811  //WS2811, WS2812, WS2812B
#define COLOR_ORDER RGB
#define DATA_PIN 5 
#define NUM_LEDS 36
#define BRIGHTNESS 255
CRGB leds[NUM_LEDS];

static float pulseSpeed = 0.5;  // Larger value gives faster pulse.

uint8_t hueA = 180;  // Start hue at valueMin.
uint8_t satA = 255;  // Start saturation at valueMin.
float valueMin = 100.0;  // Pulse minimum value (Should be less then valueMax).

uint8_t hueB = 180;  // End hue at valueMax.
uint8_t satB = 255;  // End saturation at valueMax.
float valueMax = 255.0;  // Pulse maximum value (Should be larger then valueMin).

uint8_t hue = hueA;  // Do Not Edit
uint8_t sat = satA;  // Do Not Edit
float val = valueMin;  // Do Not Edit
uint8_t hueDelta = hueA - hueB;  // Do Not Edit
static float delta = (valueMax - valueMin) / 2.35040238;  // Do Not Edit


//---------------------------------------------------------------
void setup(){
  Serial.begin(115200);  // Allows serial monitor output (check baud rate)
  delay(2000);  // Startup delay
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show();
  Serial.println("Setup done.  \n");

}


//---------------------------------------------------------------
void loop(){
  float dV = ((exp(sin(pulseSpeed * millis()/2000.0*PI)) -0.36787944) * delta);
  val = valueMin + dV;
  hue = map(val, valueMin, valueMax, hueA, hueB);  // Map hue based on current val
  sat = map(val, valueMin, valueMax, satA, satB);  // Map sat based on current val

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue, sat, val);

    // You can experiment with commenting out these dim8_video lines
    // to get a different sort of look.
    leds[i].r = dim8_video(leds[i].r);
    leds[i].g = dim8_video(leds[i].g);
    leds[i].b = dim8_video(leds[i].b);
  
  FastLED.show();

  }
} // end_main_loop

I've tried several different thing, none of which have worked.
Any help would be much appreciated.

A Google search for "merging arduino sketches." yields over 230000 hits.

Indeed, and I've looked, but every method I've tried yields compile errors of one sort or another. Also I did say I'm new to all this. My only programming experience is HTML and a little Javascript. I'm Stuck.

Then post the code that gave you the errors and post the errors. Then we can help you.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Thanks, I'll have another try later and post my errors messages.
I know I'll kick myself when I figure it out.

One obvious problem is that both sketches have an object called leds. It might help to change that in the smaller one and fix the resulting compiler errors before trying to merge.

Okay...
I've manged to merge the sketches and they work fine(ish) together.

int ledsA[6] = {6, 7, 8, 9, 10, 11};
#include "FastLED.h"
#define LED_TYPE    WS2811  //WS2811, WS2812, WS2812B
#define COLOR_ORDER RGB
#define DATA_PIN 5
#define NUM_LEDS 36
#define BRIGHTNESS 255
CRGB leds[NUM_LEDS];
static float pulseSpeed = 0.5; // Larger value gives faster pulse.
uint8_t hueA = 180;  // Start hue at valueMin.
uint8_t satA = 255;  // Start saturation at valueMin.
float valueMin = 100.0;  // Pulse minimum value (Should be less then valueMax).
uint8_t hueB = 180;  // End hue at valueMax.
uint8_t satB = 255;  // End saturation at valueMax.
float valueMax = 255.0;  // Pulse maximum value (Should be larger then valueMin).
uint8_t hue = hueA;  // Do Not Edit
uint8_t sat = satA;  // Do Not Edit
float val = valueMin;  // Do Not Edit
uint8_t hueDelta = hueA - hueB;  // Do Not Edit
static float delta = (valueMax - valueMin) / 2.35040238;  // Do Not Edit
void setup() {
  setup1();
  setup2();
}
void loop() {
  loop1();
  loop2();
}
void setup1() {
  for (int jj; jj < sizeof(ledsA) / sizeof(int); jj++) {
    pinMode(ledsA[jj], OUTPUT);
  }
}
void loop1() {
  digitalWrite(ledsA[random(0, sizeof(ledsA) / sizeof(int))], HIGH);
  delay(random(20, 200));
  digitalWrite(ledsA[random(0, sizeof(ledsA) / sizeof(int))], LOW);
}
void setup2() {
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
}
void loop2() {
  float dV = ((exp(sin(pulseSpeed * millis() / 2000.0 * PI)) - 0.36787944) * delta);
  val = valueMin + dV;
  hue = map(val, valueMin, valueMax, hueA, hueB);  // Map hue based on current val
  sat = map(val, valueMin, valueMax, satA, satB);  // Map sat based on current val
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue, sat, val);
    FastLED.show();
  }
}

No error codes, yay!
However...
The "Breathe" effect on the WS2812B Strip is not running as smoothly as it was, it's a lot more jerky.
Is this likely to be anything I've done wrong in the code?

delay(random(20, 200)); Oops

TheMemberFormerlyKnownAsAWOL:

delay(random(20, 200));

Oops

Okaaaayyyy, I know not what to do about that. Any hints?

Blink without delay in the IDE's examples.

TheMemberFormerlyKnownAsAWOL:
Blink without delay in the IDE's examples.

I have played with that but don't know how to make it random with that method

So, I changed

delay(random(20, 200));

To...

random(20, 200);

This seems to have helped, was this the right thing to do?

But now there is no delay.

If that's what you wanted, job done. Solved.

TheMemberFormerlyKnownAsAWOL:
But now there is no delay.

If that's what you wanted, job done. Solved.

Well... It works, so yeah. :slight_smile: Lol
I'd love to hear any improvements to the code that you may be able to suggest.
It's a slow process, but I am learning.

I'd love to hear any improvements to the code

If you make changes to your code, post the latest version so that we can keep up.

groundFungus:
If you make changes to your code, post the latest version so that we can keep up.

I will

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.