Custom timer function timing query
Show older comments
I am trying to create a timer that accepts 3 inputs: An integer, and two floats representing times.
(input, time1, time2)
The timer will output, (we'll call it y), 1 if the input has been 1 for at least as long as the first time, and output 2 if the input has been 2 for at least as long as the second time. I have basically two levels of confirmation, where I need to know if my initial signal has been 1 long enough or 2 long enough. 1 and 2 are the only possibilities of input. The output, y, will start as 0. To describe the behavior a little more specifically, allow me to define two variables, t1 and t2.
t1 is true if the input has been 1 for at least as long as time1
t2 is true if the input has been 2 for at least as long as time2
And a quick logical chart:
t1 t2 y
0 0 No change
1 0 Set to 1
0 1 Set to 2
1 1 Not possible due to input being either 1 or 2
I need y to remain whatever it was for the 0 0 case. If t1 asserts and sets y to 1, but then t1 deasserts, I need y to remain as a 1.
Here is what I have so far:
function [y] = custom_timer(input, time1, time2)
y = 0; %y starts at 0
duration_1 = seconds(input == 1); %Set duration_1 to the number of seconds that input has equaled 1
duration_2 = seconds(input == 2);
if(duration_1 >= time1) %If we've been 1 long enough, set y to 1
y = 1;
elseif(duration_2 >= time2)
y = 2;
else
y = y; %Not really sure if I need this here, I just want Y to remain whatever it was if 0 0 case is met
end
end
When I set x to 1 directly, and the times to any arbitrary numbers, I always get y = 0. Is this because of the initial instantiation of y? Or is this due to my function not processing long enough for duration_1 to meet time1? If I comment out the initial instantiation of y entirely I get an error. I have also tried using the duration function instead of seconds but that gives me an error (duration(line 239)), so I switched to seconds(boolean) instead since I am measuring in seconds.
Eventually my input will not be just a number, it will be the output of some other function, and the value will change over time, it may be 1 for 10 seconds, and then be 2 for 30 seconds, etc.
Any help is appreciated, I have just started using Matlab and have little experience doing any other coding as well.
5 Comments
madhan ravi
on 10 Jul 2018
Maybe you can lookup tic toc function.
Guillaume
on 10 Jul 2018
It's not really clear what it is you want to implement. Note that a timer is an object that executes a function at regular intervals. So strictly, speaking a timer does not have inputs or outputs (the function it executes has).
Assuming you were to use a timer to execute a function at regular intervals (what interval?), I'm still not clear how you'd use it. Where does the y go? What is the consumer of that output?
I think you need to explain a bit more the purpose of what you're trying to do.
----
y = y; %Not really sure if I need this here
Well of course, not. It does nothing.
Emerson Butler
on 10 Jul 2018
Guillaume
on 10 Jul 2018
I don't think the details of the hardware matter nor the properties of the signal, so there's no need for you to break confidentiality.
So, if I understood correctly, your function would be called at regular interval by ... something. Is this something you've got sorted out or are you asking help with that?
What would be passed to the function, just the latest sample(s) or the whole signal? Would the function be passed the time since last call, should it work it out, or is it guaranteed to be constant?
Emerson Butler
on 10 Jul 2018
Accepted Answer
More Answers (0)
Categories
Find more on Sources 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!