loop in realtime arduino reading

Hi
I want to read a value of the variable each 0.02 seconds I write this code:
c(1)=readCount(encoder);
for i=2:100
c(i)=readCount(encoder)
pause(0.02)
end
actually I want to read the count of the encoder for the loop (i=2:100)
each 0.02 secound

 Accepted Answer

Use a timer object with FixedRate . Be sure to pre-allocate c.
You might have some difficulty reaching 50 readings per second over the USB connection, unless you use a bit of code or a sketch on the arduino side to keep sending out the values instead of waiting for general commands. It is not uncommon for people to report they only get about 40 samples per second unless they run more dedicated code on the arduino.

10 Comments

so how I should write the code of a timer object in matlab?
function readout_fcn = pqrst(encoder)
c = zeros(1,100); %shared variable
t = timer(...., 'TimerFcn', @store_value)
cidx = 0;
start(t)
readout_fcn = @retrieve_info;
function store_value(hobject, event)
this_value = readCount(encoder);
cidx = cidx + 1;
c(cidx) = this_value;
end
function [current_c, current_idx, timeobj] = retrieve_info
current_c = c;
current_idx = cidx;
timeobj = t;
end
end
This will return the handle to a function. The function has three return parameters: the current buffer of values that have been read in, and the count of how many have been read in, and the timer object. Wait 2-ish seconds for the data to be read in and stored in the buffer, and then call the function to retrieve the information, and then delete the timer.
ok thanks a lot
in this code the number of the readCount is limited isn't it?
I mean it saved 100 numbers of pulses of the encoder in c(cidx) and there is no period of time , I want to read the pulses of the encoder each 0.01 seconds
could you help me please
In that code, readCount will be called each time the timer fires. With the timer set to ExecutionMode FixedRate with a period of 0.01 seconds, it should be called as close as it can get to every 0.01 seconds. To set how many values are stored, set the timer TasksToExecute to the number of samples you want:
NumToCollect = 100;
c = zeros(1, NumToCollect);
t = timer('ExecutionMode', 'FixedRate', 'TasksToExecute', NumToCollect, 'Period', 0.01, 'TimerFcn', @store_value);
and so on.
thank you so much
for the last question
actually I want to control a dc motor , control code wants readCount s as the input and gives the torque for dc motor as the output How could I use the timer object for this loop I mean for the theorical method I used for loop and the answer is correct for example the code is:
th(1)=0
for i=1:100
K(i)=th(i)*5;
T(i)=K(i)+3;%torque
th(i+1)=th(i)+2;
end
but for the practical method I have no Idea how to use the timer object to done this loop! by your code I could readCount each 0.01 second but how to give give the readCount as the input each 0.01 second to the K ?
is it the correct way to use this code:
c(1)=readCount(encoder);
for i=1:100
K(i)=c(i)*5;
T(i)=K(i)+3;
function readout_fcn = pqrst(encoder)
NumToCollect = 100;
c = zeros(1, NumToCollect);
t = timer('ExecutionMode', 'FixedRate', 'TasksToExecute', NumToCollect, 'Period', 0.01, 'TimerFcn', @store_value);
cidx = 0;
start(t)
readout_fcn = @retrieve_info;
function store_value(hobject, event)
this_value = readCount(encoder);
cidx = cidx + 1;
c(cidx) = this_value;
end
function [current_c, current_idx, timeobj] = retrieve_info
current_c = c;
current_idx = cidx;
timeobj = t;
end
end
end
?
Is each value to be used as soon as it is read, or are the values to be used after they are all read?
The code I posted was to give an bit of an abstract interface to a buffered data collection queue. It assumes you might want to go away and do something else while the data is arriving, and that there is a disconnect between the samples arriving and them being processed. In practice if you want to process each sample as it arrives, you would not use this kind of structure: instead you would set the TimerFcn to be a function that reads a sample and immediately uses it.
actually I want to used each value as soon as it is read and I don't know how to set the TimerFcn for this purpose!! could you help me please
please help I have no idea about the TimerFcn to do my purpose!
any way, thank you so much Walter Roberson for your help

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!