RC controlled "H-bridge DC MOSFET IRF3205" with a Traxxass 2215 & UNO

I have been working on Making our Power wheel mustang RC controlled. I got my throttle code working forward and reverse with a pwm input from the remote trigger but once the input exceeds 2000 the motor slows down instead of hitting max speed. If I pull the trigger about 90% it works perfectly. I would appreciate any suggestions.

/*
RC controlled "Dual Motor Driver Module board H-bridge DC MOSFET IRF3205" with a Traxxas 2215 receiver
I'm using this for a power wheels mod with motor 1 controling the speed and motor 2 controlling the steering.
ch3 is extra for onboard cold cathode (PC lights) or turbo 6v boost.
This is a work in progress. Feel free to use and correct as needed. 
*/


//traxxas 2215 inputs
int ch1; // steering 1100-2000
int ch2; // throttle 1400-2100
int ch3; //switch 1500-2000

//extra variables
int lights = 8;

//motor1
int ENA = 9;  //PWM analogWrite (ENA, 0); //STOP
int IN1 = 12; //HIGH FORWARD___LOW REVERSE

//motor2
int ENB = 11;  //PWM analogWrite (ENA, 0); //STOP
int IN3 = 10;  //HIGH FORWARD___LOW REVERSE

void setup() {

  pinMode(5, INPUT); //ch1
  pinMode(6, INPUT); //ch2
  pinMode(7, INPUT); //ch3
  //motor1
  pinMode (ENA, OUTPUT);//PWM analogWrite (ENA, 0); //STOP
  pinMode (IN1, OUTPUT);//HIGH FORWARD___LOW REVERSE
  //motor2
  pinMode (ENB, OUTPUT);//PWM analogWrite (ENA, 0); //STOP
  pinMode (IN3, OUTPUT);//HIGH FORWARD___LOW REVERSE

  Serial.begin(9600); // Pour a bowl of Serial

}

void loop() {

  ch1 = pulseIn(5, HIGH, 25000); // Read the pulse width of 
  ch2 = pulseIn(6, HIGH, 25000); // each channel
  ch3 = pulseIn(7, HIGH, 25000);
 
 //ch1 
  Serial.print("Channel 1 steering:"); // Print the value of 
  Serial.println(ch1);        // each channel








//ch2
  Serial.print("Channel 2 throttle:");
  Serial.println(ch2);
analogRead (ch2);
analogWrite (ENA,ch2);


if (ch2 > 1500) {
    digitalWrite(IN1, HIGH); //trigger go
  } 
  else {
   
    analogWrite (ENA,0); //set speed 0
    if (ch2 < 1300) {
    digitalWrite(IN1, LOW); //forward trigger reverse
    analogWrite(ENA,ch2); //reverse speed from trigger
  } 
  else {
   
    analogWrite (ENA,0); //set speed 0
  } 







//ch3
  Serial.print("Channel 3 switch:");
  Serial.println(ch3);
  if (ch3 > 1800) {
    digitalWrite(lights, HIGH);
  } 
  else {
    digitalWrite(lights,LOW); 
  }

  delay(1500); // I put this here just to make the terminal 
              // window happier
}}

traxxas_basic.ino (2.04 KB)

analogRead (ch2);

You are using a number from roughly 1000 to 2000 as a pin number?!?

analogWrite (ENA,ch2);

You are using a number from roughly 1000 to 2000 for the PWM range 0 to 255?!?

  pinMode (IN1, OUTPUT);//HIGH FORWARD___LOW REVERSE
  pinMode (IN3, OUTPUT);//HIGH FORWARD___LOW REVERSE

You have OUTPUTs named IN1 and IN3? That's confusing.

int ENA = 9; //PWM analogWrite (ENA, 0); //STOP int IN1 = 12; //HIGH FORWARD___LOW REVERSE ENA and in1 are the markings on the dual h bridge. They are pin 9 & 12.

I suspect part of my problem is a flakey remote. It seems to be jumping around values of ch2 randomly. I appreciate the input.

.
int ch2; // throttle 1400-2100
.
ch2 = pulseIn(6, HIGH, 25000); // each channel
.
.
//ch2
  Serial.print("Channel 2 throttle:");
  Serial.println(ch2);
analogRead (ch2);
analogWrite (ENA,ch2);
.
.

pulseIn() returns the microseconds of the pulse being HIGH. That will be in the range of hundreds to thousands. You normaly need a unsigned long to store that. So storing on an INT (signed as well) will chop some info, and because it is signed will behave very strangely.

analogRead() returns an integer from 0-1023 for the PIN inside the parenthesis. So you are asking your code to read pin "ch2" which is an undefined value caused from casting an unsigned long to a signed int. You store the return value to nowhere. Why are you using analogRead() anyway? whats the point?

Then you just write that undefined value with an analogwrite to ENA pin. AnalogWrite needs to write a byte in the renge of 0-255. You are giving it an undefined value....

So your best bet will be:

.
.
unsigned long ch2; // throttle 1400-2100

.
.
.
ch2 = pulseIn(6, HIGH, 25000); // each channel
.
.
//ch2
  Serial.print("Channel 2 throttle:");
  Serial.println(ch2);
// analogRead (ch2); /* Not Needed */
int val = map (ch2, min_value_of_ch2, Max_val_of_ch2, 0, 255);
analogWrite (ENA,val);
.
.
.

You may still have problems with map, because it expects longs and not unsigned longs, so you might want to write your own map function, which I find a better idea..

Good luck with your project. I have done the same with a Barby wrangler Jeep for my daughter, only I have build everything from ground up, including transmitter, receiver, ESC....

Hi, To help us.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Also you need to look here about examples, which I think you really need to look at.

https://www.arduino.cc/en/Tutorial/BuiltInExamples As it explains how to look at basic examples using the various commands.

Particularly

https://www.arduino.cc/en/Reference/AnalogWrite

As it explains the basic problem with your sketch pointed out in post#1.

Tom.... :)