TKK_E32211980/main.ino

203 lines
4.8 KiB
C++

#include <WiFi.h>
#include <HTTPClient.h>
// koneksi jaringan
const char* ssid = "Zidanee";
const char* password = "zidane123";
// Server endpoints
const char* statusUrl = "http://192.168.100.42/shuttle_run/games/status.php";
const char* waktuUrl = "http://192.168.100.42/shuttle_run/games/waktu.php";
const char* tambahUrl = "http://192.168.100.42/shuttle_run/games/tambah.php";
const char* endUrl = "http://192.168.100.42/shuttle_run/games/end.php";
const char* targetUrl = "http://192.168.100.42/shuttle_run/games/lampu_target.php";
// LED and button pins
const int ledPins[4] = {33 , 32, 2, 4};
const int buttonPins[4] = {12, 14, 18, 19};
// Variabel untuk menyimpan led aktif saat ini
int activeLED = -1;
unsigned long oldTime=0;
unsigned long nowTime=0;
bool ledOn = false;
int waktu = 0;
int target = 0;
int jumlah = 0;
void setup() {
Serial.begin(115200);
//Set up LED pins sebagai output
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Make sure all LEDs are off initially
}
// Set up button pins sebagai input
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Connect ke Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
nowTime = millis();
bool statusGame = checkStatus();
if (nowTime - oldTime >= 1000) {
oldTime = nowTime;
if (statusGame == 1) {
Serial.println("Status is 1, starting the program");
if (!ledOn) {
checkWaktu();
checkTarget();
randomizeLED();
ledOn = true;
}
if(waktu <= 0) {
endProgram();
ledOn = false;
}
Serial.println("waktu tersisa : " + String(waktu));
waktu -= 1;
}
}
if (statusGame == 1) {
if(target == jumlah && target != 0) {
endProgram();
ledOn = false;
}
handleButtons();
}
}
int checkTarget() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(targetUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("target: " + response);
target = response.toInt();
return response.toInt();
} else {
Serial.println("Error on HTTP request");
return 0;
}
http.end();
} else {
Serial.println("WiFi not connected");
return 0;
}
}
int checkWaktu() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(waktuUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("waktu: " + response);
waktu = response.toInt();
return response.toInt();
} else {
Serial.println("Error on HTTP request");
return 0;
}
http.end();
} else {
Serial.println("WiFi not connected");
return 0;
}
}
int checkStatus() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(statusUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
// Serial.println("Status response: " + response);
return response.toInt();
} else {
Serial.println("Error on HTTP request");
return 0;
}
http.end();
} else {
Serial.println("WiFi not connected");
return 0;
}
}
void randomizeLED() {
if (activeLED != -1) {
digitalWrite(ledPins[activeLED], LOW); // mematikan led sebelumnya
}
activeLED = random(0, 4); // memilih led acak
digitalWrite(ledPins[activeLED], HIGH); // menghidupkan led yang terpilih
Serial.println("LED " + String(activeLED) + " is on");
}
void handleButtons() {
for (int i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
if (i == activeLED) {
sendButtonPress();
randomizeLED(); // led hidup secara acak setelah menekan tombol
}
}
}
}
void sendButtonPress() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(tambahUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Button press recorded: " + response);
jumlah += 1;
} else {
Serial.println("Error on sending GET request");
}
http.end();
} else {
Serial.println("WiFi not connected");
}
}
void endProgram() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(endUrl);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Program selesai");
} else {
Serial.println("Error on sending GET request");
}
http.end();
} else {
Serial.println("WiFi not connected");
}
}