-
Notifications
You must be signed in to change notification settings - Fork 1
/
master-emulator.ino
247 lines (193 loc) · 6.59 KB
/
master-emulator.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
The Full-Arm-01, Full-Arm-05 and FA-07 (masters) use an optoisolated port, with a "20mA current loop" interface for the connection to a Repeater.
The master sends out every 42ms a string of 10 bytes.
Bytes:
- 1°: 0xFF = Beginning of the string
- 2°: 0x06 = The right score is 6
- 3°: 0x12 = The left score is 12
- 4°: 0x56 = The seconds are 56
- 5°: 0x02 = The minutes are 2
- 6°: 0x00 = 0b 0 0 0 0 0 0 0 0 State of the lamps: red, green, whites (w) and yellows (y)
| | | | | | | | In this example all the lamps are off
always 0 | rx y | red | lx w Remember 0=OFF, 1=ON
lx y green rx w
- 7°: 0x00 = 0b00000000 = Number of matches and priorite signals. b0+b1 = number of matches (00 = 0 .. 11 = 3). b2 = right priorite. b3 = left priorite
- 8°: 0x00 = Always different from 0xFF
- 9°: 0x00 = 0b00000000 = Red and yellow penalty cards. b0 = right red penalty card, b1 = left red penalty card, b2 = right yellow penalty card, b3 = left yellow penalty card, b4 and b5 can be 0 or 1 (useless), b6 and b7 are always 0
- 10°: Checksum, it is the module of the sum of the 9 previous bytes
Thanks for some of these precious information to https://www.reddit.com/r/Fencing/comments/cufcku/do_anyone_know_where_to_find_those_lightings_and/
*/
#include <SoftwareSerial.h> //https://docs.arduino.cc/learn/built-in-libraries/software-serial
#define msHitTime 5
#define msDoubleTime 40
#define msHitLampDelay 1500
#define msGNDLampDelay 0
#define supHit 600
#define extGND 300
#define aGND 400
#define v5Drop 1000
#define realGND 100
#define rxPin 10
#define txPin 11
#define msgLength 10
byte blankMsg[] = { 0xFF, 0x00, 0x00, 0x00, 0x00, 0b00111111, 0x00, 0x00, 0x00 };
byte outMsg[msgLength];
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
unsigned long currentTime, prevTime;
unsigned long lxATime, rxATime, lxGNDTime, rxGNDTime;
unsigned long redTime, greenTime, lYellowTime, rYellowTime;
bool red, green, lYellow, rYellow, lWhite, rWhite;
int lxAt, lxGNDt, rxAt, rxGNDt;
void setup() {
lxATime = rxATime = lxGNDTime = rxGNDTime = 0;
prevTime = currentTime = redTime = greenTime = lYellowTime = rYellowTime = millis();
red = green = lYellow = rYellow = lWhite = rWhite = false;
lxAt = lxGNDt = rxAt = rxGNDt = 0;
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(A0, INPUT); // A line lame left rosso a line
pinMode(A1, INPUT); // B line left nero b line
pinMode(A2, INPUT); // verde gnd
pinMode(A3, INPUT); // A line right
pinMode(A4, INPUT); // B line right
pinMode(A5, INPUT); //gnd right
Serial.begin(9600);
mySerial.begin(2400); // DATA-LINE Serial type: 2400-N-8-1
}
void loop() {
currentTime = millis();
if ((currentTime > (lxATime + msHitTime)) && lxATime != 0) {
if (lxAt >= msHitTime) {
lxAt = 0;
lxATime = 0;
if (lYellow == false) {
if (green == false) {
red = true;
redTime = currentTime;
} else {
if (currentTime <= (greenTime + msDoubleTime)) {
red = true;
redTime = currentTime;
}
}
}
} else {
lxAt = 0;
lxATime = 0;
}
}
if ((currentTime > (rxATime + msHitTime)) && rxATime != 0) {
if (rxAt >= msHitTime) {
rxAt = 0;
rxATime = 0;
if (rYellow == false) {
if (red == false) {
green = true;
greenTime = currentTime;
} else {
if (currentTime <= (redTime + msDoubleTime)) {
green = true;
greenTime = currentTime;
}
}
}
} else {
rxAt = 0;
rxATime = 0;
}
}
if ((red == true) && (currentTime > (redTime + msHitLampDelay))) {
red = false;
}
if ((lYellow == true) && (currentTime > (lYellowTime + msGNDLampDelay))) {
lYellow = false;
}
if ((green == true) && (currentTime > (greenTime + msHitLampDelay))) {
green = false;
}
if ((rYellow == true) && (currentTime > (rYellowTime + msGNDLampDelay))) {
rYellow = false;
}
int lxA = analogRead(A0);
int lxB = analogRead(A1);
int lxGND = analogRead(A2);
int rxA = analogRead(A3);
int rxB = analogRead(A4);
int rxGND = analogRead(A5);
// GNDs
if ((lxA < aGND && (lxGND > extGND || rxGND > extGND)) || (lxB < v5Drop && (lxGND > extGND || rxGND > extGND)) || lxB < realGND || lxA < realGND) {
lYellow = true;
lYellowTime = millis();
}
if ((rxA < aGND && (rxGND > extGND || lxGND > extGND)) || (rxB < v5Drop && (rxGND > extGND || lxGND > extGND)) || rxB < realGND || rxA < realGND) {
rYellow = true;
rYellowTime = millis();
}
// HITs
if (lxA > supHit) {
if (lxAt == 0) lxATime = millis();
lxAt++;
}
if (rxA > supHit) {
if (rxAt == 0) rxATime = millis();
rxAt++;
}
if (currentTime % 100 == 0) {
Serial.print(lxA);
Serial.print(", ");
Serial.print(lxB);
Serial.print(", ");
Serial.print(lxGND);
Serial.print(", ");
Serial.print(rxA);
Serial.print(", ");
Serial.print(rxB);
Serial.print(", ");
Serial.println(rxGND);
}
/*
DEBUG ZONE
if (Serial.available() > 0) {
String in = Serial.readString();
in.trim();
if (in == "red") red ? red = false : red = true;
if (in == "green") green ? green = false : green = true;
if (in == "lYellow") lYellow ? lYellow = false : lYellow = true;
if (in == "rYellow") rYellow ? rYellow = false : rYellow = true;
if (in == "lWhite") lWhite ? lWhite = false : lWhite = true;
if (in == "rWhite") rWhite ? rWhite = false : rWhite = true;
}
red = random(0, 2);
green = random(0, 2);
lYellow = random(0, 2);
rYellow = random(0, 2);
lWhite = random(0, 2);
rWhite = random(0, 2);
*/
if (currentTime >= (prevTime + 42)) { //The master sends out every 42ms a string of 10 bytes.
byte checksum = 0;
for (int i = 0; i < msgLength; i++) {
if (i == 9) {
checksum = checksum % 256;
outMsg[i] = checksum;
} else {
if (i == 5) {
byte lamps = 0;
if (lWhite) lamps += 0b00000001;
if (rWhite) lamps += 0b00000010;
if (red) lamps += 0b00000100;
if (green) lamps += 0b00001000;
if (rYellow) lamps += 0b00010000;
if (lYellow) lamps += 0b00100000;
outMsg[i] = lamps;
checksum += lamps;
} else {
outMsg[i] = blankMsg[i];
checksum += blankMsg[i];
}
}
}
mySerial.write(outMsg, sizeof(outMsg));
prevTime = currentTime;
}
}