Skip to main content
added 92 characters in body
Source Link
Trevor_G
  • 47k
  • 8
  • 73
  • 158

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

Your in-line solution is problematic for many reasons, not the least of which is your time will vary depending on what else is happening in the micro unless you stop and tight loop while you count. But stopping to count for five seconds is also a bad idea.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second.

That interrupt handler can take care or numerous housekeeping functions includinglike debouncing switches, flashing LEDs, checking for communication timeouts etc. It can also include counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

Your in-line solution is problematic for many reasons, not the least of which is your time will vary depending on what else is happening in the micro unless you stop and tight loop while you count. But stopping to count for five seconds is also a bad idea.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second.

That interrupt handler can take care or numerous housekeeping functions including counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

Your in-line solution is problematic for many reasons, not the least of which is your time will vary depending on what else is happening in the micro unless you stop and tight loop while you count. But stopping to count for five seconds is also a bad idea.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second.

That interrupt handler can take care or numerous housekeeping functions like debouncing switches, flashing LEDs, checking for communication timeouts etc. It can also include counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

added 263 characters in body
Source Link
Trevor_G
  • 47k
  • 8
  • 73
  • 158

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

Your in-line solution is problematic for many reasons, not the least of which is your time will vary depending on what else is happening in the micro unless you stop and tight loop while you count. But stopping to count for five seconds is also a bad idea.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second.

That interrupt handler can take care or numerous housekeeping functions including counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second.

That interrupt handler can take care or numerous housekeeping functions including counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

Your in-line solution is problematic for many reasons, not the least of which is your time will vary depending on what else is happening in the micro unless you stop and tight loop while you count. But stopping to count for five seconds is also a bad idea.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second.

That interrupt handler can take care or numerous housekeeping functions including counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

added 123 characters in body
Source Link
Trevor_G
  • 47k
  • 8
  • 73
  • 158

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second. 

That interrupt handler can take care or numerous housekeeping functions including counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second. That interrupt handler can take care or numerous housekeeping functions including counting how long a button has been pressed.

Your code seems to be a bit off, and is hard to really understand since it is not clear where that while loop sits with respect to the interrupt or whatever sets the DownCount function.

You also seem to be incrementing the debounce counter in two places in the while loop, which just seems wrong.

This kind of function is normally handled in micros by using a tick-counter aka heart-beat. Basically it is common practice to reserve one timer to act as a clock for your code. The timer would generate a regular interrupt, perhaps 10 times a second. 

That interrupt handler can take care or numerous housekeeping functions including counting how long a button has been pressed. When it has been pressed an appropriate length of time, said handler then calls the appropriate handler for that event.

This method lets you use a single timer for numerous functions.

Source Link
Trevor_G
  • 47k
  • 8
  • 73
  • 158
Loading