Triggering Arduino from goodmonkey()
January 08, 2023 10:24AM
* Update on Mar 31, 2025

The method written below in the original post is not recommended anymore. Please see this manual page for interfacing Arduino with NIMH ML. The new method does not require MATLAB Support Package for Arduino Hardware and generates TTL pulses of more precise duration.



* Original post

* Requirements
MATLAB R2014a or later
NIMH ML 2.2.32 & 2.0.253 (Jan 7, 2023) or later
MATLAB Support Package for Arduino Hardware

* Instructions
1. Install the MATLAB Support Package for Arduino Hardware from the following link, if it is not installed already.
https://www.mathworks.com/matlabcentral/fileexchange/47522-matlab-support-package-for-arduino-hardware

If a download error occurs while installing the Support Package, see this link. It works for R2014a 32-bit as well.
https://www.mathworks.com/support/bugreports/1741173

2. Upload MATLAB's server to your Arduino board, using the arduinosetup command. Refer to the following link.
https://www.mathworks.com/help/matlab/supportpkg/configure-setup-for-arduino-hardware.html

3. Copy 'alert_function.Arduino.m' from the NIMH ML directory to your task folder and rename it to alert_function.m. The contents of the file look like this.
function alert_function(hook,MLConfig,TrialRecord)

switch hook
    case 'init'
        MLConfig.DAQ.set_reward_object(MLConfig,arduino);
end

end

4. Use the JuiceLine option of the goodmonkey function, to specify a particular digital pin.
goodmonkey(100,'juiceline','D2');  % use the D2 pin; R2015a or later
goodmonkey(100,'juiceline',2);     % R2014b or earlier uses a numeric pin number

5. Load your ML task and turn on the [Alert] button before running the task.
https://monkeylogic.nimh.nih.gov/docs_AlertFunction.html

The National Institute of Mental Health (NIMH) is part of the National Institutes of Health (NIH), a component of the U.S. Department of Health and Human Services.