I have an Arduino Mega hooked up to a Trex controller and a 12V motor. I have pins 10/12 on the mega set up as receive and transmit (hooked up to the Tx and Rx pins respectively on the Trex).
When I turn on power, the red LED on the Trex flickers momentarily and then the green LED blinks continuously very rapidly. The motor does not move.
The modified Arduino code is shown below.
Any advice would be appreciated.
Perry
#include <NewSoftSerial.h>
#define rxPin 10 // pin 10 connects to Tred TX
#define txPin 12 // pin 12 connects to Trex RX
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0X83, BYTE); //Does serial control the motors?
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0XC1, BYTE); // motor 1 reverse
speed = -speed; // make speed positive
}
else
{
mySerial.print(0XC2, BYTE); // motor 1 forward
}
mySerial.print((unsigned char)(speed & 0X1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}
void setup()
{
// initialize software serial object with baud rate of 38.4 kbps
mySerial.begin(19200);
// the Simple Motor Controller must be running for at least 1 ms
// before we try to send serial data, so we delay here for 5 ms
delay(5);
// next we need to send the Exit Safe Start command, which
// clears the safe-start violation and lets the motor run
exitSafeStart(); // clear the safe-start violation and let the motor run
}
void loop()
{
setMotorSpeed(3200); // full-speed forward for 5 seconds
delay(5000);
setMotorSpeed(-3200); // full-speed reverse for 5 seconds
delay(5000);
}
It looks like you now have a sort of weird hybrid between the program that was written for the Simple Motor Controller and something intended for the TReX. The TReX does not have an “exit safe start” command, so you should just remove that. You should also note that TReX motor speeds range from 0 to 127 and the motor commands take a single data byte for the speed, so asking for a speed of 3200 is not going to do what you want. Try using this function:
// speed should be a number from -127 to 127
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0XC1, BYTE); // motor 1 reverse
speed = -speed; // make speed positive
}
else
{
mySerial.print(0XC2, BYTE); // motor 1 forward
}
mySerial.print((unsigned char)speed & 0x7F, BYTE);
}
It sounds like there are potentially a few other problems as well. The TX and RX pins on the TReX are for RS-232 serial. You need to connect your Arduino transmit pin to SI and your Arduino receive pin to SO, which are the TTL serial pins. This is explained in section 3.b of the user’s guide. Finally, you need to make sure your TReX is in serial mode by removing the mode jumper. From your description of the LEDs, it sounds like the TReX is not in serial mode. See section 3.c of the user’s guide.
Thanks for the prompt reply. As you suspected, the jumper was in place when it should not have been. Now, the red LED does in fact come on. I have changed the connections from TX/RX to SO/SI.
The motor still doesn’t move however. The green LED does not come on either.
The code has been modified and is shown below.
Thanks,
Perry
#include <NewSoftSerial.h>
#define rxPin 10 // pin 10 connects to Trex SO
#define txPin 12 // pin 12 connects to Trex SI
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// speed should be a number from -127 to 127
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0XC1, BYTE); // motor 1 reverse
speed = -speed; // make speed positive
}
else
{
mySerial.print(0XC2, BYTE); // motor 1 forward
}
mySerial.print((unsigned char)(speed & 0X7F), BYTE);
}
void setup()
{
// initialize software serial object with baud rate of 19.2 kbps
mySerial.begin(19200);
// the Simple Motor Controller must be running for at least 1 ms
// before we try to send serial data, so we delay here for 5 ms
delay(5);
}
void loop()
{
setMotorSpeed(127); // full-speed forward for 5 seconds
delay(5000);
setMotorSpeed(-127); // full-speed reverse for 5 seconds
delay(5000);
}
Do you have a common ground between your Arduino and the TReX? Can you post a picture of your setup and tell me how you’re powering the TReX?
The solid red LED means that you are in serial mode. The green LED should flicker briefly whenever the TReX receives serial command. Because of the way your program is written, you would only see this brief flicker once every 5 seconds.
If you have access to a USB-to-serial adapter, you can try talking to the TReX using the TReX Configurator Software. Using known-good software can help you figure out if there is a problem with your system or with your Arduino code. I also suggest you try disconnecting your motor and just use the motor indicator LEDs for feedback to start with. Keeping the system as simple as possible means you don’t have as many places to look when things aren’t working.
I’m glad to hear it’s working now. Thanks for letting us know what the problem was! Does the sketch from your previous post do what you want or did you have to tweak it some after fixing the jumper wire?
void setThruster(char side, int speed) // this fxn activates motors 1 & 2 of controller 1. Speed is an int from -127 to 127
{
if (abs(speed)<20) { // ignore slight mis-calibration of neutral position
speed=0;
}
constrain(speed,-127,127); // gracefully handle odd input
if (side=='L')
{
if (speed < 0)
{
controllerOne.print(0XC1, BYTE); // motor 1 reverse
speed = -speed; // make speed positive
}
else
{
controllerOne.print(0XC2, BYTE); // motor 1 forward
}
controllerOne.print((unsigned char)(speed & 0X7F), BYTE);
} //end left thruster control
if (side=='R')
{
if (speed < 0)
{
controllerOne.print(0XC9, BYTE); // motor 2 reverse
speed = -speed; // make speed positive
}
else
{
controllerOne.print(0XCA, BYTE); // motor 2 forward
}
controllerOne.print((unsigned char)(speed & 0X7F), BYTE);
} //end right thruster control
} // end setThruster()