We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Debouncing a button
Page Index Toggle Pages: 1
Debouncing a button (Read 872 times)
Debouncing a button
May 4th, 2009, 8:40am
 
created a simple button class and I am trying to add debouncing can anyone make any suggestions?

I look for button presses with:
if(myButton.buttonPressed())

Thanks.

Code:


class button {

//button locations
 int buttonLocX;
 int buttonLocY;

//buton sizes
 int buttonWidth;
 int buttonHeight;


 button(int x, int y, int x1, int y1){
   buttonLocX = x;
   buttonLocY = y;
   int buttonWidth = x1;
   int buttonHeight = y1;
 }
 
 boolean buttonRoll(){   //look for mouse over button
   if (mouseX>buttonLocX-5 && mouseX<buttonLocX+x1 &&  mouseY>buttonLocY-5 && mouseY<buttonLocY+y1){
   return true;
 }
   else{
  return false;
}
 }
 
boolean buttonPressed(){
if (this.buttonRoll() && mousePressed){
     return true;
} else {
 return false;
}
}

}
Re: Debouncing a button
Reply #1 - May 4th, 2009, 1:36pm
 
You will probably want to detect when the mouse was pressed and store it in another boolean.

Basic idea:
buttonClicked = mouseWasPressed && !mouseIsPressedNow;
Re: Debouncing a button
Reply #2 - May 5th, 2009, 10:03am
 
What NoahBuddy said. If you need more help, see the Arduino  debounce tutorial:


arduino.cc/en/Tutorial/Debounce
Page Index Toggle Pages: 1