States and actions of switches
From Microduino Wiki
Sensor_Key Library-Samples of Switches’ States and Actions
Required Hardware
Circuit Assembly Connect Crash to pin A0/A1 of Hub.
Code
#include <Microduino_Key.h>
AnalogKey keyAnalog(A0);
void setup() {
Serial.begin(9600);
keyAnalog.begin(INPUT);
}
void loop() {
switch (keyAnalog.readVal(0, 50)) { //(analog minimum, analog maximum)
case KEY_RELEASED:
Serial.println("KEY (analog) RELEASED---1"); //Released state of the switch
break;
case KEY_PRESSED:
Serial.println("KEY (analog) PRESSED-------2"); //Pressed state of the switch
break;
case KEY_PRESSING:
Serial.println("KEY (analog) PRESSING--------3"); //Pressing action
break;
case KEY_RELEASING:
Serial.println("KEY (analog) RELEASING---------4"); //Releasing action
break;
}
delay(100);
}
#include <Microduino_Key.h>
DigitalKey keyDigital(A0);
void setup() {
Serial.begin(9600);
keyDigital.begin(INPUT_PULLUP);
}
void loop() {
switch (keyDigital.readVal()) {
case KEY_RELEASED:
Serial.println("KEY (digital) RELEASED---1"); //Released state
break;
case KEY_PRESSED:
Serial.println("KEY (digital) PRESSED-------2"); //Pressed state
break;
case KEY_PRESSING:
Serial.println("KEY (digital) PRESSING--------3"); //Pressing action
break;
case KEY_RELEASING:
Serial.println("KEY (digital) RELEASING---------4"); //Releasing action
break;
}
delay(100);
}
#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 one bool variable
switch (keyVirtual.readVal(val)) {
case KEY_RELEASED:
Serial.println("KEY (digital) RELEASED---1"); //Released state
break;
case KEY_PRESSED:
Serial.println("KEY (digital) PRESSED-------2"); //Pressed state
break;
case KEY_PRESSING:
Serial.println("KEY (digital) PRESSING--------3"); //Pressing action
break;
case KEY_RELEASING:
Serial.println("KEY (digital) RELEASING---------4"); //Releasing action
break;
}
delay(100);
}
Relative Cases
|