/*
  Digital signal readings using KeyDetector library.

  Demonstrates how to use KeyDetector to trigger action based on digital signal readings from momentary push-buttons.
  Pressing button once or keeping it in a pressed state continuously will print corresponding message. Pressing another
  button in combination with the one already in a pressed state will be detected as well.
  
  Additional info (including the breadboard view) available on GitHub:
  https://github.com/Spirik/KeyDetector
  
  This example code is in the public domain.
*/

#include <KeyDetector.h>

// Define signal identifiers for three buttons. The actual value of defined identifier isn't important for this example
#define KEY_A 1
#define KEY_B 2
#define KEY_C 3

// Pins the three buttons are connected to
const byte pinA = 4;
const byte pinB = 3;
const byte pinC = 2;

const int keyPressDelay = 1000; // Delay after key press event triggered and before continuous press is detected, ms
const int keyPressRepeatDelay = 500; // Delay between "remains pressed" message is printed, ms

long keyPressTime = 0; // Variable to hold time of the key press event
long now; // Variable to hold current time taken with millis() function at the beginning of loop()

// Create array of Key objects that will link defined key identifiers with dedicated pins
Key keys[] = {{KEY_A, pinA}, {KEY_B, pinB}, {KEY_C, pinC}};

// Create KeyDetector object
// KeyDetector myKeyDetector(keys, sizeof(keys)/sizeof(Key));
// To account for switch bounce effect of the buttons (if occur) you may whant to specify debounceDelay
// as the third argument to KeyDetector constructor:
KeyDetector myKeyDetector(keys, sizeof(keys)/sizeof(Key), /* debounceDelay= */ 10);

void setup() {
  // Serial communications setup
  Serial.begin(115200);
  
  // Set button pins to input
  pinMode(pinA, INPUT);
  pinMode(pinB, INPUT);
  pinMode(pinC, INPUT);
}

void loop() {
  // Get current time to use later on
  now = millis();
  
  // Check the current state of input signal
  myKeyDetector.detect();

  // When button press is detected ("triggered"), print corresponding message
  // and save current time as a time of the key press event
  switch (myKeyDetector.trigger) {
    case KEY_A:
      // Determine whether button A was pressed in combination with another one already in a pressed state (B or C)
      switch (myKeyDetector.current) {
        case KEY_B:
          Serial.println("Button A pressed simultaneously with Button B!");
          break;
        case KEY_C:
          Serial.println("Button A pressed simultaneously with Button C!");
          break;
        default:
          Serial.println("Button A pressed!");
          break;
      }
      keyPressTime = now;
      break;

    case KEY_B:
      // Determine whether button B was pressed in combination with another one already in a pressed state (A or C)
      switch (myKeyDetector.current) {
        case KEY_A:
          Serial.println("Button B pressed simultaneously with Button A!");
          break;
        case KEY_C:
          Serial.println("Button B pressed simultaneously with Button C!");
          break;
        default:
          Serial.println("Button B pressed!");
          break;
      }
      keyPressTime = now;
      break;

    case KEY_C:
      // Determine whether button C was pressed in combination with another one already in a pressed state (A or B)
      switch (myKeyDetector.current) {
        case KEY_A:
          Serial.println("Button C pressed simultaneously with Button A!");
          break;
        case KEY_B:
          Serial.println("Button C pressed simultaneously with Button B!");
          break;
        default:
          Serial.println("Button C pressed!");
          break;
      }
      keyPressTime = now;
      break;
  }

  // After keyPressDelay passed since keyPressTime...
  if (now > keyPressTime + keyPressDelay) {
    // ...determine currently pressed button (i.e. button being in a pressed state)
    // and print corresponding message, followed by keyPressRepeatDelay
    switch (myKeyDetector.current) {
      case KEY_A:
        Serial.println("Button A remains pressed.");
        delay(keyPressRepeatDelay);
        break;
      case KEY_B:
        Serial.println("Button B remains pressed.");
        delay(keyPressRepeatDelay);
        break;
      case KEY_C:
        Serial.println("Button C remains pressed.");
        delay(keyPressRepeatDelay);
        break;
    }

  }

}
$abcdeabcde151015202530fghijfghij
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2