I am preparing the code for a POV LED clock and I'm having a little trouble implementing some of the timing. I currently have a line that checks if the time of rotation matches the expected time for a given position to flash an LED. Unfortunately the code takes time to execute, and rarely if ever lines up.
My question is as follows:
Say I have a long variable 123456789 how could I quickly and efficiently change that into 123456000 in order to check for an approximate equality.
I don't want to use any division to save crucial processor time but I can't seem to find a way to use any round function to get such a result.
andrewlumley:
Say I have a long variable 123456789 how could I quickly and efficiently change that into 123456000 in order to check for an approximate equality.
If you want to check two long numbers if they are approximately equal, you create the absolute value of the difference and compare against the interval.
Code for comparing a variable and 123456000 for "approximately equal" within an interval of 1000:
if (labs(variable-123456000)<1000) Serial.println("approximately equal");
andrewlumley:
Say I have a long variable 123456789 how could I quickly and efficiently change that into 123456000 in order to check for an approximate equality.
Dont forget that 10000000 is approximately equal to 09999999...
Okay, so here's where I'm at. I first tried the technique suggested by jurs to check for equality within 100 microseconds. While this method worked, it still meant that this instance jumped around by about 1cm each time which isn't ideal.
Most recently I have been trying to implement a timed interrupt to automatically pulse the LED's every period/4 microseconds for example. This is still in progress.
andrewlumley:
Okay, so here's where I'm at. I first tried the technique suggested by jurs to check for equality within 100 microseconds. While this method worked, it still meant that this instance jumped around by about 1cm each time which isn't ideal.
Most recently I have been trying to implement a timed interrupt to automatically pulse the LED's every period/4 microseconds for example. This is still in progress.
Instead of rounding down to the nearest 1000, it might be better to try rounding to the nearest 1024. This is because the processor works in binary, and 1024 is a round number in binary.
1024 (decimal) equals 10000000000 (binary)
If you have a variable x,then (x >> 10) means "what is left after removing the last ten bits from x".
If you don't know what binary is, now would be a good time to read up on it.