Code error

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ShirlySF
 
Posts: 3
Joined: Tue May 02, 2023 6:09 am

Code error

Post by ShirlySF »

Hey there, I'm having some trouble connecting my servo controller board with my Arduino board, and I'm not sure what's causing the issue. The library I'm using for my LX-16A motor from Hiwonder isn't working right - I keep getting a read timeout error, and the write function isn't working either. I've been using this library: https://github.com/madhephaestus/lx16a-servo 3 and I'm pretty sure I've got the code right, but it's still not working. Can anyone give me some advice on how to troubleshoot this? By the way, here's a picture of how I've got the board connected.

Code: Select all

const int txPin = 1; // TX pin of the Arduino connected to the data input of the bus servo controller
const int baudRate = 9600; // baud rate of the bus servo controller

void setup() {
  Serial.begin(baudRate);
}

void loop() {
  // move servo 1 to position 1000
  int servoId = 1;
  int position = 1000;
  moveServo(servoId, position);

  delay(1000);

  // move servo 1 to position 2000
  position = 2000;
  moveServo(servoId, position);

  delay(1000);
}

void moveServo(int servoId, int position) {
  // prepare the command packet
  byte commandPacket[] = {0x55, 0x55, 0x06, 0x03, servoId, position & 0xFF, position >> 8, 0x00};

  // calculate the checksum
  byte checksum = 0;
  for (int i = 2; i < 7; i++) {
    checksum ^= commandPacket[i];
  }
  commandPacket[7] = checksum;

  // send the command packet to the servo controller
  for (int i = 0; i < sizeof(commandPacket); i++) {
    Serial.write(commandPacket[i]);
  }
}
Attachments
1.jpeg
1.jpeg (196.57 KiB) Viewed 180 times

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Code error

Post by dastels »

My first thought is that it's because Rx/Tx on the UNO is connected to the serial-USB converter on the board (and I notice you have USB connected). You could try using software serial on another pair of pins. See https://docs.arduino.cc/learn/built-in- ... are-serial.

Dave

User avatar
adafruit_support_bill
 
Posts: 88097
Joined: Sat Feb 07, 2009 10:11 am

Re: Code error

Post by adafruit_support_bill »

Check the user manual for your controller board. Rx usually means the receiving pin for the board. So that should be connected to the transmit pin (Tx) of the Uno.

So it should be Tx->Rx and Rx->Tx instead Rx->Rx and Tx->Tx as in your photo.

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Code error

Post by dastels »

Good catch, Bill. Yes, it's always Tx from one board to Rx on the other, unless for some reason it's explicitly mentioned to be otherwise (in which case someone didn't know what they were doing).

Dave

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

Re: Code error

Post by dastels »

You know, you might be better off going somewhere more relevant for the hardware you're using. There might be something in reddit.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”