Home How to end loop if value does not change for X consecutive seconds in Matlab
Reply: 1

How to end loop if value does not change for X consecutive seconds in Matlab

Glu
1#
Glu Published in 2018-02-13 20:31:44Z

I am collecting data from a potentiometer connected to an Arduino. In the script, I tell matlab to keep collecting data for 2 minutes. But I need to tell it that if the user does not move the potentiometer for 10 consecutive seconds, then it should stop the loop and move to the next session (write the data to an excel file). Does anybody have ideas on how to achieve this?

Marco
2#
Marco Reply to 2018-02-13 23:04:35Z

Probably tic and toc can help you.

tic starts a stopwatch timer. The function records the internal time at execution of the tic command.

toc reads the elapsed time from the stopwatch timer started by the tic function.

tic;
while toc < 10
    % Do your loopy things
    if variable_changed
        tic; % Restart stopwatch
    end
end

Furthermore to be sure tic won't interact with other processes you should store it's value like this:

% First start stopwatch
time_since_last_movement = tic;
while toc(time_since_last_movement) < 10
    % Do your loopy things
    if variable_changed
        time_since_last_movement = tic; % Restart stopwatch
    end
end
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301103 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO