Difference between revisions of "Short press and long press of switches"
From Microduino Wiki
Line 19: | Line 19: | ||
Connect Crash to pin A0/A1 on Hub. | Connect Crash to pin A0/A1 on Hub. | ||
<br> | <br> | ||
− | Stack Battery, Core and Hub together, use MicroUSB cable to connect them to computer. For initial use, please refer to: [[AVR Core: Getting_Started |Getting Started]] | + | Stack Battery, Core and Hub together, use MicroUSB cable to connect them to computer. For initial use, please refer to: [[AVR Core: Getting_Started |Getting Started]] |
<br> | <br> | ||
[[File:Sensor-Crash_circuit.jpg|600px|center]] | [[File:Sensor-Crash_circuit.jpg|600px|center]] |
Latest 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
|