Controlling BTS7960 with arduino and radio controller

high I been trying to control a brushed motor to go forward and backward by using a radio controller and the bts7960 plus an Arduino but my code doesn't work every time I move joystick forward or backward the motor just rolls to on direction and doesn't want to stop I have to unplug power to stop it, this my code please tell what am doing wrong.

#include <BTS7960.h>

/*
BTS7960-43A-Driver
made on 22 Nov 2020
by Amir Mohammad Shojaee @ Electropeak
Home

*/

#define RPWM 5
#define LPWM 6
#define REN 9
#define LEN 10

int rc = 7;
int forward;
int backward;
int duration;
void setup() {
Serial.begin(9600);
pinMode(RPWM,OUTPUT);
pinMode(LPWM,OUTPUT);
pinMode(LEN,OUTPUT);
pinMode(REN,OUTPUT);
digitalWrite(REN,HIGH);
digitalWrite(LEN,HIGH);

}

void loop() {

duration = constrain(pulseIn(rc, HIGH), 1000 , 2000);

if (duration <=1600 && duration >= 1300 ){
analogWrite(forward, 0);
analogWrite(backward, 0);
}

if(duration>1520){
forward=map(duration, 1600, 2000, 0, 255);
analogWrite(RPWM,forward);
analogWrite(LPWM,0);

}

if(duration<1400){
backward=map(duration, 1300, 1000, 0, 255);
analogWrite(LPWM,backward);
analogWrite(RPWM,0);

}

}

1 Like

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

I suggest that you add some serial print statements in your code so you can follow the flow.

Your topic has been moved to a more suitable location on the forum. This has nothing to do with bootloader and the likes.

If the pulseIn() fails (does not see a pulse within one second) it returns zero. Your constrain() will force that to 1000 (maximum backward). You should probably check for a valid result from pulseIn() before you constrain it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.