-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpingpongplusplus.pde
145 lines (128 loc) · 3.92 KB
/
pingpongplusplus.pde
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* PingPong++
* -------------
* By Michael Bernstein ([email protected]) and Xiao Xiao ([email protected])
*
* The board configuration should look like below.
* Numbers are the default pin locations on the Arduino.
*
* |-------------------------------|
* | 1 2 | 5 6 |
* | | |
* | | |
* | 4 3 | 8 7 |
* |-------------------------------|
*/
struct Hit {
int upperLeft;
int upperRight;
int lowerLeft;
int lowerRight;
unsigned long startTime;
char side;
boolean reported; // have we sent it off via serial yet?
};
int leftPins[] = {
6, 7, 8, 9}; // clockwise from top left
int rightPins[] = {
2, 3, 4, 5}; // clockwise from top left
const unsigned long NO_RECORD = -1;
const unsigned long TIMEOUT_BETWEEN_HITS = 300000; // in millis
const char LEFT = 'l';
const char RIGHT = 'r';
Hit curHit = {
NO_RECORD, NO_RECORD, NO_RECORD, NO_RECORD, NO_RECORD, LEFT, false};
void setup() {
Serial.begin(9600);
//Serial.println("Starting PingPongPlusPlus");
for (int i = 0; i < 4; i++) {
int pinNumber = leftPins[i];
pinMode(pinNumber, INPUT);
}
for (int i = 0; i < 4; i++) {
int pinNumber = rightPins[i];
pinMode(pinNumber, INPUT);
}
}
void loop() {
readPins(leftPins, LEFT);
readPins(rightPins, RIGHT);
if (isCompleteHit(&curHit) && !curHit.reported) {
// print the current hit status
Serial.print("hit: {");
Serial.print(curHit.upperLeft);
Serial.print(" ");
Serial.print(curHit.upperRight);
Serial.print(" ");
Serial.print(curHit.lowerRight);
Serial.print(" ");
Serial.print(curHit.lowerLeft);
Serial.print(" ");
Serial.print(curHit.side);
Serial.println("}");
curHit.reported = true;
}
if (hitTimedOut()) {
// create a new hit
//Serial.println("The old hit timed out. Starting new hit.");
Hit newHit;
newHit.upperLeft = newHit.upperRight = newHit.lowerLeft = newHit.lowerRight = NO_RECORD;
newHit.side = LEFT;
newHit.startTime = NO_RECORD;
newHit.reported = false;
curHit = newHit;
}
}
/*
* Looks at each pin on the side to see if it is LOW (e.g., registered a hit)
* and updates the Hit data structure if so.
*/
void readPins(int* pinList, char curSide) {
for (int i = 0; i < 4; i++) {
int pinNumber = pinList[i];
int pinValue = digitalRead(pinNumber);
//Serial.println(pinValue);
if (pinValue == LOW) {
//Serial.println("high detected");
unsigned long detectionTime = micros();
if (curHit.startTime == NO_RECORD) {
curHit.startTime = detectionTime;
curHit.side = curSide;
}
// add this data to the existing hit
updateCorner(pinNumber, curSide, detectionTime);
}
}
}
/*
* Updates the current hit data structure in the correct corner, given the pin number
*/
void updateCorner(int pinNumber, char curSide, unsigned long detectionTime) {
int *sidePins;
if (curSide == RIGHT) {
sidePins = rightPins;
}
else {
sidePins = leftPins;
}
if (pinNumber == sidePins[0] && curHit.upperLeft == NO_RECORD) {
curHit.upperLeft = detectionTime - curHit.startTime;
}
else if (pinNumber == sidePins[1] && curHit.upperRight == NO_RECORD) {
curHit.upperRight = detectionTime - curHit.startTime;
}
else if (pinNumber == sidePins[2] && curHit.lowerRight == NO_RECORD) {
curHit.lowerRight = detectionTime - curHit.startTime;
}
else if (pinNumber == sidePins[3] && curHit.lowerLeft == NO_RECORD) {
curHit.lowerLeft = detectionTime - curHit.startTime;
}
}
boolean hitTimedOut() {
unsigned long time_since = micros() - curHit.startTime;
boolean timed_out = (time_since >= TIMEOUT_BETWEEN_HITS);
return (curHit.startTime == NO_RECORD || timed_out);
}
boolean isCompleteHit(struct Hit *hit) {
return hit->upperLeft != NO_RECORD && hit->upperRight != NO_RECORD && hit->lowerRight != NO_RECORD && hit->lowerLeft != NO_RECORD;
}