How can I call a function for a specific amount of time, using a Timer function?

I'm currently working on a serial port communications function, wherein I take multicolumn data from the serial port (from an Arduino, via Bluetooth), for saving the sensor data stream into a database, for later work with Machine Learning and curve fitting algorithms. Currently, I am facing the issue where I'm controlling the flow of data from Arduino to Matlab by sending either a '1' (enable transmission) or '0' (Stop Transmission). I want to ensure that whenever I send a 1 (by calling a separate function sendval), it runs for 5 seconds, and then sends a 0,so that the data stream captured during that period is being saved into a database on MATLAB.
At the moment, I'm having no problem sending a '1' and saving the incoming data, except that I'm only getting ONE instance of the sensor readings, rather than a repeating stream. How do I call the same function repeatedly for, say, 5 seconds? (I can include a pause of 0.1 seconds in the function to get the readings at intervals of 0.1 seconds). This is my current code:
function bttest()
% This function sends a 1 and 0 alternately to the Bluetooth module, trying
% to fetch random numbers generated on an Arduino
% The base Arduino Sketch (later modified to transmit random numbers, rather than digitalWrite for LEDs) is given at:
% http://www.instructables.com/id/Arduino-AND-Bluetooth-HC-05-Connecting-easily/
clear;clc;
b=Bluetooth('HC-05',1);
fopen(b);
for(i=1:10) % Repeat for 10 sets of samples
pause(2); % Two second delay initially
sendval(1); % Initiate serial data reception
pause(5); %Run for 5 seconds
sendval(0); %Stop serial data reception
pause(5);
end
function sendval(val)
fprintf(b,'%u',val);
% while(val = 1)
m = fscanf(b);
pause(0.1);
end
end
Any help or suggestions would be greatly appreciated. I'm not very familiar with the Timer function, but if anyone could help me figure out how to use a timer function to call that sendval function repeatedly for 5 seconds at 0.1 sec intervals, I'd be thankful.
Thank you in advance!

 Accepted Answer

The sendval function has no outputs. Perhaps you want the first line of sendval to be
function m = sendval(val)
which will return whatever is the output of fscanf(b).
Another problem: When you call
sendval(1); % Initiate serial data reception
pause(5); %Run for 5 seconds
it does not run sendval for 5 seconds. Rather, it runs sendval for however long sendval takes (I think the way you've written it, it will take roughly 0.1 seconds). Consider rewriting sendval with a second input where you can specify how long sendval will run. For example:
function m = sendval(val,RunFor)
% Start a timer:
tic
% Do this loop over and over until the timer reaches RunFor seconds:
while toc<RunFor
fprintf(b,'%u',val);
m = fscanf(b);
pause(0.1);
end
end
To illustrate the while loop with tic and toc run this by itself:
RunFor = 3;
% Start a timer:
tic
% Do this loop over and over until the timer reaches RunFor seconds:
while toc<RunFor
disp(['It has been ',num2str(toc),' seconds.'])
pause(0.1);
end

2 Comments

Thank you very much! I have used tic-toc before for timing my scripts, but I hadn't thought about using them as a timer.
I went ahead and removed the function (It was just displaying the values, rather than saving them in an array). Then, I just took the fscanf command and put it in the tic-toc timer loop, did a bit of debugging, and was finally able to load the data into an array.
Thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!