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);
}
}