How critical is the fractional portion of the frequency? If you follow Coding Badly, change the floats to uint32_t, do direct port read/write (get rid of the digitalRead/Write) and a couple of other small changes...
Sketch uses 472 bytes (46%) of program storage space. Maximum is 1024 bytes.
Global variables use 18 bytes (28%) of dynamic memory, leaving 46 bytes for local variables. Maximum is 64 bytes.
Sketch uses 290 bytes (28%) of program storage space. Maximum is 1024 bytes.
Global variables use 6 bytes (9%) of dynamic memory, leaving 58 bytes for local variables. Maximum is 64 bytes.
You realize that 1 / ( Duration / 1000 ) is the same as 1000/Duration and then if you are going to test for (1000/Duration) being between 95 and 105 it's just as easy to test for values of Duration between 9 and 10. If you need more resolution, use micros().
How is it wrong? what do mean? I want only the main function to access it and what's wrong with me declaring it static to make the program alittle more efficient?
MikeLemon:
What do you mean by may not be accurate?
The base clock for a t13 is 9.6 MHz. There is no way to configure an AVR timer to divide that down to a 1 millisecond nor a 1 microsecond clock.
I'll use microseconds for the example. To get microseconds from a 9.6 MHz clock you would have to divide by 9.6. There is no way to perform a fractional (the "0.6" part) division with an AVR timer. You could divide by 9. You could divide by 10. But you cannot divide by exactly 9.6.
One strategy is to tune the processor (using OSCCAL) to run at 8 MHz.
The correct datatype for values return from millis / micros is always an unsigned integer; typically unsigned long. Using any other datatype causes problems.
...what's wrong with me declaring it static to make the program alittle more efficient?
Duration is ephemeral. The value only has meaning for one pass through loop.
Making Durationstatic forces the value to be stored in SRAM. Something that is not just unnecessary but a waste of SRAM.
Saving Duration to SRAM requires code. If Duration is not stored to SRAM that code is eliminated.
But from what I understood that static allows you to declare a variable with an initial value in a loop still using it throughout(changing it and stuff) and won't be re declared to that initial one at next cycle.
Also Tryed some of this port manipulation this but when I try to write a binary value to it it says that:
57: error: 'B00000000' was not declared in this scope