Difference between revisions of "Short press and long press of switches"
From Microduino Wiki
(Created page with "{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''Sensor_Key Library-Sample of Switches’ Long Press and Short Press'''</p> <br> In the following samp...") |
|||
Line 2: | Line 2: | ||
|- | |- | ||
| | | | ||
− | <p style="color: #4F4E4E;font-size: | + | <p style="color: #4F4E4E;font-size:200%">'''Sensor_Key Library-Sample of Switches’ Long Press and Short Press'''</p> |
<br> | <br> | ||
In the following samples, Core is used to detect the [long press] and [short press] actions of digital, analog, and virtual switches. | In the following samples, Core is used to detect the [long press] and [short press] actions of digital, analog, and virtual switches. |
Revision as of 08:07, 18 September 2017
Sensor_Key Library-Sample of Switches’ Long Press and Short Press
Required Hardware
Circuit Assembly Connect Crash to pin A0/A1 on Hub.
Code
#include <Microduino_Key.h>
AnalogKey keyAnalog(A0);
void setup() {
Serial.begin(9600);
keyAnalog.begin(INPUT);
}
void loop() {
switch (keyAnalog.readEvent(0, 50)) { //(analog minimum, analog maximum)
case SHORT_PRESS:
Serial.println("KEY (analog) SHORT_PRESS"); //Short press
break;
case LONG_PRESS:
Serial.println("KEY (analog) LONG_PRESS"); //Long press
break;
}
delay(50);
}
#include <Microduino_Key.h>
DigitalKey keyDigital(A0);
void setup() {
Serial.begin(9600);
keyDigital.begin(INPUT_PULLUP);
}
void loop() {
switch (keyDigital.readEvent()) {
case SHORT_PRESS:
Serial.println("KEY (digital) SHORT_PRESS"); //Short press
break;
case LONG_PRESS:
Serial.println("KEY (digital) LONG_PRESS"); //Long press
break;
}
delay(50);
}
#include <Microduino_Key.h>
VirtualKey keyVirtual;
void setup() {
Serial.begin(9600);
keyVirtual.begin();
pinMode(A0, INPUT_PULLUP);
}
void loop() {
bool val = !digitalRead(A0); //val any bool variables
switch (keyVirtual.readEvent(val,1500)) {
case SHORT_PRESS:
Serial.println("KEY (Virtual) SHORT_PRESS"); //Short press
break;
case LONG_PRESS:
Serial.println("KEY (Virtual) LONG_PRESS"); //Long press
break;
}
delay(50);
}
Relative Cases
|