tank robot control Motor Driver 2A Dual L298 H-Bridge with 2ch RC receiver

hello, I have a project about tank robot and try to use code to control with 4 dc motors with RC control 6 Channels I use 2 channels the problem when I Disconnect the power Source from the transmitter or receiver tow of motors go fast crazy can anyone help me plz
by the way, I use 2 lm298 h-bridge and Arduino nano

the code

float ch1; // Here's where we'll keep our channel values
float ch2;
float f1;
float f2;
float x;
float y;
float r;
float t;
float left;
float right;
float MotorSpeed1;
float MotorSpeed2;

#define Q_PI 0.7853981633974482;

// Motor A LEFT
int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B RIGHT
int enB = 3;
int in3 = 10;
int in4 = 4;

void setup() {
// put your setup code here, to run once:

pinMode(5, INPUT); // Set our input pins as such
pinMode(6, INPUT);
Serial.begin(9600);

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Motor A
digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

// Motor B
digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void loop() {
// put your main code here, to run repeatedly:

ch1 = pulseIn(5, HIGH, 25000); // Read the pulse width of
ch2 = pulseIn(6, HIGH, 25000); // each channel
ch1 = map(ch1, 1000,2000, -500, 500);
ch2 = map(ch2, 1000,2000, -500, 500);
ch1 = constrain(ch1, -500, 500);
ch2 = constrain(ch2, -500, 500);

//f1 = map(ch1, 1000,2000, -1, 1);
//f2 = map(ch2, 1000,2000, -1, 1);

x = ch1/500;
y = ch2/500;

//y = y*-1;

r = sqrt(xx + yy);
t = atan2(x,y);

t = t + Q_PI

left = r* cos(t);
right = r* sin(t);

//rescale the new coords
left = left * sqrt(2);
right = right * sqrt(2);

// clamp to -1/+1
left = max(-1, min(left, 1));
right = max(-1, min(right, 1));

if(left > 0) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}else{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}

if(right > 0){
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}else{
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

// Set the motor speeds
MotorSpeed1 = abs(left255);
MotorSpeed2 = abs(right
255);
//Bzuceni motoru
if (MotorSpeed1 < 25)MotorSpeed1 = 0;
if (MotorSpeed2 < 25)MotorSpeed2 = 0;
analogWrite(enA, MotorSpeed1);
analogWrite(enB, MotorSpeed2);

Serial.print("A:"); //Serial debugging stuff
Serial.print(left*255);

Serial.print("B"); //Serial debugging stuff
Serial.println(right*255);

}

Sorry I can't really follow that code with all the single letter variables and no real comments. But I can only see any code for 2 motors but you say you have 4. Which 2 is it that "go fast crazy"?

Stopping the transmitter with everything else still powered up is always a bad idea. In RC you switch the receiver off first and then transmitter. Some RC receivers do not react well when they lose signal. I would also put checks in so that if pulseIn() returns 0 i.e. no signal, your code does something sensible like switch everything off.

I'd also put a few more Serial.prints in e.g. of the actual values you are writing to the motors like MotorSpeed1 and 2 and also of ch1 and ch2 from the receiver.

Steve

I've deleted your other cross-post @allforoneandoneforall.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

hi steve
first thanks for your response
about the motors I use lm298 motor driver which, Handel, with two dc motors in my project I add two motors drivers and the motor that goes crazy fast when I powered off the receiver or transmitter the right one

ch1 = pulseIn(5, HIGH, 25000); pulseIn returns unsigned long (uint32_t) not float.

Please remember to use code tags blah blah blah

plz what do you Means more explained

#define Q_PI 0.7853981633974482;I suggest you lose the semicolon.