I’m using an MD49 with an Arduino, but I’m having troubles getting the two to communicate. I’m using AltSoftSerial with of course pins 8 and 9 for Rx and Tx respectively, I’m completely sure that the Rx and Tx pins are connected properly (the MD49’s Rx pin to pin 9 on the Arduino). I’ve tried using different combinations of 38400 and 9600 baud rates, but I can’t figure out what it might be! (This is my first extracurricular Arduino project so I need a lot of help.)
Hi Needs-a-TARDIS,
Have you tried the example from the manufacturer? We would recommend trying that first (without the LCD) to verify that everything’s working, and then making the changes you need.
That’s actually what I did. I’ve got five different versions of that, actually. Here’s the one I’m using now.
[code]/*********************************************
- Arduino example code for MD49 *
- Using LCD03 in serial mode to display data *
-
- By James Henderson, 2012. *
*********************************************/
#include <AltSoftSerial.h>
// Values of 0 being sent over serial write have to be cast as a byte so they arent misinterpreted as NULL
// This is a bug with arduino 1.0
#define CMD (byte)0x00 // MD49 command address of 0
#define GET_VER 0x29
#define GET_ENC1 0x23u
#define GET_ENC2 0x24
#define GET_VI 0x2C
#define GET_ERROR 0x2D
#define SET_ACCEL 0x33
#define SET_SPEED1 0x31
#define SET_SPEED2 0x32
//#define LCD_RX 0x02 // RX and TX pins used for LCD0303 serial port
//#define LCD_TX 0x03
#define LCD03_HIDE_CUR 0x04
#define LCD03_HOME 0x01
#define LCD03_CLEAR 0x0C
#define LCD03_SET_CUR 0x02
AltSoftSerial DATSerial; //Rx is 8,Tx is 9 // Creates a serial port for the LCD03
byte enc1a, enc1b, enc1c, enc1d = 0;
byte enc2a, enc2b, enc2c, enc2d = 0;
byte bat_volt, mot1_cur, mot2_cur = 0;
byte ver = 0;
byte error = 0;
int i = 0;
void setup()
{
Serial.begin(38400);
delay(100);
Serial.println(“AltSOftSerial Test Begin”);
DATSerial.begin(9600);
DATSerial.println(“Hello World”);
}
void loop() {
delay(1000);
//
// Serial.println(“Setting acceleration and speeds”);
// DATSerial.write(CMD); // command byte
// DATSerial.write(SET_ACCEL);
// DATSerial.write(5); // Set accelleration to 5
// DATSerial.write(CMD);
// DATSerial.write(SET_SPEED1);
// DATSerial.write(140); // Set motor 1 speed
// DATSerial.write(CMD);
// DATSerial.write(SET_SPEED2);
// DATSerial.write(140); // Set motor 2 speed
//
// DATSerial.write(CMD);
// DATSerial.write(GET_VER); // Recieve version back
// delay(500);
// int i = 0;
// while(DATSerial.available() == 0) {
// delay(50);
// Serial.print(".");
// }
// if(DATSerial.available() > 0)
// {
ver = DATSerial.read();
Serial.print("Version: ");
Serial.println(ver);
// }
//
// DATSerial.write(CMD);
// DATSerial.write(GET_ERROR); // Recieve error byte back
// delay(50);
// if(DATSerial.available() > 0)
// {
// error = DATSerial.read();
// Serial.print(“Error: “);
// Serial.println(error);
// }
// DATSerial.write(CMD);
// DATSerial.write(GET_VI); // Recieve battery volts and both motor currents back
// delay(50);
// if(DATSerial.available() > 2)
// {
// bat_volt = DATSerial.read();
// mot1_cur = DATSerial.read();
// mot2_cur = DATSerial.read();
// }
//
// DATSerial.write(CMD);
// DATSerial.write(GET_ENC1); // Recieve encoder 1 value
// delay(50);
// if(DATSerial.available() > 3)
// {
// enc1a = DATSerial.read();
// enc1b = DATSerial.read();
// enc1c = DATSerial.read();
// enc1d = DATSerial.read();
// }
//
// DATSerial.write(CMD);
// DATSerial.write(GET_ENC2); // Recieve encoder 2 value
// delay(50);
// if(DATSerial.available() > 3)
// {
// enc2a = DATSerial.read();
// enc2b = DATSerial.read();
// enc2c = DATSerial.read();
// enc2d = DATSerial.read();
// }
//
//// DATSerial.write(LCD03_HOME); // Return cursor home
//
// Serial.println(“Rev: “); // Display data to the screen
// Serial.print(ver,DEC);
// Serial.println(” Battery V: “);
// Serial.print(bat_volt, DEC);
//// DATSerial.write(LCD03_SET_CUR); // Set position of cursor
//// DATSerial.write(21);
//
// delay(5); // Delay to allow LCD03 time to process data
//
// Serial.println(“Encoder 1:”);
// Serial.print(enc1a,HEX);
// Serial.print(enc1b,HEX);
// Serial.print(enc1c,HEX);
// Serial.print(enc1d,HEX);
// // DATSerial.write(LCD03_SET_CUR);
// // DATSerial.write(41);
//
// delay(5);
//
// Serial.println(“Encoder 2:”);
// Serial.print(enc2a,HEX);
// Serial.print(enc2b,HEX);
// Serial.print(enc2c,HEX);
// Serial.print(enc2d,HEX);
//// DATSerial.write(LCD03_SET_CUR);
//// DATSerial.write(61);
//
// delay(5);
//
// Serial.println(“Mot 1 I:”);
// Serial.print(mot1_cur,DEC);
// Serial.print(” Mot 2 I:”);
// Serial.print(mot2_cur,DEC);
// Serial.print(””);
//
// delay(5);
// Serial.println(“Error”);
// Serial.print(error);
}[/code]
I noticed that you aren’t sending the GET_VER command anymore, and that in the setup you’re sending “Hello World” to the MD49 motor controller, which is probably confusing it…
Can you try this code instead?
/*********************************************
* Arduino example code for MD49 *
* *
* By James Henderson, 2012. *
*********************************************/
#include <AltSoftSerial.h>
// Values of 0 being sent over serial write have to be cast as a byte so they arent misinterpreted as NULL
// This is a bug with arduino 1.0
#define CMD (byte)0x00 // MD49 command address of 0
#define GET_VER 0x29
AltSoftSerial DATSerial; //Rx is 8,Tx is 9 // Creates a serial port for the LCD03
byte ver = 0;
void setup()
{
Serial.begin(38400);
delay(100);
Serial.println("AltSOftSerial Test Begin");
DATSerial.begin(9600);
}
void loop() {
delay(1000);
DATSerial.write(CMD);
DATSerial.write(GET_VER); // Recieve version back
delay(500);
int i = 0;
while(DATSerial.available() == 0) {
delay(50);
Serial.print(".");
}
ver = DATSerial.read();
Serial.print("Version: ");
Serial.println(ver);
}
Thanks for the example code! The MD49 still returns an empty variable, though.
Unfortunately when it comes to specific code, we cannot do much more than see if the code already exists.
Since it seems like you never actually got the two working together, you might try starting from the ground up:
Connect the serial lines directly to Tx and Rx of the Arduino and create simple code to make the motors turn. Ensure you have the right baud rate.
The manufacturer encourages you to post on their forum here.