# Controlling LEDs from MATLAB with less than 1ms execution delay?

Hello,

As I am a psychology PhD student with no computer science background, I apologize in advance if my questions are too basic.

Currently my experiment setting includes one projector (60Hz refresh) and four speakers connected to a USB external sound card. And I'd like to add 4 LEDs using an Arduino Uno. My main computer is MacBook Pro 15inch early 2011 model with OSX 10.9.4 and MATLAB R2012b installed.

I installed adioes.pde on my Arduino Uno R3, and wrote the following MATLAB program (see below) to test the execution time delay for each LED switch on/off. The delays are fluctuating between around 2ms and over 7ms.

My question is:
Is there any way to minimize the delay as little as 1ms or even less?

My experiment measures participants' reaction times in milliseconds, and for the obvious reason I'd like to keep the system delay as little as possible. I'd appreciate if you could give me any comments or advice regarding how to write a better program.

Thank you very much for your time.

function arduinoTest3

a = arduino('/dev/tty.usbmodemfd121');
TL = 8; % top left
TR = 10; % top right
BL = 9; % bottom left
BR = 11; % bottom right

a.pinMode(8,'output');
a.pinMode(9,'output');
a.pinMode(10,'output');
a.pinMode(11,'output');

WaitSecs(1);

t = GetSecs;
digitalWrite(a, TL, 1);
TopLeft = (GetSecs - t)*1000 % delay to execute the top left LED on/off
WaitSecs(.05);
digitalWrite(a, TL, 0);
WaitSecs(1);

t = GetSecs;
digitalWrite(a, TR, 1);
TopRight = (GetSecs - t)*1000 % delay to execute the top right LED on/off
WaitSecs(.05);
digitalWrite(a, TR, 0);
WaitSecs(1);

t = GetSecs;
digitalWrite(a, BL, 1);
BottomLeft = (GetSecs - t)*1000 % delay to execute the bottom left LED on/off
WaitSecs(.05);
digitalWrite(a, BL, 0);
WaitSecs(1);

t = GetSecs;
digitalWrite(a, BR, 1);
BottomRight = (GetSecs - t)*1000 % delay to execute the bottom right LED on/off
WaitSecs(.05);
digitalWrite(a, BR, 0);
WaitSecs(1);

delete(a);

end

Am I missing something ?

This is an arduino forum. If you are going to post code it needs to be arduino code and not Matlab code.
I don't understand your question since you are setting the delay time.
Am I missing something ?

check this Arduino program

``````int led1 = 8;
int button1 = 10;

void setup()
{
Serial.begin(115200);
pinMode(led1, OUTPUT);
pinMode(button1 , INPUT);
digitalWrite(button1, HIGH); // use internal pullup
}

void loop()
{
unsigned long w= random(0,  30000);
delay(w);

digitalWrite(led1, HIGH);

uint32_t start = micros();
uint32_t stop = micros();

Serial.println(stop - start);
delay(10000);
}
``````

will print reaction times in microseconds

The PC (MAC) is doing the measuring, yes? Commands an LED on, user hits the keyboard, PC takes the time measurememt?

I think part of how USB is handled is that data is buffered up and then sent out in bursts to the the slaves at very high speeds (tens of MHz clock speeds).
The slave USB/Serial adapter then "decodes" and passes the data on.
I don't know if you can do anything to change that, or how much it slows things down.
As to the code you've posted, I don't know. As raschemmel says, where is the Arduino code adioes.pde?

adioes.pde appears to be part of a Matlab package Arduinoio (sic)

My guess is that it allows remote operation of an Arduino via Matlab. With comms going over USB serial, the absolute minimum delay is going to be 2ms (since USB serial is polled every 1ms, command out + response back).

So what you need to do is to get the Arduino to measure the delay and report back to the Matlab program. The Arduino side is easy, the Matlab side should be too, you just need to open a tty device as a file then write/read data - I don't know what the commands are in Matlab.

ETA This might help Create Serial Port Object - MATLAB & Simulink - MathWorks United Kingdom