majicdesigns / md_uiswitch Goto Github PK
View Code? Open in Web Editor NEWUniformly encapsulate different types of switches as user input devices
License: GNU Lesser General Public License v2.1
Uniformly encapsulate different types of switches as user input devices
License: GNU Lesser General Public License v2.1
Hi.
Do you have any plan for using I2C expander PCF8574 or other expanders?
It could be nice if you also can expand you library with some I2C expanders.
It's a very good library to handle buttons.
Hello,
The navigation doesn't work correctly with this component some button work but randomly, i try to change the debounce time but nothing change, the solution is to add a delay(50) in the MD_UISwitch::keyResult_t MD_UISwitch_Analog::read(void) function, and with this, all work fine.
// work out what key this is
for (uint8_t i = 0; i < _ktSize; i++)
{
if ((v >= (_kt[i].adcThreshold - _kt[i].adcTolerance)) &&
(v <= (_kt[i].adcThreshold + _kt[i].adcTolerance)))
{
idx = i;
UI_PRINTS("\nV : ");
UI_PRINTS(v);
UI_PRINTS("\nACT : ");
UI_PRINTS(_kt[i].adcThreshold);
delay(50); // Ajout d'un délai pour éviter le bug des boutons qui fonctionne mal
break;
}
}
The programme works fine without this delay modification with the DFRobot shield LCD Keyshield 1.1 (after change the key value of course).
Best regards
Before submitting this issue
[ ] Have you tried using the latest version of the library?
YES
[ ] Have you checked this has not already been submitted and/or resolved?
YES
[ ] If you are requesting help a better choice may be the Arduino forum
CONSIDERED
My aim was to use this library to detect button presses for a MIDI foot pedal. I am using an UNO and have wired up 4 momentary switches in the internal pullup mode to 4 digital pins. The sample application works, but I made 2 small change, i.e.:
BTN[i]->setPressTime(250);
BTN[i]->setLongPressTime(1200);
which get me close the the more typical button press behaviour seen in such pedals, since otherwise, with default values most typical short presses were also being detected as long presses. However, I notice to behaviours which I'd like to change if possible. One is, that once in every 3-5 long-presses, I get one which is followed up by a "ghost short press". This is quite consistent, and this "ghost short press" seems to happen when I release the button after a long press.
Another observation is that long presses that are perhaps a bit too long, get detected as a short press (for the "press" transition) and another short press (apparently for the "release" transition). Can this behaviour be altered in the way that a long press, could be somewhat longer long-press can be had ?
Library Version: 2.1
Arduino IDE version: 1.8.10
Hardware model/type: Arduino UNO clone (w/ CH340 chip)
OS and Version: Lubuntu 19.10 64-bit on host PC
Hello Marco,
I see an issue with the debouncing algorithm. It doesn't ignore presses shorter than the debounce time. When I press a single digital button shorter than the duration set in setDebounceTime(), the keyResult_t still returns a KEY_SINGLE event.
Isn't the whole idea of debouncing such that only after the button has been continuously and steadily pressed for the time defined in setDebounceTime(), only then it is supposed to send the logical signal of a keypress?
I tested this with a purposefully long debounce time of 400 ms:
#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);
setDebounceTime(400);
}
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("]");
}
}
I'm really digging this library, great work.
I am trying to detect when two buttons are held simultaneously for a period of time (longpress).
I've tried a basic IF statement using both switches KEY_DOWN during the loop but have had no luck.
Thanks,
Pete
I'm having an issue with the library where it does't seem to handle having multiple instances of the MD_UISwitch_Digital class instantiated in the code. Not sure if I have something configured wrong, but based on the documentation, this seems correct. (See below.)
Library Version: 2.0.0
Arduino IDE version: 1.8.8
Hardware model/type: ESP8266 / Wemos D1
With the code below, if you don't define MDUISWITCH_ENABLE and log the raw button states, the code outputs correctly. (In my case B1 and B2 have external pull-downs, B3 uses an internal pull-up, but the issue presents itself regardless of which buttons are wired up where...)
When using MD_UISwitch_Digital, BTN2 and BTN3 do not respond to any changes on the GPIO pins. For example, a single press of BTN1 yields:
B3: KEY_DOWN
B1: KEY_DOWN
B3: KEY_UP
B1: KEY_UP
B3: KEY_PRESS
B1: KEY_PRESS
BTN2 doesn't respond at all, although it does work correctly without MD_UISwitch.
BT3 single push generates:
B2: KEY_DOWN
B3: KEY_DOWN
B1: KEY_DOWN
B2: KEY_UP
B3: KEY_UP
B1: KEY_UP
B2: KEY_PRESS
B3: KEY_PRESS
B1: KEY_PRESS
If I downgrade to 1.2.1, things work fine, so this is something new in 2.0.
#include <MD_UISwitch.h>
#define MDUISWITCH_ENABLE
#ifdef MDUISWITCH_ENABLE
MD_UISwitch_Digital BTN1(D1, HIGH);
MD_UISwitch_Digital BTN2(D2, HIGH);
MD_UISwitch_Digital BTN3(D7, LOW);
#endif
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
pinMode(D1, INPUT);
pinMode(D2, INPUT);
pinMode(D7, INPUT_PULLUP);
#ifdef MDUISWITCH_ENABLE
BTN1.begin();
BTN2.begin();
BTN3.begin();
#endif
}
void loop() {
#ifndef MDUISWITCH_ENABLE
bool b1State = digitalRead(D1);
bool b2State = digitalRead(D2);
bool b3State = digitalRead(D7);
Serial.println("B1: " + String(b1State) + ", B2: " + String(b2State) + ", B3: " + String(b3State));
#endif
#ifdef MDUISWITCH_ENABLE
MD_UISwitch::keyResult_t b1_result = BTN1.read();
logButtonState("B1", b1_result);
MD_UISwitch::keyResult_t b2_result = BTN2.read();
logButtonState("B2", b2_result);
MD_UISwitch::keyResult_t b3_result = BTN3.read();
logButtonState("B3", b3_result);
#endif
delay(100);
}
void logButtonState(const char* buttonName, MD_UISwitch::keyResult_t state)
{
switch (state)
{
case MD_UISwitch::KEY_NULL: /* Serial.print("KEY_NULL"); */ break;
case MD_UISwitch::KEY_UP: Serial.printf("%s: KEY_UP\n", buttonName); break;
case MD_UISwitch::KEY_DOWN: Serial.printf("%s: KEY_DOWN\n", buttonName); break;
case MD_UISwitch::KEY_PRESS: Serial.printf("%s: KEY_PRESS\n", buttonName); break;
case MD_UISwitch::KEY_DPRESS: Serial.printf("%s: KEY_DPRESS\n", buttonName); break;
case MD_UISwitch::KEY_LONGPRESS: Serial.printf("%s: KEY_LONGPRESS\n", buttonName); break;
case MD_UISwitch::KEY_RPTPRESS: Serial.printf("%s: KEY_RPTPRESS\n", buttonName); break;
default: Serial.printf("%s: KEY_UNKNWN\n", buttonName); break;
}
}
I using your fantastic library for my PedalinoMini project but with the latest version 2.0 nothing is working ad before. Most of the time press is not reported or block the execution.
PedalinoMini is working on ESP32 board with Arduino framework.
Thanks
alf45tar
I have 10 push button? Any example how to use your library to use with multiple button? thanks
Before submitting this issue
I am unable to get a long press event to fire.
Library Version: 2.1.0
Arduino IDE version: 1.8.13
Hardware model/type: ESP8266
OS and Version: Windows 10
Using a momentary-on switch wired like this, I've been able to get a single press response and a double press response, but not a long press response.
When I press and hold the button, after I let go, a long press response should have been received.
If I press and hold the button, I receive a single press response while the button is still held down. I do not ever get a long press response.
I'm running the code block before. On the button, I will do a single press, then a double press, then press and hold for long press and paste the Serial Monitor output.
#include <MD_UISwitch.h>
#define BUTTON_PIN 14 // GPIO Pin
MD_UISwitch_Digital Button(BUTTON_PIN, HIGH);
void setup() {
Serial.begin(115200);
Button.begin();
Button.enableDoublePress(true);
Button.enableLongPress(true);
Button.enableRepeatResult(true);
}
void loop() {
MD_UISwitch::keyResult_t k = Button.read();
switch(k) {
case MD_UISwitch::KEY_UP: Serial.print("\nKEY_UP "); break;
case MD_UISwitch::KEY_DOWN: Serial.print("\n\nKEY_DOWN "); break;
case MD_UISwitch::KEY_PRESS:
Serial.print("Single press");
Serial.println("\n");
break;
case MD_UISwitch::KEY_DPRESS:
Serial.print("Double press");
Serial.println("\n");
break;
case MD_UISwitch::KEY_LONGPRESS:
Serial.print("Long press");
Serial.println("\n");
break;
}
}
The Serial Monitor reads:
KEY_DOWN
KEY_UP Single press
KEY_DOWN
KEY_UP
KEY_DOWN
KEY_UP Double press
KEY_DOWN Single press
KEY_UP
I really appreciate the library and you taking the time to address the issue. I'm pretty new at this Arduino stuff, and the project I'm working on is the first C++ I've ever written. If it's user error, I humbly apologize for wasting your time!
Hello,
pressing 2 keys simultaneously is often used to enter some kind of setup or factory default procedures.
I am facing the problem to detect 2 simultaneously pressed keys with MD_UISwitch_Digital().
Pressing one button for long time results in 'key repeat' answers from .read method.
When pressing the second button, the .read method answers 'KEY_NULL' as if no button is pressed.
regards
Wolfgang
Good day.
I need some help to solve a extrange issue.
I have a compilation error seems because some issue with the library code, when try to compile example sketch for three analog switch buttons attached on a ESP8266 Wemos D1 mini:
"Arduino\libraries\MD_UISwitch\src\MD_UISwitch.cpp:43:5: error: return-statement with no value, in function returning 'MD_UISwitch::keyResult_t' [-fpermissive]"
Below my code (little changed from example to test only analog switch readings), but I too tried directly the example sketch downloaded with the library with same result:
`#include <MD_UISwitch.h>
#define TITLE "Analaog Input"
const uint8_t ANALOG_SWITCH_PIN = A0; // switches connected to this pin
// These key values work for most LCD shields
MD_UISwitch_Analog::uiAnalogKeys_t kt[] =
{
{ 672, 15, 'R' }, // Right
{ 274, 15, 'C' }, // Left
{ 145, 15, 'L' }, // Down
{ 945, 15, 'D' }, // Right+Center
{ 405, 15, 'I' }, // LEft+Center
};
MD_UISwitch_Analog S(ANALOG_SWITCH_PIN, kt, ARRAY_SIZE(kt));
void setup(void)
{
Serial.begin(115200);
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()
{
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("]");
}
`}````
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)
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("]");
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.