debounce single button from rotary encoder button

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I need a working debounce code for a single push button. This push button I use common shaft rotary encode type which consist of a Push Button.  It is not a momentary switch.  Pls don't ask me to use momentary switch as I need to use this encoder type.

 

I use the below code which is not working properly. The error is it the Mode jump from 1,2,0, 1,2,0 ..It doesn't go into Mode 3. Even I change the debounce to 6 states, it is still working.  

 

Any ideas to fix this from the below code. 

 

 

 

 

 

 


void debounce(void){
	//Counter for number of equal states
	static uint8_t count = 0;
	//Keep track of current states
	static uint8_t button_state = 0;
	
	//Check if button is high or low for the moment
	uint8_t current_state = (~PINA & BUTTON_MASK) !=0;
	
	if (current_state != button_state){
		//button state is about to be change
		count++;
		if (count >=4){
			//The button have not bounced for four state, change state
			button_state = current_state;
			//if the button was pressed, tell me so
			if (current_state !=0) {
				button_down =1;
				
			}
			count =0;
		}
		} else {
		count = 0;
	}
	
}






int main(){
    

	while (1)
	{
        debounce();			
	    
        if (button_down)
           {
	           button_down = 0;//reset flag
	       Mode ++;
	        //disp.setNumber(mode);
	       if (Mode==4)  Mode = 0;
	     	switch (Mode)
	         {
			case 0 ://nothing
	         bla....
	         
	         }
           }
	}
}

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need a working debounce code for a single push button. This push button I use common shaft rotary encode type which consist of a Push Button.  It is not a momentary switch.  Pls don't ask me to use momentary switch as I need to use this encoder type. 

 Do you bother to read what you wrote??? 

You want a single pushbutton which consists of a pushbutton that is not a momentary switch that is an encoder type.

So what kind of switch is on your encoder?, It almost certainly is a momentary switch type (you can spin the knob and momentarily press it).  If you claim it is not a momentary action, what is it ???

What does the switch have to do with the encoder at all??

 

Where is your timer for debouncing???  How are you keeping track of time?? WHAT IS YOUR PLAN??

Have you bothered to look at any examples of switch debouncing?

 

Here is some great debounce info that will keep you from shaking so much:

https://www.avrfreaks.net/forum/...

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Apr 9, 2022 - 03:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hamisu wrote:
Any ideas to fix this from the below code. 
Is button_down initialized?

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hamisu wrote:
This push button I use common shaft rotary encode type which consist of a Push Button.  It is not a momentary switch

 

 

He needs a rotary encoder help.

 

Edit: No, I am no longer sure... should be a simple push...

 

OP, please, do clarify your requirements. Do you use 3 pins of Rotary encoder, or 2 pins of Pushbutton?

Last Edited: Sun. Apr 10, 2022 - 06:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you want a circuit like this:

 

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No. I think there is some misunderstanding. I am using rotary control switch which is momentary switch. My stuff work now. The issue is not the switch nor is the Snigelen bouncing code. is my coding error.  Pls close the thread. Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think there is some misunderstanding.

Why did you bring up encoder?? What does that have to do with the situation? 

 It is not a momentary switch. 

 I am using rotary control switch which is momentary switch. My stuff work now  

These statements are at odds, so that is how you got different advice.

Did you add the needed timer?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Apr 11, 2022 - 10:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just took delivery of...

 

 

So I guess this is the sort of thing OP is talking about? Each has 5 contacts: Vcc, Gnd then A, B for the encoder and a 5th switch connection because they are click encoders and can be clicked to activate the switch (typically to reset the value being adjusted to default). There's nothing "clever" about the click switch. You just debounce it like you would for any click-to-make switch. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

and can be clicked to activate the switch

Not sure why there was so much trouble saying so!

 

Everything seems to come with those ubiquitous knobs  ...must be a giant warehouse of 8 billion of them. Report to the knob factory at 8 am & get the forklift moving.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I love Aliexpress. I got 30 encoders. Each mounted on a breadboard /veroboard friendly PCB with 0.1" pitch pins and each with a coloured plastic knob for about £0.50 each. Basically the complete front panel of a fairly complex synth for half nothing. Wonderful stuff. In fact the little PCB on each has a whole bunch of discretes which I suspect are R-C's for hardware debounce.