Short press and long press of switches

From Microduino Wiki
Revision as of 07:25, 18 September 2017 by Fengfeng (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Sensor_Key Library-Sample of Switches’ Long Press and Short Press


In the following samples, Core is used to detect the [long press] and [short press] actions of digital, analog, and virtual switches.

Required Hardware


Sensor-Crash setNumber module.jpg



Circuit Assembly

Connect Crash to pin A0/A1 on Hub.
Stack Battery, Core and Hub together, use MicroUSB cable to connect them to computer. For initial use, please refer to: Getting Started

Sensor-Crash circuit.jpg



Code

  • Identify the [long press] and [short press] actions within corresponding sections of analog sensors.
#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);
}


  • Identify digital sensors’ [long press] and [short press] actions within corresponding sections.
#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);
}


  • Identify [long press] and [short press] actions of corresponding states of analog switches.
#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



Return to Sensor-Crash