Welcome! Log In Create A New Profile

Advanced

1. Flickering + sound feedback; 2. Arduino as I/O board

Posted by fazevedo 
1. Flickering + sound feedback; 2. Arduino as I/O board
March 12, 2021 01:09PM
Hi,

This is the first time that I program with ML2, so my questions will be beginner ones.

Q.1: I programmed a delayed-match-to-color task for touch inputs. The core of the program is working; however, I would like to add a visual feedback (flickering frames) and an auditory feedback (a beep) according to the behavior of the monkey. The flickering frames and the sound stimulus should start at the same time but be independent of each other. I partially managed to do this, but I consider my method too convoluted and I would like to know which is the best way to do so.

My whole code is attached but in brief what I did was the following:
i. I created an extra scene for the sound, an extra scene for a timer to be used only during the flickering sequence and an extra scene for the audio + one flickering timer;
ii. I created a function that runs the scene of the audio + one flickering timer and then only the scene of the flickering timer while switching the background color;
iii. I execute this function whenever the monkey commits a mistake, as an alternative to the reward.

The problem with my method is that, besides being convoluted, the first white frame of the flickering sequence must wait for the sound to be finished. However, I would like both feedbacks to be independent of each other.

Q.2: I have an arduino to deliver the reward and I wouldn't like to use bluetooth to deliver it. Is there a way to assign a specific digital channel of the arduino using the GUI of monkeylogic 2? I am currently using the matlab support package for the arduino to deliver the digital pulse but if there is an integrated way, I would like to know it. As a remark, I don't want to use the bluetooth solution because I intend to use the same arduino to listen to buttons in my next task; so, I would need a more general mechanism.

Best!
Attachments:
open | download - dmtc.txt (116 bytes)
open | download - dmtc.m (12 KB)
Re: 1. Flickering + sound feedback; 2. Arduino as I/O board
March 16, 2021 06:08PM
run_scene() is a blocking function, which means that it does not return the control to MATLAB until it finishes. You cannot just put a command or function before/after run_scene() and expect it to be executed simultaneously with run_scene().

To present a flickering visual effect and an audio sound at the same time, you should put them both in the adapter chain so that run_scene() can control them together. You can implement the flickering effect with ImageChanger.
https://monkeylogic.nimh.nih.gov/docs_RuntimeFunctions.html#ImageChanger

If you do not like to use an image sequence or do not find any existing adapter that does what you want, you should create your own adapter. There is a template provided for this (see "ext\ADAPTER_TEMPLATE.m" in the NIMH ML installation path) and also a tutorial in the "task\tutorial\2 adapter programming" directory.

I actually made a new adapter (BackgroundColorChanger) already, which may be useful in implementing that flickering effect. To try it, download the new packages.
https://monkeylogic.nimh.nih.gov/docs_RuntimeFunctions.html#BackgroundColorChanger

Arduino does not provide temporal accuracy that we need and is not supported by NIMH ML. Use it at your own risk. The bluetooth thing is about a remote trigger, not about arduino.

----------

There are a few things that you can improve your script on. See the attached files.

1. There is no need to run rng('shuffle') in the timing script. NIMH ML seeds the generator on startup already.

2. Putting the initialization code for external devices in the timing script is not efficient, since it will be executed every trial. Use the alert function.
https://monkeylogic.nimh.nih.gov/board/read.php?2,6,6#msg-6
Don't forget to turn on the alert function in the main menu.

3. SingleTarget shouldn't be separated from WaitThenHold/FreeThenHold in the chain. You can change the order of the adapters or just put graphics adapters in a separate chain.

4. Graphic objects of the same type can be created together with one adapter.

5. A flickering effect made with BackgroundColorChanger is added.
Attachments:
open | download - dmtc.txt (116 bytes)
open | download - dmtc.m (11.4 KB)
open | download - alert_function.m (439 bytes)
Re: 1. Flickering + sound feedback; 2. Arduino as I/O board
March 16, 2021 09:06PM
Thank you very much for your suggestions and corrections. I will implement them asap!

Monkey logic 2 is indeed an impressive piece of software. Very useful and very well written.

Best.
Re: 1. Flickering + sound feedback; 2. Arduino as I/O board
March 16, 2021 09:57PM
Take a look at the files I attached first. They are already modified as I suggested.
Re: 1. Flickering + sound feedback; 2. Arduino as I/O board
July 20, 2021 02:48PM
Hi Jaewon,

I tested the code that you've sent to me and it works almost perfectly. I had to modify it very slightly for my task but the changes weren't significant ones. Thank you very much!

One weird behavior though is that when I exit the task and I want to run it again, Matlab (r2021a update 4) gives me the following error: "MATLAB connection to Leonardo at COM4 exists in your workspace. To create a new connection, clear the existing object." The weird thing is that I already cleared all the variables, which includes the arduino object. I even tried fclose(instrfind) and delete(instrfind) but "instrfind" returns empty. Somehow the arduino object persists in the working memory even though I can't access it. The only way that I can run the program again is by restarting Matlab. Would you know how to solve it? I don't know whether it is a Matlab or a Monkeylogic problem.

Besides that, I realized that the sound that the touch should make at the touch moment doesn't play exactly when the touch is done. It has a minimal but perceptible delay. Should I try to mix runtime v1 and runtime v2 to make it more precise?

Best!

PS: My codes and soundfiles go attached.
Attachments:
open | download - dmtc.zip (42.9 KB)
Re: 1. Flickering + sound feedback; 2. Arduino as I/O board
July 24, 2021 07:33AM
The object is provided by MATLAB and inserted in the script by you. I do not understand what makes you think NIMH ML has anything to do with the error.

If you are not using the latest version, please update. There was an important change in touch processing.
https://monkeylogic.nimh.nih.gov/board/read.php?1,851,851#msg-851

Technically your script does not play sounds at the moment of touch. It does in the next scene after the touch is detected. And there is also a latency in audio generation.

-----

In your give_juice_ard() function, you provide the arduino object as an argument to TimerFcn but never stop nor delete the timer, which prevents the arduino object from being cleared. It is your mistake.
Re: 1. Flickering + sound feedback; 2. Arduino as I/O board
July 28, 2021 04:07PM
Thanks for your tip, Jaewon. The delete(timer) solved my problem. It was definitely my fault. The interesting thing is that even though Matlab told me that there was an arduino object which had not been deleted, my workspace was completely empty and I couldn't find a way to access that object. Anyway...

Concerning the sound, I guess that there is no way of playing it as soon as the screen is touched, right?

Thanks again for your help.
Re: 1. Flickering + sound feedback; 2. Arduino as I/O board
July 29, 2021 11:04AM
If the sound is triggered in the same scene instead of the next one, the delay can be reduced from 2 frames to 1 frame. You can also decrease the audio latency by ~30-40 ms if you use WASAPI with a right driver, instead of XAudio2.

I did not see a perceptible delay though, when I tested your script. Update your NIMH ML first, if you did not do it yet.

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.