[UNSOLVABLE?] Speed up PWM Ard. LEONARDO code (has INPUT and OUTPUT)

EDIT: Tagging as “UNSOLVABLE?” due to the simple thing and fact that I can’t make it go faster or do anything in general to change the way this is going to work (see answer below with same phrase)

Hi there; first forum post in Arduino.cc
I’d like to get some help and/or advice in how to speed up the code, but first, I want to explain the project (pictures included); First of all, there’s an Arduino LEONARDO as controller, which has a hitec HFD-07RA (that is basically a 7-channel RC Plane Receiver), and finally an L298N with 2 brushless
motors that control a car’s steering and movement (rear motor being movement, front being steering).

The problem with my Arduino code is that due to the fact that it uses analogWrite, if and switch, it ends up being quite slow and unresponsive. As aforementioned, I’d thank any tips and tricks to know in order to make the code run faster and be more responsive.

That apart, I only want to say that the wiring is the following;

[ARDUINO OUTPUTS]
→ PWM pins 10 AND 11→ L298N [IN3 AND EN4, respectively](for the front motor)
→ PWM pins 6 AND 5→ L298N [IN1 AND IN2, respectively](for the rear motor)
→ 5V and GND to FUTABA receiver
→ GROUNDED with the two 3,6V cell batteries, so GROUNDS are common

[ARDUINO INPUTS]
→ ANALOG pin A0→ HITEC channel 3 (Throttle)
→ ANALOG pin A2→ HITEC channel 1 (Aileron)
→ ANALOG pin A3→ HITEC channel 2 (Elevator)
→ 9V battery through 12DCV input

Code below!

//H U G E and enourmous kudos to https://www.arduino.cc/en/Tutorial/JoystickMouseControl

//AXIS  BEGIN
int Throttle;
int Ailerons;
int Elevator; // Down to up: A E T R 5 ? ? P (Ailerons, Elevator, Throttle, Rudder, Switch (ch. 5), unused, unused, POWER)
//AXIS  END

//DIR ( -1 | 0 | 1 )[ BACKWARDS | NEUTRAL | FORWARDS ]
int Dir = 1;
//DIR  END

//JOYSTICK  BEGIN
int range = 510;               // output range of X or Y movement
int threshold = range / 18;    // resting threshold
int center = range / 2;       // resting position value
//JOYSTICK  END

void setup() {

  //SERIAL  BEGIN
  /*
  Serial.begin(19200);
  
  while (!Serial){
    ;
  }
  */
  //SERIAL  END
  
  pinMode(A0, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

  pinMode(11, OUTPUT); // STEERING  IN1
  pinMode(10, OUTPUT); // STEERING  IN2

  pinMode(6, OUTPUT); // RWD  IN1
  pinMode(5, OUTPUT); // RWD  IN2

}

int readAxis(int reading, int ChannelMinimum, int ChannelMaximum) {
  reading = map(reading, ChannelMinimum, ChannelMaximum, 0, range);
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  }

  return distance;
}

int ReadThatAnalog(int PinToRead) {
  int R1 = pulseIn(PinToRead, HIGH);
  int R2 = pulseIn(PinToRead, HIGH);
  int R3 = pulseIn(PinToRead, HIGH);
  int result = (R1 + R2 + R3) / 3;
  return result;
}

void loop() {
  Throttle = ReadThatAnalog(A0);
  Throttle = readAxis(Throttle, 1123, 1860);
  Throttle = map(Throttle, -253, 255, 0, 255);
  
  Ailerons = ReadThatAnalog(A2);
  Elevator = ReadThatAnalog(A3);

  Ailerons = readAxis(Ailerons, 1139, 1862);
  Elevator = readAxis(Elevator, 1160, 1857);

  if ((Elevator > 180 && Elevator < 255) && (Dir != 1)) {
    Dir = 1;
  } else if ((Elevator < -180 && Elevator > -255) && (Dir != -1)) {
    Dir = -1;
  }

  if ((Ailerons > 0) && (Ailerons < 255)) {
    analogWrite(10, abs(Ailerons));
    analogWrite(11, LOW);
  } else if ((Ailerons < 0) && (Ailerons > -255)) {
    analogWrite(10, LOW);
    analogWrite(11, abs(Ailerons));
  } else if ((Ailerons < 10) && (Ailerons > -10)) {
    analogWrite(10, LOW);
    analogWrite(11, LOW);
  }

  // "OLD" THROTTLE  BEGIN
  /*
  if ((Throttle > 35 && Throttle < 255)) {
    switch (Dir) {
      case 1:
        analogWrite(5, LOW);
        analogWrite(6, abs(Throttle-20));
        break;

      case -1:
        analogWrite(5, abs(Throttle-20));
        analogWrite(6, LOW);
        break;

      default:
        analogWrite(5, Throttle-20);
        analogWrite(6, LOW);
        break;
    }
  } else if (Throttle < 35) {
    analogWrite(5, LOW);
    analogWrite(6, LOW);
  }
  */
  // "OLD" THROTTLE  END

  // "NEW" THROTTLE  BEGIN
  if (Throttle > 10) {
    if (Dir = 1)
    {
      analogWrite(5, LOW);
      analogWrite(6, abs(Throttle-20));
    } else {
      analogWrite(5, abs(Throttle-20));
      analogWrite(6, LOW);
    }
  }
  // "NEW" THROTTLE  END

}

EDIT:After reading the code for a hundreth time, I just realised that I use a triple reading with delay of the analog signal, that may be one cause… Besides that, any other help?
EDIT2:Removed useless delay()s and other useless commented code

Sorry if anything is confusing or looks bad; I’m only a spanish fourteen year old and I barely ever post in forums. (Yes, I have parental permission, and yes, I did everything under the (tiny) help and supervision of an adult).

Anyways, thanks beforehand for everything!

MPlayer_014.jpg

CarFlash5.ino (3.13 KB)

The problem with my Arduino code is that due to the fact that it uses analogWrite,

No, the problem is that it uses pulseIn(). That is a blocking function. And the pointless delay()s.

It is NOT necessary to read and average RC signals. There is no contact bounce or ADC to worry about.

PaulS: No, the problem is that it uses pulseIn().

But then how can I read and interpret (and or read) the PWM signal from the receiver? I tried to use analogRead() but it's just "garbage" and doesn't properly work.

PaulS: And the pointless delay()s.

Well, yeah, knew that would be needed to be erased, thanks for pointing out! :)

But then how can I read and interpret (and or read) the PWM signal from the receiver? I tried to use analogRead() but it's just "garbage" and doesn't properly work.

How can I use a bicycle for a fishing pole? Sometimes, it just makes sense to use the right hardware.

What hardware would I need to replace?... I don't have any other fast and reliable method of control for Arduino (afaik) and I don't know of any other way to either; read PWM, send signlas or do anything else. Any suggestions or help you may give me? U_U

EDIT: By method of control I mean a wireless, fast and (as said) reliable, since the project would be creating an RC Car, thus why I'm trying to use the plane receiver and the 35MHz FM control. Any other methods or controls?...

What hardware would I need to replace?

My RC controlled truck runs around all over the place with not an Arduino in sight. It appears as though you want the Arduino to pretend to be a receiver for your RC transmitter. Why?

PaulS: It appears as though you want the Arduino to pretend to be a receiver for your RC transmitter. Why?

Because of not other reason than the simple fact that the (EDIT) RC Car* was salted, messed up and had been exposed to really high temperatures and humidity, thus leaving the board, connections and battery completely useless but yet the chasis and motors still work and function properly

EDIT: And also because I didn't want to trash the car, at the same time that I wanted to try and salvage parts for an Arduino project, rather than just throwing and trashing it all away

So, rather than buy a fishing pole to replace the damaged fishing pole, you wish to use a bicycle. And, you wish to complain when the bicycle doesn't catch as many fish. I see. Thanks for talking the time to explain.

Yeah, I know U_U

Anyways; I'm going to close this thread (is that even possible in this forum?) since it seems that my abnoxious question is just abnoxiously unanswerable due to the situation and state right now. Thanks anyways, PaulS for explaining and saying!

EDIT: Tagging as "UNSOLVABLE?" due to the simple thing and fact that I can't make it go faster or do anything in general to change the way this is going to work→ Reading PWM from a signal takes up time, and reading three channels and sending PWM back to L298N is kind of slow as well. Besides what has already been said, I'm up and open for explaining, giving info or taking suggestions and/or tips!

I wouldn't say unsolvable. Instead of using pulsein and blocking, why not read the pulses with an interrupt. That would work wouldn't it?

The forum has a Spanish section

There are several 100 threads on reading the output from RC Rx. It's easy to do so google the correct terms and the you are

Mark

PS An RC system does not use PWM.

holmes4: PS An RC system does not use PWM.

Utter nonsense. RC servos have been using PWM since at least the 1970s (it used to be called digi-prop back then)

Delta_G: read the pulses with an interrupt.

How though? I know it's possible, but I don't know how

AWOL: The forum has a Spanish section

(I feel more comfortable reading techinal info in English; it may sound weird, but I dunno. I just do.)

Yet again, I'm still searching and researching so; any re-directs, help or even a simple LMGTFY is helpful. Thanks again!

Haven't really gone over it, but this was my first google hit.

http://playground.arduino.cc/Code/ReadReceiver

Why wait for someone to LMGTFY you. WHy not just use google yourself. I got his for "Arduino RC receiver". It's not like I'm using obscure search terms here.

I would drop the RC transmitter and RC receiver and would build one from skratch If I were you. Use the same casing of the transmitter....

Most components needed, you have already. Just throw in two NRF24L01 modules (the ones with PA and external Antenna) and you are done.

I'm using it to control a modified Powerwheels Jeep, converted to RC, with my 2.5 year old daughter in it. Talking about reliability...

You can also make your own ESC instead of using a commercial one... And get tricks into it, like under-powering or overpowering a certain wheel when you are turning to improve handling in a corner...

You can read the receiver inputs using Pin Change Interrupts. When the pin goes HIGH note the time in microseconds. When the pin goes LOW subtract that from the current time in microseconds. The difference is the pulse length which will typically be 1000 to 2000 microseconds. The interrupts take very little time so it doesn't cause delays like pulseIn() does.

You say your motors are "brushless" but I don't think you can drive a BLDC (Brushless DC) motor with an L298N. Did you mean BRUSHED? If you have BLDC motors you will need some electronics to drive it. In the RC world that is usually done with an Electronic Speed Control (ESC). They are available for RC car drive motors (forward and reverse) and airplane/multicopter motors (forward only). The Arduino talks to them like they were and RC servo. Just like how the receiver talks to the Arduino.