-
Notifications
You must be signed in to change notification settings - Fork 2
/
Example-03-2_Demux.ino
102 lines (83 loc) · 3.39 KB
/
Example-03-2_Demux.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Using Keydetector library to decode signals from PS3 controller multiplexed into single analog line. Listing 2.
(Listing 1 can be found in "/examples/Example-03_Multiplexed/Example-03-1_Mux" directory).
This part of the example demonstrates how to use KyDetector to decode signals from PS3 controller previously
encoded into single analog line (as seen in Listing 1).
Currently pressed button of the controller (D-pad keys, cross, or circle) will be printed.
In addition, axis values from left stick of the PS3 DualShock Controller (or single available stick in case of
PS3 Navigation Controller) will be printed as well. Analog values from the stick are transmitted through
separate wires.
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 the buttons
#define KEY_UP 1
#define KEY_RIGHT 2
#define KEY_DOWN 3
#define KEY_LEFT 4
#define KEY_O 5
#define KEY_X 6
// Define variables to hold axis values
int x, y;
// Signal pins
const byte kPin = A2; // Pin for multiplexed signal
const byte xPin = A1; // Pin for X axis signal
const byte yPin = A0; // Pin for Y axis signal
// Create array of Key objects that will link defined key identifiers with dedicated pins
Key keys[] = {{KEY_UP, kPin, 127}, {KEY_RIGHT, kPin, 255}, {KEY_DOWN, kPin, 383}, {KEY_LEFT, kPin, 511}, {KEY_O, kPin, 639}, {KEY_X, kPin, 767}};
// Create KeyDetector object
KeyDetector key(keys, sizeof(keys)/sizeof(Key));
// Note that you can increase threshold value to achieve more accurate detection (e.g. in case of high signal ripple
// or inaccuracy of the DAC used to encode signals). To do so, use the following line instead (where 24 is the custom
// value of threshold):
// KeyDetector key(keys, sizeof(keys)/sizeof(Key), /* debounceDelay= */ 0, /* analogThreshold= */ 24);
// Additionally you can specify debounceDelay value to account for any transient process that may occur when adjusting
// the source level of analog signal:
// KeyDetector key(keys, sizeof(keys)/sizeof(Key), /* debounceDelay= */ 5, /* analogThreshold= */ 24);
void setup() {
// Configure the reference voltage used for analog input (i.e. the value used as the top of the input range)
analogReference(EXTERNAL);
// Serial communications setup
Serial.begin(115200);
// Set signal pins to input
pinMode(kPin, INPUT);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
void loop() {
// Get X value of the stick
x = analogRead(xPin);
delay(10);
// Get Y value of the stick
y = analogRead(yPin);
// Print X and Y values. Thresholds (500 and 524) used here help prevent false readings near the mid-point of the range
if (x > 524 || x < 500 || y > 524 || y < 500) {
Serial.print("X: "); Serial.print(x);
Serial.print("\tY: "); Serial.println(y);
}
// Check the current state of input signal
key.detect();
// When button press is detected ("triggered"), print corresponding message
switch (key.trigger) {
case KEY_UP:
Serial.println("UP");
break;
case KEY_RIGHT:
Serial.println("RIGHT");
break;
case KEY_DOWN:
Serial.println("DOWN");
break;
case KEY_LEFT:
Serial.println("LEFT");
break;
case KEY_O:
Serial.println("O");
break;
case KEY_X:
Serial.println("X");
break;
}
}