Я сейчас делаю диссертация, Arduino Automatic Telecope. И я застрял на проблеме, не могу контролировать мои сервоприводы из моей программы Java, светодиодный свет для отладки работы, но мои двигатели не движутся. < /P>
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);
}
}
< /code>
java code < /p>
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);
}
< /code>
Вручную все работает нормально. Все кабели правильно подключены. Я думаю, что проблема в том, что тип данных, который я отправляю своими данными. Если у вас есть какие -либо советы, я все уши.
Подробнее здесь: https://stackoverflow.com/questions/795 ... ervomotors
Серийная компона Java to arduino с сервомоторами ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение