%% Objektiv - Nastaveni clony
% Martin Zeman, 2009

function clonaa(kamera, vyska, sirka, clona, S)
    img = getsnapshot(kamera);
    bile_clona = 0;   %nastaveni clony
    for i=1:vyska
        for j=1:sirka
            if img(i,j) >= 225
                bile_clona = bile_clona + 1;
            end
        end
    end
    if bile_clona>4000
        while (bile_clona > 4000) && (clona <= 200)
            clona = clona + 25;
            string=sprintf('lap0%03d',clona);
            fprintf(S, '\002x%s\003x', string);
            while (S.BytesAvailable < 3)
                pause(0.1)
            end
            pause(0.5);
            img = getsnapshot(kamera);
            bile_clona = 0;
            for i=1:vyska
                for j=1:sirka
                    if img(i,j) >= 225
                        bile_clona = bile_clona + 1;
                    end
                end
            end
        end
    elseif bile_clona==0
        while (bile_clona < 100) && (clona >= 100)
            clona = clona - 25;
            string=sprintf('lap0%03d',clona);
            fprintf(S, '\002x%s\003x', string);
            while (S.BytesAvailable < 3)
                pause(0.1)
            end
            pause(0.5);
            img = getsnapshot(kamera);
            bile_clona = 0;
            for i=1:vyska
                for j=1:sirka
                    if img(i,j) >= 225
                        bile_clona = bile_clona + 1;
                    end
                end
            end
        end
    end