MajicDesigns / MD_UISwitch

Uniformly encapsulate different types of switches as user input devices
GNU Lesser General Public License v2.1
40 stars 17 forks source link

False positive for KEY_SINGLE #2

Closed fleetingbytes closed 6 years ago

fleetingbytes commented 6 years ago

Hello Marco,

I have tried your UI_Switch library with the test code below and I'm getting a false positive KEY_SINGLE detect about 1 in 20 times or even more often when I press the standard Arduino push button (attached image borrowed from ladyada.net) 6mmswitch_t

I used the following code (also attached):

#include <MD_UISwitch.h>

// Define what type of testing is being done
#define TEST_DIGITAL_SIMPLE 1

#if TEST_DIGITAL_SIMPLE
#define TITLE "Simple Digital"

const uint8_t DIGITAL_SWITCH_PIN = 2;       // switch connected to this pin
const uint8_t DIGITAL_SWITCH_ACTIVE = LOW;  // digital signal when switch is pressed 'on'

MD_UISwitch_Digital S(DIGITAL_SWITCH_PIN, DIGITAL_SWITCH_ACTIVE);
#endif

void setup() {
  Serial.begin(57600);
  Serial.print(F("\n[MD_UISwitch "));
  Serial.print(F(TITLE));
  Serial.print(F(" Example]"));

  S.begin();
  S.enableDoublePress(false);
  S.enableLongPress(false);
  S.enableRepeat(false);
  S.enableRepeatResult(true);

}

void loop() {
  // put your main code here, to run repeatedly:
  MD_UISwitch::keyResult_t k = S.read();

  switch(k)
  {
    case MD_UISwitch::KEY_NULL:      /* Serial.println("NULL"); */  break;
    case MD_UISwitch::KEY_PRESS:     Serial.print("\nKEY_SINGLE "); break;
    case MD_UISwitch::KEY_DPRESS:    Serial.print("\nKEY_DOUBLE "); break;
    case MD_UISwitch::KEY_LONGPRESS: Serial.print("\nKEY_LONG   "); break;
    case MD_UISwitch::KEY_RPTPRESS:  Serial.print("\nKEY_REPEAT "); break;
    default:                         Serial.print("\nKEY_UNKNWN "); break;
  }
  if (k != MD_UISwitch::KEY_NULL)
  {
    if (S.getKey() >= ' ')
    {
      Serial.print((char)S.getKey());
      Serial.print(" ");
    }
    Serial.print("[0x");
    Serial.print(S.getKey(), HEX);
    Serial.print("]");
  }

}

Debounce_test.zip

MajicDesigns commented 6 years ago

The switch you show is called a 'tact' switch and is a momentary-on keyswitch.

Explain what you mean by a 'false positive KEY_SINGLE' as there is no explanation of what you expect to see, what you are seeing and why that should be considered a problem?

fleetingbytes commented 6 years ago

Well, most of the time, when I press the button once, the serial console writes a singe event KEY_SINGLE. But sometimes, about 1 in 20 times or more often I get two such events after just one press.

MajicDesigns commented 6 years ago

OK. It does not happen for me so if you find specific reasons for it let me know.