Drok motor driver help

I am hoping someone can help me out here. I’m working on building a remote controlled “Lawn Bot”. I bought this motor driver to power two wheelchair motors using 24v power supply remotely using HC-12 transceivers between 2 arduinos. To keep it simple, I got the motors to work in one direction but I can’t seem to get them to work in the opposite direction. I’m fairly new at coding and am still trying to figure everything out so don’t be too hard on me. :o

Here is a link to the motor driver. There’s a user manual pdf under “technical Specifications”:
https://www.amazon.com/Controller-DROK-H-Bridge-Brushed-Regulator/dp/B078TFLD7Q

Here is the code for the receiving end:

//Receiver

const char EOPmarker = '*'; //This is the end of packet marker
char serialbuf[32]; //This gives the incoming serial some room. Change it if you want a longer incoming.
 
#include <SoftwareSerial.h>
#include<Servo.h> 
#include <String.h> // we'll need this for subString

#define MAX_STRING_LEN 20 // like 4 lines above, change as needed.

// Motor 1 
int en1 = 9;
int dr1 = 8;
int pwm1 = 7;
 
// Motor 2 
int en2 = 3;
int dr2 = 5;
int pwm2 = 4;
 
SoftwareSerial HC12(10, 11); // RX, TX

void setup(){
  Serial.begin(9600);
  HC12.begin(9600);

  pinMode(en1, OUTPUT);
  pinMode(en2, OUTPUT);
  pinMode(dr1, OUTPUT);
  pinMode(dr2, OUTPUT);
  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);

}
 
void loop() {

    while (HC12.available() > 0) {      //makes sure something is ready to be read
      static int bufpos = 0;            //starts the buffer back at the first position in the incoming serial.read
      char inchar = HC12.read();        //assigns one byte (as serial.read()'s only input one byte at a time
      if (inchar != EOPmarker) {        //if the incoming character is not the byte that is the incoming package ender
        serialbuf[bufpos] = inchar;     //the buffer position in the array get assigned to the current read
        bufpos++;                       //once that has happend the buffer advances, doing this over and over again until the end of package marker is read.
      }
      else {                            //once the end of package marker has been read
        serialbuf[bufpos] = 0;          //restart the buff
        bufpos = 0;                     //restart the position of the buff
 
        int M1 = atoi(subStr(serialbuf, ",", 1));
        int M2 = atoi(subStr(serialbuf, ",", 2));
        int BH = atoi(subStr(serialbuf, ",", 3));
        int BW = atoi(subStr(serialbuf, ",", 4));
        int BS = atoi(subStr(serialbuf, ",", 5));
        int BX = atoi(subStr(serialbuf, ",", 6));
        
  // Joystick Input 
int Motor1 = M1; 
int Motor2 = M2;
int Motor1R = M1*-1; 
int Motor2R = M2*-1;
 
      if ( Motor1 > 2 ){
        digitalWrite(dr1, HIGH);
        digitalWrite(en1, HIGH);
        analogWrite(pwm1, Motor1);}
       
      else {
        digitalWrite(en1, LOW);

}

      if ( Motor1 < -2 ){
        digitalWrite(dr1, LOW);
        digitalWrite(en1, HIGH);
        analogWrite(pwm1, Motor1R);}
       
       else {
        digitalWrite(en1, LOW);

}

      if ( Motor2 > 2 ){
        digitalWrite(dr2, HIGH);
        digitalWrite(en2, HIGH);
        analogWrite(pwm2, Motor2);}
       
      else {
        digitalWrite(en2, LOW);

}

      if ( Motor2 < -2 ){
        digitalWrite(dr2, LOW);
        digitalWrite(en2, HIGH);
        analogWrite(pwm2, Motor2R);}
       
       else {
        digitalWrite(en2, LOW);

}  
  
        Serial.print(" M1: ");
        Serial.print(Motor1);
        Serial.print("  | M2: ");
        Serial.print(Motor2);
        Serial.print(" | M1R: ");
        Serial.print(Motor1R);
        Serial.print("  | M2R: ");
        Serial.print(Motor2R);
        
        Serial.print("  | B1: ");
        Serial.print(BH);
        Serial.print("  | B2: ");
        Serial.print(BW);
        Serial.print("  | B3: ");
        Serial.print(BS);
        Serial.print("  | B4: ");
        Serial.print(BX);
        Serial.println();
        delay(20);

      }
    }
}
 
char* subStr (char* input_string, char *separator, int segment_number) {
  char *act, *sub, *ptr;
  static char copy[MAX_STRING_LEN];
  int i;
  strcpy(copy, input_string);
  for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
    sub = strtok_r(act, separator, &ptr);
    if (sub == NULL) break;

  }
 return sub;
}

I have a feeling it’s something simple that I just haven’t caught yet.

Thanks in advance!

Have you managed to get one of the motors to work in both directions using a simple program that has no wireless stuff? if not, that should be your first step.

You have not told us what message is being sent using the wireless connection. have you checked whether the messages are being received correctly.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with ‘\0’ (NULL).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

Thanks for the quick reply but sorry for the slow response...

Here is the code that is being sent wirelessly from the "transmitting" arduino:

String ss;
 LM = map(analogRead(A2), 0, 1024, -255, 255);
 RM = map(analogRead(A0), 0, 1024, -255, 255);
 
  ss+=LM;      //ss = ss + sentRM
  ss+=",";     //ss = ss + &
  ss+=RM;
    ss+=",";
    ss+=stateHL;
    ss+=",";
    ss+=stateWL;
/*    ss+=",";
    ss+=BS;
    ss+=",";
    ss+=BX;    */
    ss+="*";
             
    Serial.println(ss);
    HC12.println(ss);

I just tried code where everything is hardwired into the arduino, but I get the same result. I did, however, swap out the Drok H-Bridge with a L298N H-Bridge (Connected different motors using a 12v power supply) and everything works fine.

I did notice that I am getting this error for all the strings of my original code when I am uploading to the arduino:

"warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]"

So, I will modify the code to use cstrings like you suggested and go from there unless there is something a little more obvious that I'm not seeing?

@SpekTater, are you the same person as @kroon001?

If so why have you got two accounts?

...R

Yes, I'm the same person. I was in the process of deleting my first account and creating a new one. Not sure why it posted under my deleted account though even though it's still linked to this new one. I'm confused about as about as much as you are. ::slight_smile:

SpekTater:
Thanks for the quick reply but sorry for the slow response...

You have not answered the first and most important question in Reply #1

...R

Have you managed to get one of the motors to work in both directions using a simple program that has no wireless stuff? if not, that should be your first step.

The simple answer is no, I did not get it working in both directions using a program that is not wireless.

I just tried code where everything is hardwired into the arduino, but I get the same result. I did, however, swap out the Drok H-Bridge with a L298N H-Bridge (Connected different motors using a 12v power supply) and everything works fine.

The receiving arduino was getting all of the correct information from the transmitting arduino but with an error code of: "warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]" for each string read so I am rewriting the code using your Serial Input Basics examples to get rid of those errors.

That's a warning, not an error. The compiler still correctly compiled your program.

So, just thought I’d post an update. I got everything working using the L298N H-Bridge by changing the motor code to:

      if ( Motor1 > 2 ){
        digitalWrite(dr2, HIGH);
        digitalWrite(pwm2, LOW);
        analogWrite(en1, Motor1);}

       else if ( Motor1 < 2 ){
        digitalWrite(dr1, LOW);
        digitalWrite(pwm1, HIGH);
        analogWrite(en1, Motor1R);}
       
      else {
        digitalWrite(en1, LOW);

}

      if ( Motor2 > 2 ){
        digitalWrite(dr2, HIGH);
        digitalWrite(pwm2, LOW);
        analogWrite(en2, Motor2);}

       else if ( Motor2 < 2 ){
        digitalWrite(dr2, LOW);
        digitalWrite(pwm2, HIGH);
        analogWrite(en2, Motor2R);}
       
      else {
        digitalWrite(en2, LOW);

}

I’m still having a problem getting the Drok H-Bridge to work and power my larger motors though.

SpekTater:
The simple answer is no, I did not get it working in both directions using a program that is not wireless.

Then stick with that until you have a solution. Keep things simple.

...R