How to replicate an Arduino timer function in Matlab?
11 views (last 30 days)
Show older comments
I'm using the Mathworks Arduino Support Package and trying to replicate the function of an Arduino script in Matlab. The code should control an ultrasonic sensor to measure distance to a moving object.
The sensor returns its measurement readings by varying the time it holds a nominated Arduino I/O pin high. So, I need to have Matlab watch that input pin, timing the period it takes to go from digital low, to high, to low again.
In Arduino speak I have the native "pulseIn" function, which is used in this way:
pulse_duration=pulseIn(sensor_signal_pin, HIGH)
Can anyone suggest the best way to do this in Matlab?
I've been trying variations on a while loop, but making an embarrassing mess of it. This sort of thing:
while a.digitalRead(sensor_signal_pin)==1;
tic;
end
toc
pulse_duration=toc-tic;
The "a.digitalRead(sensor_signal_pin)" command simply instructs Matlab to read the value of the nominated Arduino pin, checking for high or low signal (1 or 0).
Anyone offer a suggestion? I need to do this as neatly as possible because the timing is important - if I don't time the duration the input pin is held high correctly, my distance readings will be inaccurate.
Thanks if you can offer advice.
G.
1 Comment
Gautam Vallabha
on 5 Apr 2012
If you want to measure elapsed time on the order of tens of milliseconds, you need to do it by adding a custom capability to the .pde file, as in Felipe's answer below. Doing the timing measurement from MATLAB will lead to a lot of variability due to
a) the communication latency in the serial request and the response, and
b) Operating system variability (e.g., if there are other high-priority processes running on the host OS that preempt MATLAB).
Accepted Answer
felipe
on 2 Apr 2012
Hi man, i'm with the same problem and i might have a solution but i didnt test it... so if u want u can test it for us... so lets begin
Well... open the arduino.m file, this file controls the functions u can use with the arduino... when u type a.digitalread(blabla), ur arduino receives some sort of call for the function digitalread...
So... that call goes to the server that runs in the arduino, adiosrv...
locate this part of the adiosrv.pde
case 10:
_/* the second received value indicates the pin
from abs('c')=99, pin 2, to abs('t')=116, pin 19 */
if (val>98 && val <117) {
pin=val-97; /* calculate pin */
dgv=digitalRead(pin); /* perform Digital Input */
Serial.println(dgv); /* send value via serial */
}
s=-1; /* we are done with DI so next state is -1 */
break; /* s=10 taken care of _
Thats the call for digitalRead, what i meant to try out was to change that call for pulsein just like the follow code...
case 10:
/* the second received value indicates the pin
from abs('c')=99, pin 2, to abs('t')=116, pin 19 */
if (val>98 && val <117) {
pin=val-97; /* calculate pin */
dgv=pulseIn(pin,LOW); /* perform Digital Input */
Serial.println(dgv); /* send value via serial */
}
s=-1; /* we are done with DI so next state is -1 */
break; /* s=10 taken care of
I just changed the function, already made it for the LOW part of the signal, and the answer will be in dgv and come by serial anyway so i think it may work
But... to test it u have to call for a.digitalRead(PIN) and it will give it the pulseIn answer in ms... i think...
And... if u need the digitalread function (in my case i dont) u will lose that function... u can try to make a new call for pulseIn, as i dont know that will work i dont even bother to add some lines in the code... so... i dont have it...
Hope i helped u... post it if it worked or not... good luck
1 Comment
Gautam Vallabha
on 5 Apr 2012
You can also add your own case statement with a new number (e.g., "case 101:"), and add a corresponding method in the arduino.m file that will invoke that capability.
More Answers (0)
See Also
Categories
Find more on Device Driver Blocks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!