I’m working on a DIY Star Tracker project and have run into a problem. After uploading my sketch to the Arduino via the IDE, the Serial Monitor always shows:
Current AZ : 00.00
Current EL : 90.00
Current AZ : 00.00
Current EL : 90.00
This output never changes—even when I send a new command in the Serial Monitor. For example, if I type AZ:080EL:075
, the monitor still displays:
Current AZ : 00.00
Current EL : 90.00
Current AZ : 00.00
Current EL : 90.00
I’m fairly new to Arduino—could anyone help me figure out what’s going wrong?
Here’s the code I’m using:
#include <Servo.h>
#include <Wire.h>
#include <MPU9250_asukiaaa.h>
Servo servoAzimuth;
Servo servoElevation;
MPU9250_asukiaaa mySensor;
String inputData = "";
float targetAz = 45.0;
float targetEl = 45.0;
float currentAz = 0.0;
float currentEl = 0.0;
const float errorTolerance = 1.0; // Toleransi error dalam derajat
void setup() {
Serial.begin(115200); // Debugging ke PC
Serial1.begin(115200); // Komunikasi dengan ESP8266
Wire.begin(); // I2C Start
// Init MPU9250
mySensor.beginAccel();
mySensor.beginGyro();
mySensor.beginMag();
servoAzimuth.attach(10);
servoElevation.attach(11);
// Kalibrasi awal posisi servo
servoAzimuth.write(90);
servoElevation.write(90);
delay(1000);
Serial.println("Star Tracker Ready");
}
void loop() {
// 1. Baca data dari ESP8266 via Serial1
while (Serial1.available()) {
char c = Serial1.read();
if (c == '\n') {
parseTarget(inputData);
inputData = "";
} else {
inputData += c;
}
}
// 2. Baca orientasi aktual dari sensor
readOrientation();
// 3. Gerakkan servo menuju target
moveToTarget();
delay(50); // Refresh rate lebih cepat
}
void parseTarget(String data) {
data.trim();
// Format baru: <AZ>,<EL>
if (data.startsWith("<") && data.endsWith(">")) {
String cleanData = data.substring(1, data.length()-1);
int commaIndex = cleanData.indexOf(',');
if (commaIndex != -1) {
targetAz = cleanData.substring(0, commaIndex).toFloat();
targetEl = cleanData.substring(commaIndex+1).toFloat();
// Stellarium: Altitude (0-90) -> Servo: (0-180)
targetEl = 90 - targetEl; // Konversi sudut
Serial.print("Target AZ: ");
Serial.print(targetAz);
Serial.print(" | Target EL: ");
Serial.println(targetEl);
}
}
}
void readOrientation() {
mySensor.accelUpdate();
mySensor.magUpdate();
// Hitung azimuth dari magnetometer
float mx = mySensor.magX();
float my = mySensor.magY();
currentAz = atan2(my, mx) * 180 / PI;
if (currentAz < 0) currentAz += 360;
// Hitung elevasi dari accelerometer (Z-axis)
float az = mySensor.accelZ();
currentEl = map(az * 100, -100, 100, 0, 180); // Konversi sederhana
Serial.print("Current AZ: ");
Serial.print(currentAz);
Serial.print(" | Current EL: ");
Serial.println(currentEl);
}
void moveToTarget() {
float errorAz = targetAz - currentAz;
float errorEl = targetEl - currentEl;
// Koreksi error azimuth (lintas 360 derajat)
if (errorAz > 180) errorAz -= 360;
else if (errorAz < -180) errorAz += 360;
// Gerakkan azimuth jika error signifikan
if (abs(errorAz) > errorTolerance) {
int newPos = servoAzimuth.read() + (errorAz > 0 ? 1 : -1);
newPos = constrain(newPos, 0, 180);
servoAzimuth.write(newPos);
}
// Gerakkan elevasi jika error signifikan
if (abs(errorEl) > errorTolerance) {
int newPos = servoElevation.read() + (errorEl > 0 ? 1 : -1);
newPos = constrain(newPos, 0, 180);
servoElevation.write(newPos);
}
}