Skip to main content
4 of 6
deleted 23 characters in body
rrz0
  • 1.2k
  • 1
  • 22
  • 44

Software Debouncing to detect length of switch press

I need to detect if a switch has been pressed for longer than a set time, without using any Timer registers in my embedded code.

I am using software debouncing in the following manner to detect a normal switch press.

int buttonPress(void)   {
static int downCount= 0; // static = value isn't lost between calls.

int currentButton = ((IO0PIN & 0x00000200) == 0x00000200); 

  if (currentButton ) { // button is down
    
    if (downCount == 5) { // button has been down for 5 counts
      downCount++;      // increase the count so we don't trigger next time
      return 1;
  } else {              // some count other than 5
    if (downCount < 5)  // increase if it's less than 5
       downCount++;
    }
  } else   {    // button is up
    downCount = 0; 
    }
  return 0;
}   

However, I would like to detect whether this button has been pressed for 5 seconds or longer. The problem I am facing is that I cannot seem to be able to count up to 300,000 and if this is not reached, fall back on the original downCount of 5 to mark as a normal button press.

(Since the clock speed for my micro-controller is at 60 MHz, then it will take 5 seconds to reach 300,000.)

I have played around with some logic as follows, however in the case shown below,due to the introduction of the while statement, I am counting up to 300,000 irrespectively of how long the button has been pressed.

if (currentButton ) {   // button is down

  if (downCount == 5) { // button has been down for 5 counts
      downCount++;      // increase the count so we don't trigger next time
            while (downCount > 5) { 
                if (debounce == 300000) {
                    flag_5SEC = 1;
                 }
                else if (debounce < 300000) {
                    debounce++;
                }
            }
      return 1;
    } else {              // some count other than 5
      if (downCount < 5)  // increase if it's less than 5
         downCount++;
    }
  }

Any tips or suggestions on how to go about this task?

Note: I want to detect for a longer switch press explicitly in the debouncing code above, since doing anything else may have adverse side-affects on the rest of the code, which someone else had previously wrote.


I have managed to complete the aforementioned task using timers, however, since then, I have used all timer registers (and match resisters that are available) for more important tasks within the same project.

I do not need to detect an accurate time delay, simply if the button has been down for longer than, say, 2-3 seconds.

rrz0
  • 1.2k
  • 1
  • 22
  • 44