Fading matrix to black

I would like to fade a matrix with a dynamicly created content slowly to black. I’ve made an example maxpatch attached at the end of this post that shows how it should work.

Every click on the button "create square" creates a white square in the pwindow. The matrix should fade by and by to black, so the white squares can have different grey values whenever the user clicked on the button.

I’m trying since hours how this could be made. I thought about recursively methods but you can’t make
recursively loops in Max without creating an error. I’ve also tried to use jit.op somehow, but with no success. Perhaps there’s an easy way to do this. But unfortunately I have no idea.

It would be great if anyone can give me a tip. Many thanks in advance.

freeze

-- Pasted Max Patch, click to expand. --

Copy all of the following text. Then, in Max, select New From Clipboard.

I have a 10×10 video that i use to drive a 10×10 led matrix. All i need to do is : each time you have a pixel that goes to "ON" (let’s say, value = 100, or 255 if you prefer), i’d like it to fade out to black just after it goes to 255, or also have the possibility of doing 0>fade to 255>fade to 0, at a specific (fixed) speed.