-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
245 lines (188 loc) · 6.11 KB
/
main.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
#include <WiFi.h>
#include <PubSubClient.h>
#include <stdio.h>
#include <stdlib.h>
// PIN setup variables
// SNAKE_POS
const int PINO_SNAKE6 = 36;
const int PINO_SNAKE5 = 39;
const int PINO_SNAKE4 = 34;
const int PINO_SNAKE3 = 35;
const int PINO_SNAKE2 = 32;
const int PINO_SNAKE1 = 33;
// APPLE_POS
const int PINO_APPLE1 = 23;
const int PINO_APPLE2 = 22;
const int PINO_APPLE3 = 5;
const int PINO_APPLE4 = 21;
const int PINO_APPLE5 = 19;
const int PINO_APPLE6 = 18;
// STATE
const int PINO_STATE1 = 27;
const int PINO_STATE2 = 13;
const int PINO_STATE3 = 16;
const int PINO_STATE4 = 4;
const int PINO_STATE5 = 15;
// VELOCITY
const int PINO_VEL = 14;
// MODE
const int PINO_MODE = 12;
// DIFF
const int PINO_DIFF = 2;
// COMEU MACA
const int PINO_COMEU_MACA = 25; // FPGA signal
// BUZZER
const int PINO_BUZZER = 26; // Buzzer pin
const int PWM_CHANNEL = 0; // PWM Channel for buzzer
// Network setup variables
const char* ssid = "WIFI-NAME";
const char* password = "WIFI-PASSWORD";
const char* mqtt_server = "BROKER-IP";
const int connection_port = 0; // Insert the desired port here (usually 1883 works on a local broker)
//create wifi client
WiFiClient espClient;
//create PubSubClient
PubSubClient client(espClient);
#define PWM_FREQ 1000
#define PWM_RES 8
int comeu_maca = 0;
String snake_pos_string = "";
String apple_pos_string = "";
String state_string = "";
String new_snake_pos_string = "";
String new_apple_pos_string = "";
String new_state_string = "";
void setup() {
// PIN/PWM setup
Serial.begin(115200); // Baud Rate
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RES); // Setups PWM channel
ledcAttachPin(PINO_BUZZER, PWM_CHANNEL);
pinMode(PINO_SNAKE6, INPUT);
pinMode(PINO_SNAKE5, INPUT);
pinMode(PINO_SNAKE4, INPUT);
pinMode(PINO_SNAKE3, INPUT);
pinMode(PINO_SNAKE2, INPUT);
pinMode(PINO_SNAKE1, INPUT);
pinMode(PINO_APPLE1, INPUT);
pinMode(PINO_APPLE2, INPUT);
pinMode(PINO_APPLE3, INPUT);
pinMode(PINO_APPLE4, INPUT);
pinMode(PINO_APPLE5, INPUT);
pinMode(PINO_APPLE6, INPUT);
pinMode(PINO_STATE1, INPUT);
pinMode(PINO_STATE2, INPUT);
pinMode(PINO_STATE3, INPUT);
pinMode(PINO_STATE4, INPUT);
pinMode(PINO_STATE5, INPUT);
pinMode(PINO_VEL, INPUT);
pinMode(PINO_MODE, INPUT);
pinMode(PINO_DIFF, INPUT);
pinMode(PINO_COMEU_MACA, INPUT);
// Network setup
setup_wifi();
client.setServer(mqtt_server, connection_port);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//connect to WiFi network
WiFi.begin(ssid, password);
//test to see, if connected
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Trying to connect to wifi \n");
Serial.println(ssid);
Serial.println(password);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
//print the ip address of the esp32
Serial.println(WiFi.localIP());
}
//function to reconnect if disconnected from the server
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
//random client ID, you can put whatever you want.
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
//if not connected, print the error code
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
int snake_pos[6] = {digitalRead(PINO_SNAKE6),
digitalRead(PINO_SNAKE5),
digitalRead(PINO_SNAKE4),
digitalRead(PINO_SNAKE3),
digitalRead(PINO_SNAKE2),
digitalRead(PINO_SNAKE1)};
int apple_pos[6] = {digitalRead(PINO_APPLE1),
digitalRead(PINO_APPLE2),
digitalRead(PINO_APPLE3),
digitalRead(PINO_APPLE4),
digitalRead(PINO_APPLE5),
digitalRead(PINO_APPLE6)};
int state[5] = {digitalRead(PINO_STATE1),
digitalRead(PINO_STATE2),
digitalRead(PINO_STATE3),
digitalRead(PINO_STATE4),
digitalRead(PINO_STATE5)};
new_snake_pos_string = "";
new_apple_pos_string = "";
new_state_string = "";
for (int i = 0; i < 6; i++) {
new_snake_pos_string += String(snake_pos[i]);
new_apple_pos_string += String(apple_pos[i]);
if (i < 5) {
new_state_string += String(state[i]);
}
}
int new_comeu_maca = digitalRead(PINO_COMEU_MACA);
int vel_select = digitalRead(PINO_VEL);
int mode_select = digitalRead(PINO_MODE);
int diff_select = digitalRead(PINO_DIFF);
int melody[] = {
440, 100, 850
};
int tempo[] = {
50, 50, 50
};
if (!client.connected()) {
// Reconnect to MQTT broker if not connected
reconnect();
}
client.loop();
Serial.println("comeu_maca: ");
Serial.println(new_comeu_maca);
snake_pos_string = new_snake_pos_string;
apple_pos_string = new_apple_pos_string;
state_string = new_state_string;
client.publish("snake_game/snake_pos", snake_pos_string.c_str());
client.publish("snake_game/apple_pos", apple_pos_string.c_str());
client.publish("snake_game/state", state_string.c_str());
client.publish("snake_game/vel_selector", String(vel_select).c_str());
client.publish("snake_game/mode_selector", String(mode_select).c_str());
client.publish("snake_game/diff_selector", String(diff_select).c_str());
if (new_comeu_maca == 1) {
comeu_maca = 1;
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
ledcWrite(PWM_CHANNEL, melody[i]);
delay(tempo[i] * 1.30); // Pequeno delay para espaçar as notas
ledcWrite(PWM_CHANNEL, 0); // Desliga o buzzer ao final de cada nota
if (i != sizeof(melody) / sizeof(melody[0]) - 1)
delay(25); // Pequeno delay para evitar cliques entre as notas
}
}
else {
comeu_maca = 0;
}
}