[SOLVED] Unwanted speed up of motors!?

Hello,

A have a project, where I want to control a Brushless motors with a ESCs and a Arduino Nano.

My problem:
The motors sometimes accelerate briefly and jerkily. [Shown in the video]

Video:
https://streamable.com/1hc1i

In the video, I always use the same speed. Because of this, the problem is not related to changing the speed.

It is definitely a programming issue, because the have two scripts. The first script works without the problem, but the second script have the problem.

Script 1 (my motor test script, also works with 4 motors) [is working]:

#include <Servo.h>
#define MIN_PULSE_LENGTH 1180
#define MAX_PULSE_LENGTH 1508
Servo mot;
void setup() {
    mot.attach(7, MIN_PULSE_LENGTH, MAX_PULSE_LENGTH);
    mot.writeMicroseconds(MIN_PULSE_LENGTH);
    delay(8000);
    for (int i = MIN_PULSE_LENGTH; i <= MAX_PULSE_LENGTH; i += 5) {
        mot.writeMicroseconds(i);
        delay(200);
    }
    for (int i = MAX_PULSE_LENGTH; i >= MIN_PULSE_LENGTH; i -= 5) {
        mot.writeMicroseconds(i);
        delay(200);
    }
}
void loop() {}

Script 2 (controlling the motor with remote control) [It does not work]:

If I add after setESC(realspeed); (in the middle of the code in the for loop) a delay(1000) the problem is rarer, BUT my command have a delay of 1 second (who would have thought that)

#include <SPI.h>
#include <Servo.h>
#include <RH_RF95.h>

#define RF95_FREQ 868.0
#define RFM95_CS 4
#define RFM95_RST 2
#define RFM95_INT 3

#define MIN_PULSE_LENGTH 1180 // Minimum pulse length in µs
#define MAX_PULSE_LENGTH 1508 // Maximum pulse length in µs

RH_RF95 rf95(RFM95_CS, RFM95_INT);
Servo m1, m2, m3, m4;

void setup()
{
  pinMode(RFM95_RST, OUTPUT);
  
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  Serial.begin(115200);
  while (!Serial)delay(1);
  delay(100);

  Serial.println("Starting...");

  Serial.println("Attach motors");
  m1.attach(7, MIN_PULSE_LENGTH, MAX_PULSE_LENGTH);
  m2.attach(8, MIN_PULSE_LENGTH, MAX_PULSE_LENGTH);
  m3.attach(9, MIN_PULSE_LENGTH, MAX_PULSE_LENGTH);
  m4.attach(10, MIN_PULSE_LENGTH, MAX_PULSE_LENGTH);
  
  m1.writeMicroseconds(MIN_PULSE_LENGTH);
  m2.writeMicroseconds(MIN_PULSE_LENGTH);
  m3.writeMicroseconds(MIN_PULSE_LENGTH);
  m4.writeMicroseconds(MIN_PULSE_LENGTH);
  delay(8000);

  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);

  while (!rf95.init()) {
    Serial.println("LoRa radio init failed");
    while (1);
  }
  Serial.println("LoRa radio init successful!");
  
  if (!rf95.setFrequency(RF95_FREQ)) {
    Serial.println("setFrequency failed");
    while (1);
  }
  rf95.setTxPower(23, false);
}
void loop()
{
  if (rf95.available())
  {
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);

    if (rf95.recv(buf, &len))
    {
      char* received = (char*)buf;
      if(received[0] == ':'){
        String property = "";
        String val = "";
        bool pf = false;
        for(unsigned int i = 1; i < strlen(received); i++){
          if(received[i] == ',')pf = true;
          else if(received[i] == ':'){
            if(property == "speed"){
              int realspeed = val.toInt()*5+MIN_PULSE_LENGTH;
              setESC(realspeed);
            }
            pf = false;
            val="";
            property="";
          }
          else if(pf)val+= received[i];
          else property+=received[i];
        }
      }
      else if(strcmp(received, "rssi") == 0){
        uint8_t data[] = "1";
        rf95.send(data, sizeof(data));
        rf95.waitPacketSent();
      }
    }
    else Serial.println("Receive failed");
  }
}
int lastValue = 0;


void setESC(int val){
  if(val != lastValue){ 
    m1.writeMicroseconds(val);
    m2.writeMicroseconds(val);
    m3.writeMicroseconds(val);
    m4.writeMicroseconds(val);
    lastValue = val;
  }
}

Example input from remote control:

:speed,0:; //speed 0
:speed,65:; //max speed

Thanks for your answer.

Add a Serial.println to setESC so you can see what commands are actually being sent to the motors. Then
you'll know if its software or hardware related.

I have already done that, but everything looks fine.

Thanks for all your answers!

Now I have solved the problem!

The problem was that the Arduino received so much data, that it was overloaded and calculated the speed values wrong.

Alwin07:
Thanks for all your answers!

Now I have solved the problem!

The problem was that the Arduino received so much data, that it was overloaded and calculated the speed values wrong.

The problem was that my software was overloaded and calculated the speed values wrong.

There, corrected that for you.