Code for arduino+remote controller+receptor2.4ghz+H BRIDGE DRIVER BTS7960

items that will be mentioned in this topic.
dc997 engine
![Sc5593fed253449a2a45e3a104ee606e1x.jpg_80x80.jpg_|80x80]

remote controller
S9e1caf8c6e034453a4c8a97248f37acai.jpg_80x80.jpg
2.4ghz receiver
Sd1ce9dedd40f4e8fa45e562fbe8c0116z.jpg_120x120.jpg_

H BRIDGE DRIVER BTS7960

arduino leonardo

I need that when the receiver connected to the Arduino's PWM 6 port receives signals, it communicates with the Arduino and the Arduino communicates with the BTS7960. I will leave photos of all the items so you can have a better idea. the remote control will be used only the trigger(channel3)
and it can be tightened either downwards or upwards. The idea is that when I press it down the engine accelerates and when I press it up it turns in the opposite direction to brake. I'm leaving this brake part to add later because I'm already having complications with acceleration. I made a pre code in the gpt chat. I want you to understand that I am a complete amateur programming and I just told GPT chat to do as I wanted. I want to know if the code can really work

// Definição dos pinos dos canais do receptor
int rcPins[6] = {5, 6, 7, 8, 9, 10};  // Canal 6 está no pino 10
float chValue[6];

// Pinos do driver de motor L298N
const int motorIN1 = 3;   // Pino IN1 do L298N
const int motorIN2 = 4;   // Pino IN2 do L298N
const int ENA = 11;      // Pino ENA do L298N (PWM para controlar a velocidade)

// Pino do botão
const int botaoAcelerar = 12;  // Pino do botão para controlar a aceleração do motor

// Valor máximo para o sinal do receptor em microsegundos (valores típicos para um receptor RC)
const int pulseInDelay = 20000;

float velocidadeMotor = 0.0;  // Valor inicial da velocidade do motor
const float incremento = 0.02; // Incremento da velocidade
const int maxPWM = 255;      // Valor máximo do PWM (0 a 255)

// Função para mapear valores
float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// Função para ler um canal do receptor
void readChannel(int channel) {
  int rawValue = pulseIn(rcPins[channel], HIGH, pulseInDelay);
  chValue[channel] = fmap((float)rawValue, 1000.0, 2000.0, 0.0, 1.0);
  chValue[channel] = chValue[channel] < 0.0 ? 0.0 : chValue[channel];
  chValue[channel] = chValue[channel] > 1.0 ? 1.0 : chValue[channel];
}

// Função para exibir os valores dos canais no Monitor Serial
void printChannel() {
  for(int iChannel = 0; iChannel < 6; iChannel++) {
    Serial.print("Ch #");
    Serial.print(iChannel);
    Serial.print(": ");
    Serial.println(chValue[iChannel]);
  }
  Serial.println("------------");
}

void setup() {
  // Inicializa o Monitor Serial
  Serial.begin(9600);

  // Configura os pinos do motor como saída
  pinMode(motorIN1, OUTPUT);
  pinMode(motorIN2, OUTPUT);
  pinMode(ENA, OUTPUT);

  // Configura o pino do botão como entrada com pull-up interno
  pinMode(botaoAcelerar, INPUT_PULLUP);

  // Inicializa o motor em estado LOW e PWM em 0
  digitalWrite(motorIN1, LOW);
  digitalWrite(motorIN2, LOW);
  analogWrite(ENA, 0);
}

void loop() {
  // Lê os canais do receptor
  for(int iChannel = 0; iChannel < 6; iChannel++) {
    readChannel(iChannel);
  }

  // Exibe os valores dos canais no Monitor Serial
  printChannel();

  // Lê o estado do botão para aumentar a velocidade
  if (digitalRead(botaoAcelerar) == LOW) {  // Botão pressionado para acelerar
    velocidadeMotor += incremento;
    if (velocidadeMotor > 1.0) velocidadeMotor = 1.0;  // Limita a velocidade máxima
  } else {
    velocidadeMotor -= incremento;
    if (velocidadeMotor < 0.0) velocidadeMotor = 0.0;  // Limita a velocidade mínima
  }

  // Mapeia o valor de aceleração de 0.0 a 1.0 para 0 a 255 (PWM para controle de velocidade)
  int pwmValue = fmap(velocidadeMotor, 0.0, 1.0, 0, maxPWM);

  // Mostra o valor da aceleração e a velocidade do motor no Monitor Serial
  Serial.print("Velocidade do Motor: ");
  Serial.println(pwmValue);

  // Define a direção e a velocidade do motor
  if (pwmValue > 0) {
    digitalWrite(motorIN1, HIGH);  // Define a direção do motor
    digitalWrite(motorIN2, LOW);
    analogWrite(ENA, pwmValue);  // Define a velocidade do motor com PWM
  } else {
    digitalWrite(motorIN1, LOW);
    digitalWrite(motorIN2, LOW);
    analogWrite(ENA, 0);  // Desliga o motor se a velocidade for 0
  }

  delay(50);  // Pequeno atraso para evitar a leitura excessiva do botão
}

I'm Brazilian I used the translator so sorry for the English

It's okey.

Thank You for posting the code. Did You make it Yourself?

Please post schematics.

As I wrote in the post, I'm a complete amateur. the code was made by gpt chat and I'm asking if it can work

many unnecessary code, but may work

Why didn't you tested it yourself?

I'll test it as soon as the driver bridge arrives. But for now I'm trying to get the Arduino to respond to the 2.4ghz and send it to the motor. Because previously this was my biggest difficulty in this project.

I know it doesn't make much sense, I just wanted to know if the code can be improved

#define rcPins 5  // Canal 3 do receptor

// Pinos do driver de motor L298N
const int motorIN1 = 3;   // Pino IN1 do L298N
const int motorIN2 = 4;   // Pino IN2 do L298N
const int ENA = 11;      // Pino ENA do L298N (PWM para controlar a velocidade)

// Valor máximo para o sinal do receptor em microsegundos (valores típicos para um receptor RC)
const uint16_t pulseInDelay = 21000;
const byte maxPWM = 255;      // Valor máximo do PWM (0 a 255)

void setup() {
  // Inicializa o Monitor Serial
  Serial.begin(115200);

  // Configura os pinos do motor como saída
  pinMode(motorIN1, OUTPUT);
  pinMode(motorIN2, OUTPUT);
  pinMode(ENA, OUTPUT);

  // Inicializa o motor em estado LOW e PWM em 0
  digitalWrite(motorIN1, LOW);
  digitalWrite(motorIN2, LOW);
  analogWrite(ENA, 0);
}

void loop() {
  static int pwmValue = 0;
  long rawValue = pulseIn(rcPins, HIGH, pulseInDelay);
  Serial.print("Ch #");
  Serial.print(iChannel);

  // Mapeia o valor de aceleração de 0 a 1000 para 0 a 255 (PWM para controle de velocidade)
  if (rawValue) {
    rawValue = constrain(rawValue, 1000, 2000);
    pwmValue = map(rawValue, 1000, 2000, 0, maxPWM);

    // Mostra o valor da aceleração e a velocidade do motor no Monitor Serial
    Serial.print("Velocidade do Motor: ");
    Serial.println(pwmValue);

    // Define a direção e a velocidade do motor
    if (pwmValue > 10) {
      digitalWrite(motorIN1, HIGH);  // Define a direção do motor
      analogWrite(ENA, pwmValue);  // Define a velocidade do motor com PWM
    }
    else {
      digitalWrite(motorIN1, LOW);
      digitalWrite(ENA, LOW);  // Desliga o motor se a velocidade for 0
    }
    delay(20);  // Pequeno atraso para evitar a leitura excessiva do botão
  }
}

In this case I would use pin 5 to connect the 2.4ghz receiver?

yes.

or you want other pin?

No, not without problems. I will test the code now and send feedback