How can I call a function for a specific amount of time, using a Timer function?
Show older comments
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
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!