-1

Im doing my thesis right now, Arduino Automatic Telescope. And im stuck at a problem cant control my servo motors from my Java program, LED light for debugging works but my motors dont move.

void loop() {
  if (Serial.available() > 0) {
    // Turn on LED when receiving data
    digitalWrite(ledPin, HIGH);
    
    // Read the incoming data
    String receivedData = Serial.readStringUntil('\n');
    int position = receivedData.toInt();
    
    // Move servos
    altitude.write(position);
    azimuth.write(position);
    delay(1000);
    
    // Return to zero
    altitude.write(0);
    azimuth.write(0);
    
    // Turn off LED when done processing
    digitalWrite(ledPin, LOW);
  }
}

Java code

public static void main(String[] args) {

       SerialPort arduinoPort = SerialPort.getCommPort("COM4");

       if(arduinoPort.openPort()){
        System.out.println("Port opened successfully");
       }else{
        System.out.println("Port couldnt be opened");
        return;
       }

       //Configure Port
       arduinoPort.setComPortParameters(9600, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
       arduinoPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 1000, 0);


       sendIntToArduino(arduinoPort,90);
       // Add delay to ensure data is sent
        try {
            Thread.sleep(2000);  // Wait 2 seconds
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

       arduinoPort.closePort();
    }

    public static void sendIntToArduino(SerialPort port, int degrees) {
        // Convert to string with newline
        String command = degrees + "\n";
        byte[] buffer = command.getBytes();
        
        port.writeBytes(buffer, buffer.length);
        System.out.println("Data was SENT: " + degrees);
    }

Manually everything works fine all cables are correctly connected. I think the problem is the data type im sending my data. If you got any advices im all ears.

4
  • When I open my serial monitor and type 90. Both motors move 90degrees and back to 0 like my arduino sketch. Commented Apr 24 at 11:05
  • After some debugging my java sends this data Port opened successfully Data was SENT: [B@448139f0 Data was : 574810 Which is correct. Seems like the line Serial.available>0 never is true :/ Commented Apr 24 at 12:05
  • 1
    I haven't done any Arduino programming in a while, but worth trying: in the Java code, add a delay between opening the port and sending the string. Commented Apr 24 at 13:33
  • Bellow I have answered my question after a week of thinking about it. Commented Apr 30 at 18:23

1 Answer 1

0

When you open the serial port, Arduino resets, so any data sent immediately can be lost. Adding a delay ensures the board is ready to receive input.

Update your Java code w/ below:

if (arduinoPort.openPort()) {
    System.out.println("Port opened successfully");

    // Add this delay to wait for Arduino reset to complete
    try {
        Thread.sleep(2000);  // Wait 2 seconds
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    sendIntToArduino(arduinoPort, 90);
} else {
    System.out.println("Port couldn't be opened");
}
Sign up to request clarification or add additional context in comments.

1 Comment

So i've come to the conclution that the fist serial read that my java program does is almost 100% WRONG every time. Even adding delay isnt fixing it but I kept them. So what I did was my arduino is SENDING me gps data every 1sec. And the MOST IMPORTANT it to have a cycle which READS AGAIN 2 or 3 times the serialWrite of the Arduino ! And the outcome is this Data received from arduino is : 42. Data received from arduino is : 42.135398,24.745300 Data received from arduino is : 42.135398,24.745300 And this is how I solved this problem. Hope if anybody has the same problem will see my solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.