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

2.4ghz receiver

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
