-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMWired.ino
165 lines (113 loc) · 2.55 KB
/
TMWired.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
#define KLAXPIN 3
#define VIBPIN 7
#define FLASHLEDPIN 5
#define CONTACTPIN 11
#define STARTPIN 4
#define COMPLETEPIN 9
boolean contact = false;
boolean complete = false;
boolean startPosition = false;
int sens = 4; // reads
long timeout = 100;
long lastContactMillis = 0;
int contactCount = 0;
long lastCompleteMillis = 0;
int completeCount = 0;
long lastMillis = 0;
bool gameOver = true;
long flashLedDelay = 1000;
int ledState = 0;
void setup() {
pinMode(KLAXPIN, OUTPUT);
pinMode(FLASHLEDPIN, OUTPUT);
pinMode(VIBPIN, OUTPUT);
pinMode(STARTPIN, INPUT);
pinMode(COMPLETEPIN, INPUT);
pinMode(CONTACTPIN, INPUT);
digitalWrite(CONTACTPIN, HIGH);
digitalWrite(STARTPIN, HIGH);
digitalWrite(COMPLETEPIN, HIGH);
Serial.begin(9600);
}
void loop() {
if (!gameOver) {
boolean contactRead = digitalRead(CONTACTPIN);
if (!contactRead) {
contactCount++;
lastContactMillis = millis();
if (contactCount > sens) {
contact = true;
}
else if (millis() - lastContactMillis > timeout) {
contactCount = 0;
}
}
boolean completeRead = digitalRead(COMPLETEPIN);
if (!completeRead) {
completeCount++;
lastCompleteMillis = millis();
if (completeCount > sens) {
complete = true;
}
else if (millis() - lastCompleteMillis > timeout) {
completeCount = 0;
}
}
if (contact) {
contactHappened();
}
if (complete) {
gameComplete();
}
flashLed();
}
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case '0':
resetGame();
break;
case '1':
resetGame();
break;
}
}
delay(2);
}
void contactHappened() {
Serial.println("FAIL");
digitalWrite(KLAXPIN, HIGH);
digitalWrite(VIBPIN, HIGH);
delay(1000); // wait for a second
digitalWrite(KLAXPIN, LOW);
digitalWrite(VIBPIN, LOW);
gameOver = true;
}
void flashLed() {
if (!gameOver) {
if ((millis() - flashLedDelay) > lastMillis) {
ledState = !ledState;
lastMillis = millis();
}
digitalWrite(FLASHLEDPIN, ledState);
}
else {
digitalWrite(FLASHLEDPIN, HIGH);
}
}
void gameComplete() {
Serial.println("COMPLETE");
gameOver = true;
}
void resetGame() {
startPosition = digitalRead(STARTPIN);
if (!startPosition) {
contact = true;
complete = true;
gameOver = false;
Serial.println("READY");
}
else {
Serial.println("NOTREADY");
}
}