Multi touch demo edit

Dears,
I have some difficulties in implementing the multitouch in my script.

I am editing the MultiTouchminimalDemo. I want that only the first two touches are recorded, then a blackscreen is shown and, again, the script detects the other two first touches (and so on).

I tried to edit this part of the demo code in this way:

  % Main loop: Run until keypress:
    while ~KbCheck
        Screen('FillRect',w,[0 0 0]);
        Screen('Flip',w);
        blobcol = {};
        blobmin = inf;
        x = 0;
        while x < 2

            % Process all currently pending touch events:
            while TouchEventAvail(dev)
                % Process next touch event 'evt':
                evt = TouchEventGet(dev, w);

                % Touch blob id - Unique in the session at least as
                % long as the finger stays on the screen:
                id = evt.Keycode;

                % Keep the id's low, so we have to iterate over less blobcol slots
                % to save computation time:
                if isinf(blobmin)
                    blobmin = id - 1;
                end
                id = id - blobmin;

                if evt.Type == 2
                    % New touch point -> New blob!
                    blobcol{id}.col = rand(3, 1);
                    blobcol{id}.x = evt.MappedX;
                    blobcol{id}.y = evt.MappedY;
                    blobcol{id}.t = evt.Time;
                    % Track time delta in msecs between touch point updates:
                    blobcol{id}.dt = 0;
                    x = x + 1;
                end
            end

        end

In fact, if I press with two fingers (two touches) the script seems to work quite well and I see two dots. But if I touches 3 times, I see two dots on the present trial, while another one appears on the following trial… creating a lot of confusion.

Any advice?

Thank you,
best regards

help PsychPaidSupportAndService for professional advice.
-mario