AnimatedGif example: remove random

Ultra novice here: Just trying to get the random function removed from the Animated Gif player library sample. I seem to have succeeded by commenting out

// Seed the random number generator


and removing ‘random’ from

int index = random(num_files);

But now it just plays one gif over and over. What’s more strange to me is that it skips over the first one in the list. I know the gifs on my sd card are fine because when randomizing is happening in the default program, it eventually plays all of them. Clearly I’m missing (or need to add) some kind of +1 function somewhere to make it progress down the list.



I added sequential support and browsing in my fork of it, you can find it here:

it includes other changes that you may or may not work for you, but you can see the changes to the main file and take them if you’d like.

Hi John,

I think you uncovered a bug in the AnimatedGifs sketch. Try changing this line:

    int index = random(num_files);

to this (adding static to the beginning, so index stays unchanged throughout each loop):

    static int index = random(num_files);

You can then replace random(num_files); with 0.

If you want to make sure the first GIF is played first, and not the second GIF, change the first line of loop:

    static unsigned long futureTime = millis() + (DISPLAY_TIME_SECONDS * 1000);

I haven’t checked out Marc’s fork of the AnimatedGifs sketch, but it probably has a lot of this stuff sorted out already, you might want to check it out instead.

Thanks so much for the super fast response!! I used your changes Louis, that did the trick, it plays them sequentially. I looked at Marc’s a bit, but I confess my uber-newbie coding eye understands only about 25% of what I’m looking at.

Now I’ve got to figure out how to incorporate this vl53l0x I’ve got into my gif playing.

Basically I’m taking an intro-to-arduino workshop, and figured this would be a relatively easy thing for a brand-new-to-arduino guy to do. In retrospect I’m thinking maybe coding is (still) not for me. :wink: Anyway my small mission now is to make my different gifs play back at different times - depending on how close/far a person is to the sensor. I’ve managed to get the IDE’s test sketch for the sensor + serial monitor to prove the sensor works, but haven’t found any example code that would do something like I’m thinking. If you guys have suggestions that would be extra super, otherwise, I’m askin’ our teacher.

Thanks again!

I noticed another thing, Louis - while your code changes do make the gifs play sequentially, I can’t figure out the sort order. At first I though it was doing it solely alphabetically, but it doesn’t seem to be doing that. For example, I’ve got some filenames that look like


It skips the very first file in the list, plays the other ‘fire’ files sequentially, then skips to the ‘wavy’ files, then the ‘dance’ files, and finally plays the first file last. ?

Try setting the second parameter on this call to true, and look at the result on the Serial Monitor. What order are the files listed in?

If I recall correctly, the SD library isn’t good at playing files in order by name. It might be sorted by date or something else.

I’m confused, why? index is recomputed every time the function is called, so no need to keep its value across function calls

As for randomSeed(analogRead(14)), yeah it can be a bug depending on the wiring because it took one of my output pins (14) and made it input in the process, which broke the driver.
Explained in more details here Warning, analogRead can break SmartMatrix output

@Louis As for file order, I think it may depend on which order the files were written on the FAT (i.e. natural unordered)


True, it doesn’t matter for selecting a random GIF. It does matter if you want to replace the random functionality with sequential order.