Hey Folks,
I got this Sketch off a YouTube tutorial from SparkFun, it worked in the video but it's not working for me.
I made some changes to go with my Arduino and Motor Driver PIN set-up and I added the "Motors Initial State = Off" section and commented a couple of things since I'm learning how it all works.
I'm using the L298N motor driver, Arduino Uno r3, and the RC Element6 Transmitter & Receiver from goBILDA dot com
I have the RC set to NO CH MIX, but I also tried both A & B Mixing but I want to use ONLY the right Gimble.
As is, all I'm getting is Forward & Spin Left.
/**
- Two Channel Receiver
- Author: Shawn Hymel (SparkFun Electronics)
- Date: Aug 24, 2017
- Connect a TB6612FNG and RC (PWM) receiver to the Arduino.
- Mixes two channels for arcade drive.
*/// receiver pins and arduino pins
const int ch_1 = 10; // receiver pin 1 (L&R) arduino pin 10
const int ch_2 = 11; // receiver pin 2 (F&B) arduino pin 11// Motor driver pins connection to arduino pins
// motor A
const int enA = 5; // PWM signal
const int in1 = 2;
const int in2 = 4;// motor B
const int enB = 6; // PWM signal
const int in3 = 7;
const int in4 = 8;// Parameters
const int deadzone = 20; // Anything between -20 and 20 is stopvoid setup() {
// Configure pins
pinMode(enA, OUTPUT); // PWM signal
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT); // PWM signal
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);// Motors Initial State = Off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}void loop() {
// Read pulse width from receiver
int x = pulseIn(ch_1, HIGH, 30000);
int y = pulseIn(ch_2, HIGH, 30000);// Convert to PWM value (-150 to 150)
x = pulseToPWM(x);
y = pulseToPWM(y);// Mix for arcade drive
int left = y + x;
int right = y - x;// Drive motor
drive(left, right);
delay(5);
}// Positive 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 motor A
if ( speed_a == 0 ) {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
} else if ( speed_a > 0 ) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
} else {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}// Set direction for motor B
if ( speed_b == 0 ) {
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
} else if ( speed_b > 0 ) {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
} else {
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}// Set speed
analogWrite(enA, abs(speed_a));
analogWrite(enB, 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;
}// Anything in deadzone should stop the motor
if ( abs(pulse) <= deadzone ) {
pulse = 0;
}return pulse;
}
Any help, advice, would be helpful.
Thank you.