Help combine 2 Sketches

I'm trying to merge two sketches together, One that blinks Led's, Another that controls a lift system that uses buttons to control motors that run until a limit switch is pressed and then stops.

The first sketch that I wrote alternates a number of Led's and runs an led matrix that is set to blink all Led's Randomly and quickly.

The Second Sketch that I did not, is for a "lift System" that is designed for a life-size R2-D2 and its functions are better detailed in THIS Post.

(Tho, my problem has nothing to do with the question in the above link)

BOTH systems have been built and Are working PERFECTLY on their own.

I'm trying to combine them onto ONE Arduino Mega, and when I do, the "lift" portion of the sketch still works, However, the Led section Is showing some problems with the Lights Not working exactly (tho close) as scripted, AND the bigger problem is that the Led matrix is now only flashing one light every loop and not acting like the very quick random sequence I am looking for.

I had a problem with this same sketch (just the LED sketch) and the matrix, when I had written the code using DELAY(s), and I think what is happening, is something in the "Lift" Sketch is acting as a delay OR perhaps it is the Length of the sketch is "delaying" the "logic lights" portion of the Sketch and acting as if I had used delays.

Can anyone Let me know if I'm on the right track in my thinking? and where I may have gone wrong? Or maybe suggest a better way to run the Max7219 matrix to achieve the same effect and NOT be effected by the second "lift" sketch.

Thank you in advance!

I'll post the 2 sketches in another Post (went over 9000)

Dome_lift_system_changes.ino (28.3 KB)

DomeLightsNormal.ino (9.21 KB)

Apparently both sketches are well over 9000 characters, But I have attached them above.

the section I'm having the major issue with is this Led matrix section of the code:

// ***** Logic Lights NORMAL Operation*****
void LogicLightsNormal() {
  randNumberX = random(0, 8);
  randNumberY = random(0, 8);
  lc.setLed(0, randNumberX, randNumberY, true); // turns on LED at col, row

  lc.setLed(0, randNumberX, randNumberY, false); // turns on LED at col, row

}

Doesn't that just turn on one random LED and immediately turn it off again?

What do you WANT it to do? Perhaps you could turn on one LED half the time and turn OFF one LED half the time:

// ***** Logic Lights NORMAL Operation*****
void LogicLightsNormal()
{
  randNumberX = random(8);
  randNumberY = random(8);
  
  lc.setLed(0, randNumberX, randNumberY, random(1)); // turns LED on or off at col, row
}

You can refer to how to combine two sketches

johnwasser:
Doesn't that just turn on one random LED and immediately turn it off again?

What do you WANT it to do? Perhaps you could turn on one LED half the time and turn OFF one LED half the time:

I want it to randomly blink all the LEDs in the matrix, rapidly one after another. so it looks as if all of them are randomly on or off, or cycling thru. Something like THIS

Which is what the above code does exactly, but it would seem whenever you put a delay in any part of the code, OR something is acting upon it like a delay it slows it down to a crawl, where it looks like only one led is on, waits then turns on another random one.

BubbleHockey:
Which is what the above code does exactly, but it would seem whenever you put a delay in any part of the code, OR something is acting upon it like a delay it slows it down to a crawl, where it looks like only one led is on, waits then turns on another random one.

Yes, that’s what delay does. It blocks execution of any other code until the delay is over.

You’ll need to do one of the following...
a) randomise either turning a led on or off a led (as already suggested)
b) invert the state of a random led
c) turn on random led on, turn another random led off
c) keep track of which LEDs you’ve turned on, let’s say 20 of them. Then after that turn of the first one you turned on, and repeat the turn off process for each successive new led you turn on.

Each of these will probably produce slightly different effects.

pcbbc:
Yes, that’s what delay does. It blocks execution of any other code until the delay is over.

You’ll need to do one of the following...
a) randomise either turning a led on or off a led (as already suggested)
b) invert the state of a random led
c) turn on random led on, turn another random led off
c) keep track of which LEDs you’ve turned on, let’s say 20 of them. Then after that turn of the first one you turned on, and repeat the turn off process for each successive new led you turn on.

Each of these will probably produce slightly different effects.

for "a)" Is that not what is happening VIA the Max7219?

I'm not sure how to do any of that, as I really pieced the code together thru different examples and have a fair understanding on what I've written but Im still a bit lost when it comes to reading libraries.

Also would the above suggestions not ALSO be interrupted by a delay?
I guess the larger question, is What is it in the Larger Lift code that is causing the delay? as it is written with no delays, other then the one in the setup, where I don't think it would have any effect on the Loop.

Would a switch case do anything like whats being described (delay)? Or is it primarily the length of the loop and the checking of the state of each function thats causing the delay?

I know the problem could be solved by placing the lights on a separate Nano, but I'd like to have it all together on the same Mega, as it feels like a waist of pins and space.

BubbleHockey:
for "a)" Is that not what is happening VIA the Max7219?

Your code turns the (random) SAME led on and then immediately OFF again. Likely this is so quick that you won’t ever be able to see it.

Johns code in post 2 implements case a). It turns a random led either ON or OFF. That means if a LED is turned ON then it will stay ON until it is randomly picked and then turned OFF again.

pcbbc:
Your code turns the (random) SAME led on and then immediately OFF again. Likely this is so quick that you won’t ever be able to see it.

Johns code in post 2 implements case a). It turns a random led either ON or OFF. That means if a LED is turned ON then it will stay ON until it is randomly picked and then turned OFF again.

When I upload the code from Post 2/option a), Even WITHOUT the lift code being added, the Matrix does nothing. What am I missing?
The code that I had originally works great (without the lift code), Im not sure what you mean by I would not be able to see it.
I guess there is probably something im not communicating well or Simple that Im not understanding.

BubbleHockey:
When I upload the code from Post 2/option a), Even WITHOUT the lift code being added, the Matrix does nothing. What am I missing?

We can't see what you are missing because you only posted a very small part of your sketch. I suspect there is a problem in the part of the sketch you did not bother to show.

johnwasser:
We can't see what you are missing because you only posted a very small part of your sketch. I suspect there is a problem in the part of the sketch you did not bother to show.

Both sketches are posted in the first post, as attachments because Both are well over the 9000 character limit.

The problem Only comes when I merge the two together. I only posted the small portion of the First sketch, to point out what is being effected by something in the Lift sketch when I combine the two together.