# What happens if a function takes longer than it should?

Hello. I'm curious. Suppose I have a function that takes 300uS to do what it has to do... but it only has 200uS to do it, what would happen? It wouldn't finish it? It would take longer than it should? For example, i have a counter which counts 1, 2, 3, 4, every 200uS. And i have a function which should happen only when the counter is 2:

``````if(counter == 2){
//300uS function here}
``````

The Arduino can only do one thing at a time.

If you have a counter running with a delay of 200uS between each increment and call a function that takes 300uS when the counter reaches 2 then the counter will not increment again until the function completes.

but it only has 200uS to do it, what would happen?

Not too sure what you mean by only having a small ammount of time. Basically a function will compleat unless you set the interrupts to stop it.

If things happen that are too fast to handle then it simply will not handle all of them, just some.

How do you define how long a function "should" take? It will keep going until it is finished.

Perhaps if you post more code. This question is rather vague.

Actually, I don't have a code for this, it was just a question that came up... because sometimes I get lost by the way Arduino does the things, maybe that's why my question is vague. I say it should take 200uS because the if condition is true for only 200uS when the counter == 2. 200uS later the counter will be 3, so if the function takes 300uS, the counter will wait for the function to finish?... Would it make the time between the counter longer? For example, analogRead() takes 120uS to finish right? If three analogRead() inside the function, it would take 360uS or not? but the function will be done only when the condition is true for 200uS... also, my main language isn't english so it may be a bit hard to understand me

you have a 'counter'? what are you meaning by counter?

For example, something like this

``````byte counter = 0;
unsigned long = lastTime;
int interval=200;
void loop(){
if(micros() - lastTime >= interval) {

lastTime += interval;

counter++;
}

if(counter == 2){
//300uS function here
}
if(counter == 10){
counter=0;
}

}
``````

i just wrote this so it may have an error, it's just as an example

Ah.

The Arduino can only do one thing at a time. In that program, functions aren't 'limited' to 200us. They will take as long as they need to execute. Once they have completed, they will return and your loop will continue.

In that example, if the function took longer than 200us, then the counter variable will increase once the function has returned, however the period between counts that one time will be much longer than 200us.

An example.

Counter = 0. nothing will happen until "micros()-lastTime >= 200". So counter increases after 200us Counter = 1 again, counter will increase after ~200us Counter = 2 the long function is called which takes 300us to complete. So counter will increase after ~300us. Counter = 3 after 200us the counter will increase.