V1 Full
V1 Full
h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Preferences.h>
// Biến chế độ
bool isAutoMode = false;
bool isPaused = false;
preferences.end();
if (stageIdx + 1 > maxSavedStage) maxSavedStage = stageIdx + 1;
}
preferences.end();
if (stageIdx + 1 > maxSavedStage) maxSavedStage = stageIdx + 1;
}
// Joystick DC (V0)
BLYNK_WRITE(V0) {
Serial.println("V0 (Joystick DC) - Called, isAutoMode: " + String(isAutoMode));
if (!isAutoMode) {
int x = param[0].asInt();
if (x == -1) {
dir = -1; // Trái: chiều ngược
currentSpeed = constrain(inputSpeed, 0, 11); // Giới hạn tốc độ từ 0-11
joystickActive = true;
} else if (x == 1) {
dir = 1; // Phải: chiều thuận
currentSpeed = constrain(inputSpeed, 0, 11); // Giới hạn tốc độ từ 0-11
joystickActive = true;
} else {
dir = 0; // Buông: dừng
currentSpeed = 0;
joystickActive = false;
}
Serial.println("V0 (Joystick DC) - X: " + String(x) + ", Dir: " + String(dir) +
", Speed: " + String(currentSpeed) + ", InputSpeed: " +
String(inputSpeed) +
", JoystickActive: " + String(joystickActive));
} else {
Serial.println("V0 (Joystick DC) - Ignored: Auto mode is active");
}
}
// Distance DC (V7)
BLYNK_WRITE(V7) {
inputDistance = param.asFloat();
Serial.println("V7 (Distance DC) - Input Distance: " + String(inputDistance));
}
// Speed DC (V8)
BLYNK_WRITE(V8) {
inputSpeed = param.asFloat();
inputSpeed = constrain(inputSpeed, 0, 11); // Giới hạn tốc độ từ 0-11
Serial.println("V8 (Speed DC) - Input Speed: " + String(inputSpeed));
}
// Zoom (V10)
BLYNK_WRITE(V10) {
int zoomState = param.asInt();
if (zoomState == 1) {
Serial.println("V10 (Zoom) - Zoom In triggered");
} else {
Serial.println("V10 (Zoom) - Zoom Out triggered");
}
}
// Theta1 (V15)
BLYNK_WRITE(V15) {
inputTheta1 = param.asFloat();
Serial.println("V15 (Theta1 Pan-Tilt) - Input Theta1: " + String(inputTheta1));
}
// Theta2 (V16)
BLYNK_WRITE(V16) {
inputTheta2 = param.asFloat();
Serial.println("V16 (Theta2 Pan-Tilt) - Input Theta2: " + String(inputTheta2));
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("ESP32 Started");
SerialUART.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
Serial.println("UART Initialized");
loadOptionsFromPreferences();
loadPanTiltOptionsFromPreferences();
void loop() {
Blynk.run();
unsigned long now = millis();
theta1 = targetStage.theta1;
theta2 = targetStage.theta2;
lastTheta1 = theta1;
lastTheta2 = theta2;
panSpeed = targetStage.panSpeed;
tiltSpeed = targetStage.tiltSpeed;
panDir = (deltaTheta1 >= 0) ? 0 : 1;
tiltDir = (deltaTheta2 >= 0) ? 0 : 1;
currentPanTiltStageStep++;
} else {
currentPanTiltStageStep = 0;
panTiltRunning = false;
currentSpeed = 0;
String msg = "D:0,S:0\n";
SerialUART.print(msg);
Serial.println("Auto - Option " + String(currentOption) + " completed up to
saved stage S" + String(maxSavedStage));
}
}
// Xử lý dữ liệu từ STM32
while (SerialUART.available()) {
char c = SerialUART.read();
if (c == '\n') {
if (uartData.startsWith("V:")) {
float CurVel = uartData.substring(2).toFloat();
if (now - lastPrintTime_CV >= 1000) {
lastPrintTime_CV = now;
Serial.println("STM32 - CurVel: " + String(CurVel));
}
} else if (uartData.startsWith("NP:")) {
float NewCurPos = uartData.substring(3).toFloat();
if (resetPending && (now - lastResetTime < 2000)) {
if (abs(NewCurPos) < 1.0) {
Serial.println("STM32 - Confirmed reset: NewCurPos=" +
String(NewCurPos));
resetPending = false;
} else {
Serial.println("STM32 - Error: Invalid position after reset: NP:" +
String(NewCurPos) + ", forcing to 0");
NewCurPos_global = 0;
}
} else {
NewCurPos_global = NewCurPos;
}
if (now - lastPrintTime_NP >= 1000) {
lastPrintTime_NP = now;
Serial.println("STM32 - NewCurPos: " + String(NewCurPos_global));
}
} else if (uartData.startsWith("Z:")) {
String zoomCmd = uartData.substring(2);
if (zoomCmd == "IN") {
Blynk.virtualWrite(V10, 1);
Serial.println("STM32 - Zoom In triggered");
} else if (zoomCmd == "OUT") {
Blynk.virtualWrite(V10, 0);
Serial.println("STM32 - Zoom Out triggered");
}
}
uartData = "";
} else {
uartData += c;
}
}
}