Is it the Code, the Hardware or my Ignorance?

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 stop

void 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.

The code looks reasonable to me. You should set the ch_1 and ch_2 pins to INPUT but that's the default.

I would print to Serial the values of 'x' and 'y' just after the calls to pulseIn() to make sure you are getting the expected values between 1000 and 2000.

// 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

void setup()
{
  Serial.begin(115200);
  delay(200);

  pinMode(ch_1, INPUT);
  pinMode(ch_2, INPUT);
}

void loop()
{
  // Read pulse width from receiver
  int x = pulseIn(ch_1, HIGH, 30000);
  int y = pulseIn(ch_2, HIGH, 30000);

  Serial.print(x);
  Serial.print(' ');
  Serial.println(y);

  delay(2000);
}

If you are NOT getting a value of 1000 to 2000 for both channels every time then it's time to worry about hardware.

John,
Thank you, I appreciate the input and mini-lesson.

Two Questions:
Would the receiver's pulse keep the Right Turn, Left Turn, and Reverse from occurring but still give me Forward and Left-turn?

The code snippets you sent, should they go in any particular section of my current Sketch?

Sorry, I'm a total noob, and still learning.

Thank you again.

Yes. If the pulse lengths are not what you expect then anything can happen.

Those aren't snippets. It's an entire sketch. Run it on your Arduino, open Tools->Serial Monitor, and set the data rate to 115200. Look at the values displayed. With the stick centered, both numbers should be near 1500. The first number should vary from about 1000 (full left) to 2000 (full right) when you move the stick left and right. The second number should vary from about 1000 (full back) to 2000 (full forward) when you move the stick forward and back. Are they working as they should?

John,
Thank you again, Sir.
I will run that code ASAP.
I put W.A.L.D.O. together last night and I need the glues and paints to dry before I can handle the robot.
Also, I need to start packing for our move this weekend, before the wife divorces me; NC here we come.

Thank you again, I'll post back as soon as I can.

Okay, so John,
I ran the code you sent me, as written, and this is what I got

0 0
¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿

I'm not sure if this is the same but, I did check the PWM between channel 1 & pin 10 and channel 2 & pin 11 and I got similar numbers to the ones you mentioned on the Serial Monitor, or close to those numbers, but I'm thinking that's not the same thing.

Still, when I tried to turn Right (ch_1 = 10) or go in Reverse (ch_2 = 11) on the actual robot, using the right gimble, I got nothing; only Forward and Left (or Spin Left, since both tracks are spinning0 are working.

Thank you again, John

PS
While I have your attention, and I'm not sure if this question belongs here so if it doesn't just let me know where I should post it.
Due to the nature of the robot's design, every time I need to access the USB port on the Arduino board I have to remove the chassis.
I would like to permanently attach a short 3" USB cord to the Arduino's USB port to give me easier access to it and avoid having to remove the chassis every-single-time.
So, can I run the robot with a USB cord still attached (not connected to the computer) while powered by another external power supply?

This is probably where the issue is.
I recommend making an exact copy of the example. Then if you want to make changes, only make one tiny change at a time, and test. If you've made a mistake it must be the last thing that you did.

Start over, you got this.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.