-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalogstick.ino
84 lines (73 loc) · 2.53 KB
/
analogstick.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
/***********************************************************************************************************************
* Intro: A single analog stick.
* Arduino reads its raw values, filters(Radial Scaling method) them and
* prints to serial port when there is an change.
*
* Board: Arduino Pro Mini ATmega328 (3.3V, 8MHz)
*
* Connections: Type Pro Mini Analog Stick
* Output A2 VRx
* Output A3 VRy
* Output VCC +5V
* Output GND GND
*
**********************************************************************************************************************/
//Radial Scaling Constants
const int rMax = sqrt(2 * pow(512, 2));
const int deadzone = 0.20 * rMax;
const int rScaledMax = rMax / (rMax - deadzone);
int xPrev = 1024;
int yPrev = 1024;
/**********************************************************************************************************************
* Static Functions
*********************************************************************************************************************/
static int stickTranspose(int val) {
if (val > 512) {
return val - 511;
} else if (val < 511) {
return val - 512;
} else {
return 0;
}
}
static void stickPrint(int *x, int *y, int valx, int valy) {
if (*x != valx || *y != valy) {
if (*x != valx) {
*x = valx;
}
if (*y != valy) {
*y = valy;
}
Serial.print(F("("));
Serial.print(*x);
Serial.print(F(","));
Serial.print(*y);
Serial.print(F(")"));
Serial.println();
}
}
/**********************************************************************************************************************
* Setup
*********************************************************************************************************************/
void setup() {
Serial.begin(57600);
}
/**********************************************************************************************************************
* Main
*********************************************************************************************************************/
void loop() {
int xCurr, yCurr, r;
xCurr = stickTranspose(analogRead(A2));
delay(100);
yCurr = stickTranspose(analogRead(A3));
r = sqrt(pow(xCurr, 2) + pow(yCurr, 2));
if (r < deadzone) {
xCurr = 0;
yCurr = 0;
} else {
int rScaled = r / (r - deadzone) ;
xCurr = rScaledMax / rScaled * xCurr;
yCurr = rScaledMax / rScaled * yCurr;
}
stickPrint(&xPrev, &yPrev, xCurr, yCurr);
}