rand('state',sum(100*clock));

screens=Screen('Screens');
screenNumber=max(screens);

HideCursor;

gray=GrayIndex(screenNumber); 
white=WhiteIndex(screenNumber);

[w, wRect]=Screen('OpenWindow',screenNumber, white);

Screen('TextSize', w, 32);

priorityLevel=MaxPriority(w);
Priority(priorityLevel);

message = 'First we are going to set up the equipment\n... press any key to begin ...';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
KbWait;  


%start communicating with the Dikablis laptop
%in Dik recorder, calibrate then start recording
%Craig worked this out
c = tcpip('150.100.100.20',2001);%set up the IP/port
% c = tcpip('142.103.0.138',2001);%set up the IP/port
c.Terminator = 'LF/CR';  %tell it about line endings
fopen(c)    %open the channel
fprintf(c,'Luser,pass');pause(1); %tell Dik we're going to be communicating
WaitSecs(2);


%open project, and create new subject
fprintf(c,'Opetertest');pause(1);
fprintf(c,'CEs003');pause(1)
WaitSecs(2);

%start Dikablis recording
fprintf(c, 'Rstart');pause(1);
WaitSecs(2);

message = 'fixation';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
% fprintf(c,message);%pause(1); 
fprintf(c,'EFixOn');pause(1);            
Waitsecs(1);

message = 'stim1';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
fprintf(c,'Estim1');%pause(1); 
WaitSecs(1);

message = 'fixation';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
fprintf(c,'EFixOn');pause(1);            
WaitSecs(1);

message = 'stim2';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
fprintf(c,['E',message]);%pause(1); 
WaitSecs(1);

message = 'fixation';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
fprintf(c,'EFixOn');pause(1);            
WaitSecs(1);

message = 'stim2';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
fprintf(c,['E',message]);%pause(1); 
WaitSecs(1);

message = 'fixation';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
fprintf(c,'EFixOn');pause(1);            
WaitSecs(1);

message = 'stim1';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
fprintf(c,['E',message]);%pause(1); 
WaitSecs(1);


message = 'finished';
DrawFormattedText(w, message, 'center', 'center', BlackIndex(w));
Screen('Flip', w);
KbWait;

%stop Dikablis recording
fprintf(c, 'Rstop');pause(1);

Screen('CloseAll');
ShowCursor;
fclose(c);
fclose('all');

