Help with multiple tasks

I have been working on a new sketch. In my older sketches, I was able to figure out how to create a “multiple tasks” using one time base.

But this new sketch I am having trouble with.

Here is my loop code:

void loop() {
  scroll(msg[msgChoice]);  // scrolling message on an HT16K33 led matrix
  currentTime = millis();    // time base
  updateLedState();          //  this is used for switching LEDs
  switchLeds();                 //  This is just turning LEDs on and off
  msgDecision();               //  This is used to watch the status of a switch

The breakdown. each line is a sub function that gets called.

Here is the “scroll” function:

void scroll(char* text) {
  int scrollPositions = (strlen(text) * 8) - 32;

  for (int x=(8 * NUM_MATRICES); x>=-scrollPositions; x--) {
   for(uint8_t m=0; m<NUM_MATRICES; m++) {
    matrix[m].setCursor((x - (m * 8)), 0);

I have tested the LED switching with other sketches. It works fine. But here is my trouble. When I added the LED parts to the loop, it is not working. I can tell the “delay(msgInterval)” is what is part of the issue. The loop is in hold until it releases. By then the LEDs have gotten all screwed up.

Here is the LED switch code:

void updateLedState() {
 if (currentTime > 0) {
    if (currentTime >= previousTime + LedInterval) {
         Led0State = 1;
    if (currentTime >= previousTime + (LedInterval*2)) {
         Led1State = 1;
    if (currentTime >= previousTime + (LedInterval*3)) {
         Led2State = 1;
    if (currentTime >= previousTime + (LedInterval*4)) {
         Led3State = 1;
    if (currentTime >= previousTime + (LedInterval*5)) {
         Led4State = 1;
    if (currentTime >= previousTime + (LedInterval*6)) {
         Led5State = 1;
    if (currentTime >= previousTime + (LedInterval*7)) {
         Led0State = 0;
         Led1State = 0;
         Led2State = 0;
         Led3State = 0;
         Led4State = 0;
         Led5State = 0;
         previousTime = currentTime;

This has been driving me nuts trying to get this working.

The desired end result:

The HT16K33 8x8 matrix scrolls the message while 6 LEDs turn on and off based on updateLedState() and there is a switch being monitored.

if (currentTime >= previousTime + (LedInterval*2))

This has a big bug. Never write using addition. Don't predict the future time. Millis could roll over and you'll miss an interval. Always use this form to avoid that situation:

if(currentTime - previousTime >= Interval)

But either way, you obviously understand how to handle timing without using delay because there you did it. Why don't you do the same thing for the timing of your scroll function?

Please change the quote tags in the post to code tags. So we don’t have to try to translate smilies and we don’t have to read italics.
Please post complete code. If it’s too big, either attach or split over two or more posts.

And for the problem, I think that you need to get rid of delay completely.

Re-reading your post, I think that you already figured that out. In which case you should get rid of the outer for loop in your scroll function.

void scroll(char* text)
  static int scrollPositions = 0;

  // when starting a new scroll, get the length
  if(scrollPositions == 0)
    scrollPositions = (strlen(text) * 8) - 32;

  static int scrollCount = 8 * NUM_MATRICES;
  static unsigned long scrollStart;

  // if it's time
  if (scrollStart - millis() >= msgInterval)
    // scroll one position
    for (uint8_t m = 0; m < NUM_MATRICES; m++)
      matrix[m].setCursor((x - (m * 8)), 0);
    // restart timing
    scrollStart = millis();
    // one move completed
  // if scroll completed
  if (scrollCount == 0)
    // reset for next scroll
    scrollPositions = 0;


Not compiled but it should give you the idea.

If it's too big, either attach or split over two or more posts.

Attach it. Do NOT split it over multiple posts. It's a pain in the ass to try to sew code back together.

Thanks for the help!

I have fixed the original post, no more quotes.

I have fixed the math syntax (no more addition, now using subtraction)

AND, I have attached the whole sketch.

I have not had a chance to try the “delay” fix yet. If I am able to today during lunch I will, otherwise, it will be tonight.

I guess what is messing me up is I am using a sketch for a MAX7219 as a guide and this sketch is just a wee bit different. But with y’alls help I think I will have this fixed.

Mini_Range_Finder.ino (4.45 KB)

sterretje, I have one question. I am looking over your example, but the line:

matrix[m].setCursor((x - (m * 8)), 0);

I am lost what the x is. I get the idea of what it is, but exactly what is it? I am also asking because that X is causing a compile failure.

but exactly what is it?

Speaking of what exactly something is, what is m * 8)?

Yeah, I was trying to fix that. I didn't use the code block thinking it was just one line, no real need to put the big code block in. OH BOY was I wrong. And then trying to edit it to fix it, the board said I had posted too many times in 5 minutes and had to be put in the corner in a time out.

Now I got it

I guess I renamed one of your variables but did not update it; should now be scrollCount by the looks of it.

But as said, it's to give you the idea how to get rid of the outer loop; it might happily not work ;)

Hmmmm…I tried the code. It compiled, but did not give the desired results.

The scroll starts, but locks up. Also, the adding LEDs do not work.

I know this like comparing apples to VW beetles, but I have attached the sketch am working on, and a sketch that works. Only trouble is, the working sketch is for the MAX7219 chips.

I am hoping someone else can see what I can’t. I’m starting to get crosseyed.

FullHelmet7219.ino (11.8 KB)

Mini_Range_Finder.ino (5.29 KB)

Guys, thank you for your help! I know it may not seem like it, but y’all were a great help figuring this out. Once I realized my LED timing wasn’t critical I was able to figure out my step counter.

I attached my final version if you want to look at it.

Fett_Helmet_Final.ino (4.35 KB)