DIY Star Tracker using Arduino Mega 2560 and ESP8266

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);
  }
}

Please post the wiring, the schematics.

But your code does not read from the serial monitor. That would explain why it does not work.
It does seem to read something from the ESP8266

1 Like

Because your profile is hidden, we don't know if you are a noob or not. I will assume you are given that you are not following the guidance re posting code.
Let me save you reading the pinned post re 'How to get the most from the forum'
You did post your code in code tags, that is good. Now post the serial log also in code tags.
As stated in post 2, post the hand drawn wiring diagram.
p.s. I am an astrophotographer and astronomer so I have some idea what you are doing. I used to have an arduino based tracker but it wasn't that good so I scrapped it and purchased a NOMAD MSM Star Tracker.

Hi, @essasalas37
Is this the same project?

Tom.... :smiley: :+1: :coffee: :australia:

Is that the esp8266 code? What is the Mega 2560 used for. A friend built one for me as a gift and he only used an Arduino NANO.
Why do you need two processors?