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:
"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?
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?
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?
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().
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.
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
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.
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?