function getPasswd(timeLimit)

%waits for password to be entered (then returns to main program)
%not case sensitive
%if timeLimit seconds have passed, program ends automatically
%timeLimit=0 means no time limit (program waits indefinitely for password)

password='inished'; %first letter typed usually gets ignored.  tell RAs to use "finished"
key=' ';
i=1;
released=1;
startTime = GetSecs;

while i<=length(password)
    [keyIsDown,secs,keyCode] = KbCheck;
    if keyIsDown
        if released  %had ~keyIsDown at last check
            released=0;
            key=KbName(keyCode);
            if upper(key)==upper(password(i))  %correct key
                i=i+1; %advance place in sequence
            elseif upper(key)==upper(password(1))  %user restarted sequence
                i=2; %start at position 2
            else i=1;  %wrong key. start over
            end
        else
            if ~isequal(key,KbName(keyCode))  %have moved suddenly to new key
                key=KbName(keyCode);
                if upper(key)==upper(password(i))  %correct key
                    i=i+1; %advance sequence
                elseif upper(key)==upper(password(1))  %user restarted sequence
                    i=2; %start at position 2
                else i=1;  %wrong key. start over
                end
            end
        end
    else released=1; %no key being pressed
    end
    if (GetSecs > startTime + timeLimit) & (timeLimit>0)  %time is up
        i = length(password)+1;  %forcing program to end
    end
end