1

Topic: How to Control a motor with BTS7960 with Remote XY joystick

Hello I'm trying to figure out how to get my 2 motors to work with the remote XY app, BLE HM-10, Arduino Uno and 2 BTS7960 (one for each motor). I will be honest I'm new to all of this and have tried to copy portions of sketches I've downloaded that people are using the same setup but a ps2 controller instead of Remote XY any help would be awesome. I keep getting an Error 1 when I try to verify the Code. I'm trying to run my ble controller using pins 11 and 12, pin 8 as an enable for both H bridges (BTS 7960), Pins 6 and 9 for my Left PWM on the BTS7960, and Pins 3 and 5 for my Right PWM on the BTS7960. I'm also trying to use an S and F button on the Remote XY app so when S is pressed the PWM is limited to 80 and when F is pressed it can go to max of 250. Any help would be amazing.

/*
   -- New project --
   
   This source code of graphical user interface
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 3.1.6 or later version
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.8.01 or later version;
     - for iOS 1.5.1 or later version;
   
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.   
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>

#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 12
#define REMOTEXY_SERIAL_TX 11
#define REMOTEXY_SERIAL_SPEED 9600


// RemoteXY configurate 
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,4,0,0,0,47,0,13,8,1,
  5,32,7,48,50,50,0,26,16,129,
  0,10,11,42,10,31,71,111,100,100,
  97,114,100,0,1,0,8,32,12,12,
  0,31,83,0,1,0,44,32,12,12,
  0,31,70,0 };
 
// this structure defines all the variables and events of your control interface
struct {

    // input variables
  int8_t joystick_1_x; // =-100..100 x-coordinate joystick position
  int8_t joystick_1_y; // =-100..100 y-coordinate joystick position
  uint8_t Speed_1; // =1 if button pressed, else =0
  uint8_t Speed_2; // =1 if button pressed, else =0

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define Speed_1  80
#define Speed_2  250

String inString = "";    // string to hold input
int motorPWM = 0;

int defaultSpeed = 80;
int leftAdd = 20;

const uint8_t EN = 8;
const uint8_t L1_PWM = 6;
const uint8_t L2_PWM = 9;
const uint8_t R1_PWM = 3;
const uint8_t R2_PWM = 5;

BTS7960 motorController_1(EN, L1_PWM, L2_PWM);

BTS7960 motorController_2(EN, R1_PWM, R2_PWM);


void setup()
{
  RemoteXY_Init ();
 
   Serial.begin(9600);
  pinMode(R1PWM_Output, OUTPUT);
  pinMode(R2PWM_Output, OUTPUT);

  pinMode(L1PWM_Output, OUTPUT);
  pinMode(L2PWM_Output, OUTPUT);
 
}

void loop()
{
  RemoteXY_Handler ();

    motorController.Enable();

  for(int speed = 0 ; speed < 255; speed+=10)
  {
    motorController.TurnLeft(speed);
    delay(100);
  } 

  motorController.Stop();
 
  for(int speed = 255 ; speed > 0; speed-=10)
  {
    motorController.TurnLeft(speed);
    delay(100);
  } 
  motorController.Stop();

  motorController.Disable();
 
 
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay()

2

Re: How to Control a motor with BTS7960 with Remote XY joystick

Welcome

A '}' is missing at the end, to close the loop() function.

Your code will not really work as it is, because it's blocking for a long time.

For example, this loop :

for(int speed = 0 ; speed < 255; speed+=10)
{
  motorController.TurnLeft(speed);
  delay(100);
}

will block your code for ~2.5 seconds (255/10*100ms), and you have two of these loops

During these ~5 seconds, your arduino cannot do anything else, but RemoteXY need your arduino to be responsive or it will lose connection, or at least not respond to your controls.

One easy (bad) solution is to call RemoteXY_Handler inside the for loop so at least it will be called once every 100ms

for(int speed = 0 ; speed < 255; speed+=10)
{
  motorController.TurnLeft(speed);
  delay(100);
  RemoteXY_Handler();
}

A much better solution would be to rewrite your code so that it is not blocking at all (using millis instead of delay). See the Blink Without Delay example in the Arduino IDE.


Other problems:

- You can't have a #define with the same name as a variable

uint8_t Speed_1; // =1 if button pressed, else =0
uint8_t Speed_2; // =1 if button pressed, else =0

#define Speed_1  80
#define Speed_2  250

- You forgot to #include the BTS7960 library

- Your variable motorController doesn't exist, instead you have declared variables motorController_1 and motorController_2 so use that

3

Re: How to Control a motor with BTS7960 with Remote XY joystick

Would this work better? I still have the issue with closing the loop even though I added } to the end. Also is there anyway you could help me to add millis to this code instead of delay? apologies I am extremely new to this. Also would this code allow for me to run both motors with the remote XY joystick and have the S button decrease speed to 80 and the F button increase to 250?

/*
   -- New project --
   
   This source code of graphical user interface
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 3.1.6 or later version
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.8.01 or later version;
     - for iOS 1.5.1 or later version;
   
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.   
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>
#include <BTS7960>


#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 12
#define REMOTEXY_SERIAL_TX 11
#define REMOTEXY_SERIAL_SPEED 9600


// RemoteXY configurate 
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,4,0,0,0,47,0,13,8,1,
  5,32,7,48,50,50,0,26,16,129,
  0,10,11,42,10,31,71,111,100,100,
  97,114,100,0,1,0,8,32,12,12,
  0,31,83,0,1,0,44,32,12,12,
  0,31,70,0 };
 
// this structure defines all the variables and events of your control interface
struct {

    // input variables
  int8_t joystick_1_x; // =-100..100 x-coordinate joystick position
  int8_t joystick_1_y; // =-100..100 y-coordinate joystick position
  uint8_t Speed_1; // =1 if button pressed, else =0
  uint8_t Speed_2; // =1 if button pressed, else =0

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define S  80
#define F  250


String inString = "";    // string to hold input
int motorPWM = 0;

int defaultSpeed = 80;
int leftAdd = 20;

const uint8_t EN = 8;
const uint8_t L1_PWM = 6;
const uint8_t L2_PWM = 9;
const uint8_t R1_PWM = 3;
const uint8_t R2_PWM = 5;

BTS7960 motorController_1(EN, L1_PWM, L2_PWM);

BTS7960 motorController_2(EN, R1_PWM, R2_PWM);


void setup()
{
  RemoteXY_Init ();
 
   Serial.begin(9600);
  pinMode(R1PWM_Output, OUTPUT);
  pinMode(R2PWM_Output, OUTPUT);

  pinMode(L1PWM_Output, OUTPUT);
  pinMode(L2PWM_Output, OUTPUT);
 
}

void loop()
{
  RemoteXY_Handler ();

    motorController_1.Enable();
    motorController_2.Enable();

  for(int speed = 0 ; speed < 255; speed+=1)
  {
    motorController_1.TurnLeft(speed);
    delay(100);
  } 

  motorController_1.Stop();
  motorController_2.Stop();
 
  for(int speed = 255 ; speed > 0; speed-=10)
  {
    motorController_2.TurnLeft(speed);
    delay(100);
  } 
  motorController_1.Stop();
  motorController_2.Stop();

  motorController_1.Disable();
  motorCOntroller_2.Disable();
}
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay()

4

Re: How to Control a motor with BTS7960 with Remote XY joystick

jcoreson1 wrote:

I still have the issue with closing the loop even though I added } to the end

When posting code here, it is best to use the <> icon in the editor and paste the code where the cursor flashes.



There is a good example of using the joystick to control two motors here - https://remotexy.com/en/examples/car/

It assumes using the L298N, but a little googling should show how similar your controller would be to use instead.

"And voila, which is French for.......'and then I found out.'" - Ready Player One