Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Paradoxical Pushbutton behavior and nonresponsiveness

Subject: Paradoxical Pushbutton behavior and nonresponsiveness

From: Paul

Date: 4 Dec, 2012 10:23:08

Message: 1 of 2

Dear Matlabbers, I have the following problem, for which I will supply some code and then also describe my unsuccesful attempts to localize the exact problem. I hope you can help.

I'm doing research on sensory perception and learning in rats, and I'm using Matlab to control a behavioral setup through some external hardware (a field programmable gate array (FPGA) from Opal Kelley, if you must know).

On the push of a button, I want to begin the task, i.e. run the Matlab function which automates the behavioral setup. This includes several loops for checking the status of photo-beams in the setup and subsequent reward delivery in two sites in the setup.

In general this works fine, and I'm using many more buttons and pop-up menu's to control various aspects of various versions of the task this way. However there is one problem which bothers me in all these cases;

I wish to be able to temprorarilly override the reward-delivery criteria, so that I can deliver reward at any moment I wish, when the script is already running. I am able to do this by using the KbCheck function in a loop, but for some reason I cannot do this using a callback function of a pushbutton.

I placed the code at the end. As you can see it's quite a lot of loops which continuously check the state of the FPGA output.

1) Initially I thought the pushbutton callback didn't work because the FPGA was busy, but this is not the case since I can operate the FPGA using the KbCheck function as coded.
2) Then I thought the GUI functions were busy in some way (I'm a n00b), but this is also not the case, since I was able to substitute the current code with some arbitrary loops to keep Matlab busy, but then the pushbutton callback DID work.
3) I'm able to operate hardware OTHER than the FPGA through pushbutton commands in this loop (the StepperSend functions control other hardware), but this occurs with a striking delay of seconds/minutes.

So something must be keeping something too busy to respond to the pushbutton commands; it's just not very clear what. I have been reading all the information on pushbutton operations online but I just can't find a solution. Please help! Thanks in advance!

% --- Executes on button press in PBStart.
function
PBStart_Callback(hObject, eventdata, handles)
global objCard
global sPortDef
global structVals
% hObject handle to PBStart (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

a=cellstr(get(handles.POPStage,'String'));

switch a{get(handles.POPStage, 'Value')}
case 'Stage 0.2'
StepperSend('com3','Y000G');
pause(0.2)
StepperSend('com3','X18000G');
pause(0.2)
ITI = 2;
I = 0;
start = 1; % controls loop
reward = 1; % controls loop
x = 0;
ES = 0; % Exit Signal

while ~ES % press 'escape' to exit

BeamBC = ReadFPGA(objCard,sPortDef.ADRES_BeamBC,false,structVals,sPortDef);

if BeamBC.SENSOR0 == 499 && start

StepperSend('com3','X000G') % Close door X when BeamBC (in2) is interrupted

time1 = BeamBC.KLOK1*60 + (BeamBC.KLOK0/1000);

start = 0; % skips this loop until trial is over
reward = 1; % allow reward dispensing

x = 1;

end

if x

time2 = BeamBC.KLOK1*60 + (BeamBC.KLOK0/1000);

I = time2 - time1; % I is the amount of seconds that have passed since beamBC interruption (in2).

end

if I > ITI % If I is larger than the designated ITI, open DoorY.

StepperSend('com3','Y18000G')

x = 0;

I = 0;

end

AccessL = ReadFPGA(objCard,sPortDef.ADRES_AccessL,false,structVals,sPortDef);

AccessR = ReadFPGA(objCard,sPortDef.ADRES_AccessR,false,structVals,sPortDef);

if AccessL.SENSOR0 == 375 && reward

disp('Dispensing reward on the left side')

SetFPGA(objCard,sPortDef.ADRES_ValveS1,1,uint32(600),sPortDef.PULSE,sPortDef)% Open ValveS1 (left)

pause(0.2)

SetFPGA(objCard,sPortDef.ADRES_PumpS,1,uint32(280),sPortDef.PULSE,sPortDef)% Pump reward

StepperSend('com3','Y000G')

StepperSend('com3','X18000G')

start = 1; % prepares start of next trial by allowing doorX to close upon BeamFC (in2) interruption

reward = 0;

% no more rewards until next trial is started

elseif AccessR.SENSOR0 == 471 && reward

disp('Dispensing reward on the right side')

SetFPGA(objCard,sPortDef.ADRES_ValveS2,1,uint32(600),sPortDef.PULSE,sPortDef)% Open ValveS2 (right)

pause(0.2)

SetFPGA(objCard,sPortDef.ADRES_PumpS,1,uint32(280),sPortDef.PULSE,sPortDef)% Pump reward

StepperSend('com3','Y000G')

StepperSend('com3','X18000G')

start = 1; % prepares start of next trial by allowing doorX to close upon BeamFC (in2) interruption

reward = 0;

% no more rewards until next trial is started

end

[ keyIsDown, seconds, keyCode ] = KbCheck;

if keyIsDown % Define keyboard shortcuts

if keyCode(37) % Left arrow for left reward

disp('Dispensing reward on the left side')

SetFPGA(objCard,sPortDef.ADRES_ValveS1,1,uint32(600),sPortDef.PULSE,sPortDef)% Open ValveS1 (left)

pause(0.2)

SetFPGA(objCard,sPortDef.ADRES_PumpS,1,uint32(280),sPortDef.PULSE,sPortDef)% Pump reward

elseif keyCode(39) % Right arrow for right reward

disp('Dispensing reward on the right side')

SetFPGA(objCard,sPortDef.ADRES_ValveS2,1,uint32(600),sPortDef.PULSE,sPortDef)% Open ValveS2 (right)

pause(0.2)

SetFPGA(objCard,sPortDef.ADRES_PumpS,1,uint32(280),sPortDef.PULSE,sPortDef)% Pump reward

elseif keyCode(27)

disp('Received exit signal. Stopping session.')

ES = 1;

break

end

end

end

case 'Stage 1.0'

disp(

'Not coded yet')

end

Subject: Paradoxical Pushbutton behavior and nonresponsiveness

From: Paul

Date: 4 Dec, 2012 12:44:08

Message: 2 of 2

ps. using Matlab 7.5 on Windows 7 Ultimate 64-bit.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us