Non-blocking for loop

I’ve got an arduino project which displays a message across an LED matrix. The code for this is here.

The important part of it is this:

void loop()
{
    // io.run(); is required for all sketches.
    // it should always be present at the top of your loop
    // function. it keeps the client connected to
    // io.adafruit.com, and processes any incoming data.
    io.run();

    setDisplayText();

    for (int16_t x = 16; x >= getScrollWidth(); x--)
    {
        if (button.getSingleDebouncedPress())
        {
            reportIncident();
            setDisplayText();
            x = 16; // resets the display loop
        }

        // Serial.print("Scroll position: ");
        // Serial.println(x);
        matrix.clear();
        matrix.setCursor(x, 0);
        matrix.print(displayText);
        matrix.writeDisplay();
        delay(80);
    }
}

Specifically the for loop, which controls the display of message scrolling across the screen. The problem is this is a blocking method, which means for the period of time the message is scrolling, the io.run(); is unable to run, meaning I’m not getting updates.

I figure there must be a way to update this code so the for loop isn’t needed, instead, for some type of calculation to happen as part of the loop method, working out the positions of the text, etc… and calling the relevant matrixmethods to update the display - but I don’t know how to do it…

… which is where I’m hoping someone here might be able to help me translate the code, please?

Thank you.

I’m no expert but I had a similar problem trying to get multiple neopixels running different patterns at the same time.
Look at this and read up on the time slicing method:


It’s probably the delay at the end of the code that’s causing the block.
I don’t know how long your display takes to complete a screen but add 80 milliseconds