Controlling motors via serial communication + joysticks values

Me and a friend are currently working on a project for university trying to build a Drive-By-Wire Go-Cart. Our setup is 2 wheel hub motors + 1 serial controller that can speak to each motor seperately. Our current goal is to control these motors using a Joystick (We have got 2 PS2 Joysticks and a virtual joystick via the blynk app working). We have some limited, basic programming language.

The serial Controller works with a few limited commands. When we connect it to standard RX/TX pins of the arduino DUE, we can easily serial.println("f0") these commands to get ractions from the motors.

The commands are as follows:
"f41" switch motors on + forward direction
"f45" same + backwards
"d0" - "d255" Speed

With the following code we can create Routines for the motors.

WString a;

void setup() {

  Serial.begin(38400); // opens serial port, sets data rate to 9600 bps

}

void loop() {

  while (Serial.available()) {

    a = Serial.readString(); // read the incoming data as string

    Serial.println(a);

    delay(5000);
    Serial.println("d20");
    delay(5000);
    Serial.println("f41");
    delay(3000);
    Serial.println("d0");
    Serial.println("f0");
    delay(5000);
    Serial.println("f45");
    delay(5000);

    Serial.println("d20");
    delay(3000);
    Serial.println("d0");
    Serial.println("f0");
    delay(5000);
  }
}

We upload the code to the arduino, switch on the controller, it prints out the current preset values of the motors (irrelevant for us, stuff like operating voltage...) and then starts the routine

We also got Blynk working via Bluetooth connection, getting exact data from the virtual joystick printed to the serial monitor.

Here we tried to integrate one code into the other and got some problems:

#define BLYNK_PRINT Serial
#include <BlynkSimpleSerialBLE.h>

BlynkTimer timer;

String c;
//char auth[] = "cPWgF8QdQV61ZaBJt-2ZUMUbKAzCBgsf";
char auth[] = "qAqND9BWc6BKjpSVGx-Jis7QRiTbDBpR";


BLYNK_WRITE(V1) {

  
  int x = param[0].asInt();
  int y = param[1].asInt();
  int a; 
  int b;

  // Do something with x and y printet dann x und y werte auf monitor
  // Serial.print("X = ");
  // Serial.print(x);
  // Serial.print("; Y = ");
  // Serial.println(y);
   
    while(Serial.available()) 
  {
  c= Serial.readString();// read the incoming data as string
  Serial.println(c);
  }
    
    if(y > 40)                        // Joystickvalues 0-80 > 40 means forward
      { 
        Serial.println("f41");
        Serial.println((String)"d15");
      }
      if(y < 40)
          {
        Serial.println("f45");
        Serial.println((String)"d15");         
          }
}

void setup() {

/*{
  timer.setInterval(); //timer will run every sec 
}*/
Serial.begin(38400); 
Serial3.begin(9600);
Blynk.begin(Serial3, auth);

}

void loop()
{ 
  Blynk.run();  
}

Dont mind int a / int b. All we tried is to get the motors moving forward or backwards, depending of the position of the joystick. We have the Bluetooth HC-05 module connected to serial pins RX3/TX3

Don´t laugh at the code, we know there is probably a easy fix that we just dont see with our basic knowledge. What might be a problem is that printing the "f" commands every loop causes some problems, only the "d" values should be changed when moving the joystick (aslong as its y > 40). Maybe we need to connect the controller to a different serial port? Maybe we need some sort of timing ?

When we tried out the code, we got the x / y values printed to the monitor, when switching on the controller, we got the standard message with the preset motorvalues. After switching it on, no x/ y values appeared on the monitor anymore until we switched it of again.

Any help regarding syntax or special functions we need to use to get it working are highly apprecciated, Tyvm in advance

This looks very strange

    while(Serial.available()) 
    {
        c= Serial.readString();// read the incoming data as string
        Serial.println(c);
    }
    
    if(y > 40)

You have gathered a value into the variable 'c' and you are testing a variable 'y' which does not seem to be updated anywhere.

Using single-character variable names is just bad practice. They are meaningless and it makes the variables impossible to find with SEARCH.

ALWAYS use meaningful variable names (even for short programs) and it will be much less likely that you test the wrong variable.

Separately, 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).

And 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.

...R