States and actions of switches

From Microduino Wiki
Revision as of 08:41, 18 September 2017 by Fengfeng (talk | contribs) (Created page with "{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''Sensor_Key Library-Samples of Switches’ States and Actions'''</p> <br> In the following samples, Co...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Sensor_Key Library-Samples of Switches’ States and Actions


In the following samples, Cores are used to detect digital, analog, and virtual switches’ [release] and [press] actions and states.

Required Hardware


Sensor-Crash setNumber module.jpg



Circuit Assembly

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

Sensor-Crash circuit.jpg



Code

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


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


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



Return to Sensor-Crash