# Millis and micros give different results

Hi
I made a mpu 6050 Air mouse.

The Mouse.move() value is always not greater than one, and according to the angle of the MPU 6050 changes the frequency that the command is called.
I noted that after setting the frequency delay at around 2000 micros the cursor would simply jump and snap to a faster velocity making it impossible to make it gradual. I guessed it was because the time it took for the board to process the loop was around that much.

Now i noted that in this sketch, setting the last piece like this:

`````` if (xangle > 40 && xangle <= 70 && micros()> time_21_1 + 1000){
time_21_1 = micros();
Mouse.move (-1,0) ;
}//
``````

Gave a different result than making it like this:

`````` if (xangle > 40 && xangle <= 70 && millis()> time_21_1 + 1){
time_21_1 = millis();
Mouse.move (-1,0) ;
}//
``````

The micros one moves the cursor faster. This i don’t understand.

Complete sketch is attached (is quite long)

Can someone give some insight?

micros()> time_21_1 + 1000)

?micros()> time_21_1 * 1000)?

I think the idea is to move once per millisecond.

DKWatson:
micros()> time_21_1 + 1000)

?micros()> time_21_1 * 1000)?

I'm not sure what you mean. I thougt that micros(1000) was equal to millis (1). Is this not like that?

pert:
I think the idea is to move once per millisecond.

Yes it is

Sorry, just woke up.

`````` if (xangle > 40 && xangle <= 70 && micros()> time_21_1 + 1000){
``````

Addition of times is NOT guaranteed. Subtraction is.

`````` if (xangle > 40 && xangle <= 70 && micros() - time_21_1 >= 1000)
{ // Down here, where it belongs
``````

To avoid any accumulating timing errors do it like this (with millis() or micros() )

``````if (millis() - prevTime >= interval) {
prevTime += interval;
``````

If you do it like this

``````if (millis() - prevTime >= interval) {
prevTime = millis();
``````

it is possible that the value of millis() on the second line is not the same as on the first line

And if you do it like this to use the same value in both lines

``````currentMillis = millis();
if (currentMillis - prevTime >= interval) {
prevTime = currentMillis;
``````

there is a risk on some occasions that the IF statement will not have been checked until after the time interval has been exceeded so that the prevTime variable is actually updated to a time that is a little bit wrong.

…R