Analog button with noisy switch. What could be the proper way to debounce

Hello

I realised that I installed verry noisy switch on my analog button setup. My code do find the proper switch but onnce in a while not the proper one is decode

I did installed a debounce time of 400 ms to prevent multiple iteractions of the same button press. If I press the same button ten time, I may not received the same button number EX: switc 2 was pressed

2
2
3
2.

Actually, I'm reading the analog value every 400ms. If the switch is in transition, when the reading is done, I may get false reading. Is it the proper way to debounce an analog reading?

void loop()
{
  if(Debouncetimer.done()){
   ReadKeyPress();{
    if (SW>0){
      Serial.println(SW);
      Debouncetimer.start();
    }
   }
  }    
}

Any input appreciated.
Martin

Complete code



#include <neotimer.h>

Neotimer SW_timer = Neotimer(300);
Neotimer Debouncetimer = Neotimer(400);

const unsigned long shortPress = 500;
const unsigned long  longPress = 1000;


#define ANALOG_PIN A0
int SW = 0;           //Switch value
int SW_Prev = 0;      //Previous switch

int SW1 = 1024;       //Analog read values
int SW2 = 280;
int SW3 = 74;
int SW4 = 22;
int SW0 = 12;

int T0 = 0;
int T1 = 0;
int T2 = 0;
int T3 = 0;
int T4 = 0;

int count_Rebounce = 0;

bool FirstClick = true;
bool WaitForUnclick = false;
bool Cycle_Complete = false;    //A complete cycle is done : A click followed by unclick
bool LongPress = false;
bool IN_Debounce_Click = false;
bool IN_Debounce_Unclick = false;
bool Valid_Button = false;

bool S1_Click = false;
bool S2_Click = false;
bool S3_Click = false;
bool S4_Click = false;
bool S1_LongP = false;
bool S2_LongP = false;
bool S3_LongP = false;
bool S4_LongP = false;


int Debounce_Count = 0;
int Debounce_Count_Unclick = 0;
const int Debounce_Count_Target = 50;   //Same value read target 
const int Debounce_Count_Unclick_Target = 20;
const int LongPressTime = 1000;            //If switch is pressed for more then  1 second LongPress = true

void setup() 
{
  Serial.begin(9600);
  SW_timer.start();
  Debouncetimer.start();
}

void loop()
{
  if(Debouncetimer.done()){
   ReadKeyPress();{
    if (SW>0){
      Serial.println(SW);
      Debouncetimer.start();
    }
   }
  }    
}
   

void ReadKeyPress()
  {
  DecodeKey();
  }

void DecodeKey(){
   
    int value = 0;
    value =  (analogRead(ANALOG_PIN));
    
    T0 = abs(value-SW0);
    T1 = abs(value-SW1);
    T2 = abs(value-SW2);
    T3 = abs(value-SW3);
    T4 = abs(value-SW4);

  if (value <20) {
        SW=0;
      }
        else if (T1 < T2){ SW = 1;}
        else if (T2 < T3){ SW = 2;}
        else if (T3 <T4){ SW  = 3;}
        else {SW=4;}
}     


    

A debounce time 0f 10 to maximum 25 ms would be normal.
What controller are You using?
Where is the declaration of the switch button pin? I don't find it.
How is it wired? Schematics would be helpful.

Here's a good topic to read: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

Hello Railroader

I'm using ESP12F

I agree with you that the 400ms debouce is outrageous. But that is the lowest value that I found to prevent a rebounce
For a project I did 25 years ago if I recall, I was using 50 ms.

The declaration is:

#define ANALOG_PIN A0 // at line 12

Similar circuit not the same resistors value. The capacitor is not installed
Image insérée

Martin

Thanks for the schematics!
That 100k is a bit too much in my opinion. Try using lower values like 47k, 27k, 10k.

Railroader,
The resistor that I use are not the same as the shematic. I took resistor that I had on hand, I will look to desolder and reinstall using these value.

I'm thinking the possibility to use another concept of debounce
Instead of a delay, look for a series of the same SW # value in a row. Everytime the SW is not the same, reset the counter. If the switch is stable, meaning no debounce, I should always read the same value in series
Don't know how it is feasable,

Martin

A huge mistake, if 100nF does not help put 1uF, it is possible that you don't need debounce delays.

There's a button library and plenty of tutorials for debouncing. Why guess, fail and have trouble?

You want your analog input impedance to be 10K or less. If it is more you will get funny readings. Also read the button several times and use it the third time or whatever it has remained stable. When I have a lot of buttons I debounce them eight at a time. The external cap is your best debounce.

Can you reveal what values they are and which Arduino?

It's only a bleed resistor, in case no button is pressed.
Leo..

I know.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.