Using a Spektrum DX6e RC transmitter and AR620 receiver, Arduino MEGA 2560, and Dual G2 High Power 24v14 driver. I modified a Sparkfun code. When I run the code and using the toggle on the transmitter (right toggle, up/down) only M2 operates properly. I am new to coding and can’t figure out if the code is written incorrectly. I have changed out the motor driver to ensure it wasn’t a bad board or that my novice soldering wasn’t the problem but I got the same result. I verified my wires were in the correct pins and used different wires to make sure there wasn’t a short of some kind…but same result. Also, when I have the SLP pins connected, I get no motion out of either motor. So, I removed them. The only other attached wire (not in the code) is GND (on left side of motor driver) to GND on Arduino. I have attached the code I’m running. This is for my senior design project so any help will be greatly appreciated.
// controller pins
const int CH_4_PIN = 11;
// motor driver pins
const int M1SLP = 10;
const int M1DIR = 2;
const int M1PWM = 5;
const int M2SLP = 9;
const int M2DIR = 7;
const int M2PWM = 6;
// parameters
const int deadzone = 20; //anything between -20 and 20 is stop
void setup() {
//configure pins
pinMode(M1SLP, OUTPUT);
pinMode(M1DIR, OUTPUT);
pinMode(M1PWM, OUTPUT);
pinMode(M2SLP, OUTPUT);
pinMode(M2DIR, OUTPUT);
pinMode(M2PWM, OUTPUT);
//Enable motor driver
digitalWrite(M1SLP, HIGH);
digitalWrite(M2SLP, HIGH);
}
void loop() {
//read pulse width from receiver
int ch_4 = pulseIn(CH_4_PIN, HIGH, 25000);
// convert to PWM value (-255 to 255)
ch_4 = pulseToPWM(ch_4);
// drive motor
drive(ch_4, ch_4);
delay(5);
}
// postive for forward, negative for reverse
void drive(int speed_a, int speed_b) {
//limit speed between -255 and 255
speed_a = constrain(speed_a, -255, 255);
speed_b = constrain(speed_b, -255, 255);
// set direction for M1
if ( speed_a == 0) {
digitalWrite(M1SLP, LOW);
} else if (speed_a > 0){
digitalWrite(M1DIR, LOW);
} else {
digitalWrite(M1DIR, HIGH);
}
// set direction for M2
if (speed_b == 0) {
digitalWrite(M2SLP, LOW);
} else if (speed_b > 0) {
digitalWrite(M2DIR, LOW);
} else {
digitalWrite(M2DIR, HIGH);
}
// set speed
analogWrite(M1PWM, abs(speed_a));
analogWrite(M2PWM, abs(speed_b));
}
//Convert RC pulse value to motor PWM value
int pulseToPWM(int pulse) {
//If we're receiving numbers, convert them to motor PWM
if (pulse > 1000) {
pulse = map(pulse, 1000, 2000, -500, 500);
pulse = constrain(pulse, -255, 255);
} else {
pulse = 0;
}
//anythinig in deadzone should stop the motor
if (abs(pulse) <= deadzone) {
pulse = 0;
}
return pulse;
}