1void loop() {
2
3
4
5 currentMillis = millis();
6
7 if (!client.connected()) {
8 reconnect();
9 }
10
11 client.loop();
12
13
14 if (setupGame == false) {
15 client.publish(topicApp, "e");
16 setupGame = true;
17 }
18
19 if (readyTeam2 == true && readyToStart == true) {
20 readyTeam2 = false;
21 delay(3000);
22 client.publish(topicApp, "s");
23 client.publish("ptuxiakh/devices/commands", "s");
24 }
25
26
27 for (int i = 0; i < 4; i++) {
28 btn[i] = digitalRead(btnPin[i]);
29 proximity[i] = digitalRead(proximityPin[i]);
30
31 if (readyToStart == false) {
32 if (btn[i] == HIGH && checkStart[i] == false) {
33 btnMillis = millis();
34 checkStart[i] = true;
35 digitalWrite(btnLed[i] , HIGH);
36 }
37 else if (btn[i] == LOW && checkStart[i] == true) {
38 checkStart [i] = false;
39 btnMillis = 0;
40 digitalWrite(btnLed[i] , LOW);
41 }
42
43 if (millis() - btnMillis > 3000 && checkStart[i] == true) {
44 readyToStart = true;
45 Serial.println("Ready");
46
47
48 client.publish(topicApp, "r1");
49
50 for (int i = 0; i < 4; i++) {
51 digitalWrite(btnLed[i] , HIGH);
52 }
53 }
54 }
55 }
56
57
58
59 if (gameReadyToStart == true) {
60 myDFPlayer.play(StartGame);
61 for (int i = 0; i < 4; i++) {
62 digitalWrite(btnLed[i] , LOW);
63 delay(1000);
64 }
65 Serial.println("Start");
66 startCompleteGame = true;
67 delay(1000);
68 gameReadyToStart = false;
69 }
70
71
72 if (startCompleteGame == true) {
73
74 if (game == 0) {
75 LedGame(1);
76 }
77 else if (game == 1) {
78 LedGame(2);
79 }
80 else if (game == 2) {
81 LedGame(3);
82 }
83
84 else if (game == 3) {
85 reactionTime();
86 }
87 else if (game == 4) {
88 reactionTime();
89 }
90 else if (game == 5) {
91 reactionTime();
92 }
93
94
95 else if (game == 6) {
96 Pattern(3);
97 }
98 else if (game == 7) {
99 Pattern(4);
100 }
101 else if (game == 8) {
102 Pattern(5);
103 }
104
105
106
107 else if (game == 9) {
108 rfid_game();
109 }
110 else if (game == 10) {
111 rfid_game();
112 }
113 else if (game == 11) {
114 rfid_game();
115 }
116
117 else if (game == 12) {
118 Music(4, 100);
119 }
120 else if (game == 13) {
121 Music(4, 100);
122 }
123 else if (game == 14) {
124 Music(4, 100);
125 }
126
127 if (game == 15) {
128 startCompleteGame = false;
129 game = 0;
130 Serial.println("Time Scores");
131 Serial.print(" Time ||"); Serial.println("Score");
132 for (int i = 0; i < 15; i++) {
133 TotalScore = TotalScore + pointBoard[i];
134 Serial.print(scoreBoard[i]); Serial.print(timeBoard[i]); Serial.print(" || "); Serial.println(pointBoard[i]);
135 }
136 dtostrf(TotalScore, 6, 0, TotalScoreSend);
137
138 client.publish("scoreTeam1", TotalScoreSend);
139 Serial.print("Total Score = "); Serial.println(TotalScore);
140 Serial.print("Total Score Send = ");
141 Serial.println(TotalScoreSend);
142 Serial.println("End Score Board");
143
144 }
145 }
146 gameResult();
147 delay(30);
148}