 # Doubt about the micros() function!

I have an Arduino program that do some functions, control a dc motor and display some informations on the serial monitor, and one of them is the time difference of some readings of a light sensor.

So, I have this piece of code that calculates the time difference:

``````  void info() {
tempo = micros();
tempo = micros() - tempo;
Serial.print("LDR 1: ");     Serial.print(valor1);
Serial.print("   LDR2: ");   Serial.print(valor2);
Serial.print("   Erro: ");   Serial.print(dif);
Serial.print("   DT: ");     Serial.println(tempo);
}
``````

"tempo" is the variable for the time difference, so I call the function micros() to get Arduino's running time, and after printing some informations on the screen, I define the variable "tempo" as micros() to do the next time difference calculation.

So my doubt is the following... I've read that the micros() function on Duemilanove can calculate the time in 4 from 4 microseconds. Does the time of printing and the other functions I mentioned create a delay on the micros() reading? Or it will always be able to make reading in 4 from 4 microseconds?

Thanks everybody!

``````tempo = micros();
tempo = micros() - tempo;
``````

Those two calls are probably much less than 4 microseconds apart, but I'm still not sure I understand your question. Is "tempo" an "unsigned long" variable?

Yes, “tempo” is assgned to a unsigned long. This is the entire code, not hard to understand.

``````#define InA1 2
#define InA2 4
#define PWM1 3
int ldr1 = 1; int ldr2 = 2;
int valor1; int valor2; int dif;
int mindif = 40;
unsigned long tempo;

void setup() {
Serial.begin(9600);
pinMode(InA1, OUTPUT);
pinMode(InA2, OUTPUT);
pinMode(PWM1, OUTPUT);
}

void loop() {
valor1 = analogRead(ldr1);
valor2 = analogRead(ldr2);
dif = abs(valor1 - valor2);
info();
if (dif <= mindif) {motorP();}
else if (dif > mindif) {motorH(127);}
else {motorAH(127);}
}

void motorP() {
digitalWrite(InA1, LOW);
digitalWrite(InA2, LOW);
analogWrite(PWM1, 0);
}

void motorH(int PWM_val) {
digitalWrite(InA1, HIGH);
digitalWrite(InA2, LOW);
analogWrite(PWM1, PWM_val);
}

void motorAH(int PWM_val) {
digitalWrite(InA1, LOW);
digitalWrite(InA2, HIGH);
analogWrite(PWM1, PWM_val);
}

void info() {
tempo = micros();
tempo = micros() - tempo;
Serial.print("LDR 1: ");     Serial.print(valor1);
Serial.print("   LDR2: ");   Serial.print(valor2);
Serial.print("   Erro: ");   Serial.print(dif);
Serial.print("   DT: ");     Serial.println(tempo);
}
``````

So, I make the reading of 2 ldr’s and calculate the reading difference betwwen them. This small piece of loop code could take more than 4 microseconds for Arduino to process, and then create a delay on the micros() reading?

Thanks

in 4 from 4 microseconds?

This phrase does not make sense to me. Can you explain what "in 4 from 4" means?

Does the time of printing and the other functions I mentioned create a delay on the micros() reading?

Yes, but not how you wrote the code. As AWOL points out, you are making the calls to micros() back-to-back, when occurs much faster than the accuracy of micros().

I think you are trying to calculate the amount of time it takes for an operation to complete. In which case, make sure you have declared a global variable called "tempo" as a unsigned long.

``````  void info() {

unsigned long currentDifference = micros() - tempo;   // This subtracts the previously stored micros() value from the current Value.
Serial.print("LDR 1: ");     Serial.print(valor1);
Serial.print("   LDR2: ");   Serial.print(valor2);
Serial.print("   Erro: ");   Serial.print(dif);
Serial.print("   DT: ");
Serial.println(currentDifference);

tempo = micros();    // Store a new value of micros() for calculating the next time info() is called.
}
``````

Each time you call info, "DT" will show the how many micros() have elapsed since the last call to info().

``````  tempo = micros();
tempo = micros() - tempo;
``````

You’re calling ‘micros()’, then immediately afterwards calling it again and subtracting the values. They’re likely to be very close, if not the same value, so you’ll get a very small integer result. Did you mean to do some processing in between the two calls? You need to put one call before the event you want to time, and the other call after it.

``````if (dif <= mindif) {motorP();}
else if (dif > mindif) {motorH(127);}
``````

If you’ve tested to see if something is <= some value and it isn’t, there’s no need to test it again to see if it > than the same value.

omg, too many answers!

First of all, sorry about my english! I’m brazilian, and it’s a little rusty! I really confused myself while writting that thing, James.

I understood what you guys meant. But the “info()” function is called on the loop as you can see in the code, so as I wrote it, the loop is processed and then I call the micros() function, so after all, I calculate the time that this loop takes to be processed! Or I’m completely wrong?

And about AWOL statement… You are right, I wrote the code totally wrong.

It should be like this:

``````dif = abs(valor1 - valor2);
if(dif <= 40)
//stop the motor
else if (valor1>Valor2)
//Run forwards
else
//Run backwards
``````

Thanks and regards to all of you!

But the "info()" function is called on the loop as you can see in the code, so as I wrote it, the loop is processed and then I call the micros() function, so after all, I calculate the time that this loop takes to be processed! Or I'm completely wrong?

I think your wrong:

``````tempo = micros();
tempo = micros() - tempo;
``````

Only calculates the time it took between those two statements, not the main loop cycle time.

Lefty

retrolefty: I think your wrong:

``````tempo = micros();
tempo = micros() - tempo;
``````

Only calculates the time it took between those two statements, not the main loop cycle time.

Lefty

Hmm ok! Got it... So in order to get the time that takes to process the loop, I must the first the running time in the beggining of the loop, calculate the time difference after all the loop and then say something like "lastMicro = micros()" in the end of the loop?

Thanks and regards! =)