Short press and long press of switches
From Microduino Wiki
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
|