Measuring duration by using loops
Mason_L
Posts: 5
in BASIC Stamp
Hello. As I'm going through What's a Microcontroller book, I've noticed that there are many occasions where duration of time is roughly measured and controlled by use of loops. For example, by estimating one pass of through a loop to be 1/100th of a second, the for loop would be made to loop for 100 times to last for one second. I'm wondering if this method is used for the sake of simplicity in this introductory text and BS2, or if this is a common method in microcontroller programming.
I've found this technique used often in the servo programming chapter, where a position is needed to be held for specified number of seconds. I already see drawbacks to this technique if for example, some other actions need to be performed while servo position is being maintained, which may increase the time of each pass through the loop.
Are there other frequently used methods in the microcontroller programming world when developed for production use? Thank you for your help.
I've found this technique used often in the servo programming chapter, where a position is needed to be held for specified number of seconds. I already see drawbacks to this technique if for example, some other actions need to be performed while servo position is being maintained, which may increase the time of each pass through the loop.
Are there other frequently used methods in the microcontroller programming world when developed for production use? Thank you for your help.
Comments
The propellor has other options, including the ability to sense when a specific interval has passed.
Thank you.
The Stamp (and Propeller) have no interrupts, and therefore code loops are deterministic. The Stamp can run only one program thread at a time, so the watchword is "simple loops". Anything involving more than one task at a time must be considered in relation to the accuracy required. You might have to "tune" the loop to hit an exact period if that is necessary. In some situations it is quite adequate. You can always add an external real time clock chip to the Stamp circuit.
Today I would code it in the Propeller and have as many channels as I have pins timed with sub-1ms resolution. Times change, as do products. The BASIC Stamp is a great way to get accustomed to programming. When your needs are beyond what the BASIC Stamp can handle, the Propeller is waiting in the wings (and boy is it a lot of fun).
The bs2 does have an internal clock; what it doesn't have is a counter that is visible to the programmer.
As JonnyMac says, learn about programming on BS2 and go on to a more powerful machine when your requirement dictate.