Arduino code running slow, unoptimized code?

The arduino doesnt print very fast, it prints only every few seconds

#include <Servo.h>

int YawInput = A0;
int ThrottleInput = A1;
int ch3 = A2;
int ch4 = A3;
//Output section declaration
int lmwpin = 3;
int rmwpin = 5;
int lbwpin = 6;
int rbwpin = 9;
int rudderpin = 10;
int ThrottleOutput = 11;
Servo Motor;
Servo lmw;
Servo rmw;
Servo lbw;
Servo rbw;
Servo rudder;


void setup()
{
  // Input section
  pinMode(YawInput, INPUT);
  pinMode(ThrottleInput, INPUT);
  pinMode(ch3, INPUT);
  pinMode(ch4, INPUT);
  // Output section
  pinMode(lmwpin, OUTPUT);
  pinMode(rmwpin, OUTPUT);
  pinMode(lbwpin, OUTPUT);
  pinMode(rbwpin, OUTPUT);
  pinMode(rudderpin, OUTPUT);
  Motor.attach(11);
  lmw.attach(3);
  rmw.attach(5);
  lbw.attach(6);
  rbw.attach(9);
  rudder.attach(10);
  Serial.begin(9600);
  // ESC ARMING 


}

void loop()
{
  int ReadYaw = pulseIn(YawInput, HIGH);
  int ReadThrottle = pulseIn(ThrottleInput, HIGH);
  int Readch3 = pulseIn(ch3, HIGH);
  int Readch4 = pulseIn(ch4, HIGH);
//Mapping
  int YawMap = map(ReadYaw, 870, 1915, 0, 180);
  int Throttle = map(ReadThrottle, 870, 1915, 0, 180);
if(Readch3 > 1300)
{
  Serial.println("");
     // Roll
     Serial.print("Roll - ");
    if(YawMap < 85)
    {
      Serial.println("Roll left");
      lmw.write(YawMap);
      rmw.write(YawMap);
      lbw.write(YawMap);
      rbw.write(YawMap);
    }
    else{}
    if(YawMap > 85 && YawMap < 95)
    {
      Serial.println("Roll stable");
      lmw.write(90);
      rmw.write(90);
      lbw.write(90);
      rbw.write(90);
    }
    else{}
    if(YawMap > 95)
    {
      Serial.println("Roll right");
    }
    // Pitch



     Serial.print("Pitch - ");
    if(ReadThrottle < 1200)
      {
      Serial.println("Down"); 
      }
    else{}
    if(ReadThrottle > 1201 && ReadThrottle < 1800)
      {
        Serial.println("Middle");
      }
    else{}
    if(ReadThrottle > 1800)
    {
      Serial.println("Up");
    }
    else{}

}
else{}
if(Readch3 < 1300)
{
   Serial.println("");
     // YAW
     Serial.print("Yaw - ");
    if(ReadYaw < 1200)
      {
      Serial.println("left"); 
      }
    else{}
    if(ReadYaw > 1201 && ReadYaw < 1800)
      {
        Serial.println("Middle");
      }
    else{}
    if(ReadYaw > 1800)
    {
      Serial.println("Right");
    }
    else{}



    // Throttle



     Serial.print("Throttle - ");
    if(ReadThrottle < 1200)
      {
      Serial.println("Low"); 
      }
    else{}
    if(ReadThrottle > 1201 && ReadThrottle < 1800)
      {
        Serial.println("Middle");
      }
    else{}
    if(ReadThrottle > 1800)
    {
      Serial.println("High");
    }
    else{}



}
else{}
  delay(1);
}

your if ...else constructs can be made with less comparisons.
Furthermore you miss some values in your tests,
and you could use a higher baudrate (assumption)

Note that your print rate depends on the duration of the four pulseIn() commands.
Do you need to measure them all (always)?
At least one is not used...

#include <Servo.h>

int YawInput = A0;
int ThrottleInput = A1;
int ch3 = A2;
int ch4 = A3;

//Output section declaration
int lmwpin = 3;
int rmwpin = 5;
int lbwpin = 6;
int rbwpin = 9;
int rudderpin = 10;
int ThrottleOutput = 11;

Servo Motor;
Servo lmw;
Servo rmw;
Servo lbw;
Servo rbw;
Servo rudder;


void setup()
{
  // Input section
  pinMode(YawInput, INPUT);
  pinMode(ThrottleInput, INPUT);
  pinMode(ch3, INPUT);
  pinMode(ch4, INPUT);
  
  // Output section
  pinMode(lmwpin, OUTPUT);
  pinMode(rmwpin, OUTPUT);
  pinMode(lbwpin, OUTPUT);
  pinMode(rbwpin, OUTPUT);
  pinMode(rudderpin, OUTPUT);
  
  Motor.attach(11);
  lmw.attach(3);
  rmw.attach(5);
  lbw.attach(6);
  rbw.attach(9);
  
  rudder.attach(10);
  Serial.begin(9600);  //  why not higher e.g. 115200
  // ESC ARMING


}

void loop()
{
  int ReadYaw = pulseIn(YawInput, HIGH);
  int ReadThrottle = pulseIn(ThrottleInput, HIGH);
  int Readch3 = pulseIn(ch3, HIGH);
  // int Readch4 = pulseIn(ch4, HIGH);

  //  Mapping
  int YawMap = map(ReadYaw, 870, 1915, 0, 180);
  //  int Throttle = map(ReadThrottle, 870, 1915, 0, 180);

  if (Readch3 > 1300)
  {
    Serial.println("");
    // Roll
    Serial.print("Roll - ");
    if (YawMap < 85)
    {
      Serial.println("Roll left");
      lmw.write(YawMap);
      rmw.write(YawMap);
      lbw.write(YawMap);
      rbw.write(YawMap);
    }
    else if (YawMap < 95)
    {
      Serial.println("Roll stable");
      lmw.write(90);
      rmw.write(90);
      lbw.write(90);
      rbw.write(90);
    }
    else
    {
      Serial.println("Roll right");
    }

    // Pitch
    Serial.print("Pitch - ");
    if (ReadThrottle < 1200)
    {
      Serial.println("Down");
    }
    else if (ReadThrottle < 1800)
    {
      Serial.println("Middle");
    }
    else
    {
      Serial.println("Up");
    }

  }
  else //  Readch3 <= 1300
  {
    Serial.println();
    // YAW
    Serial.print("Yaw - ");
    if (ReadYaw < 1200)
    {
      Serial.println("left");
    }
    else if (ReadYaw < 1800)
    {
      Serial.println("Middle");
    }
    else
    {
      Serial.println("Right");
    }

    // Throttle
    Serial.print("Throttle - ");
    if (ReadThrottle < 1200)
    {
      Serial.println("Low");
    }
    else if (ReadThrottle < 1800)
    {
      Serial.println("Middle");
    }
    else
    {
      Serial.println("High");
    }
  }
  delay(1);
}

Thanks, ill try that out

Btw I am reading from a rc receiver and writing it to servos and motors

It still runs pretty slow, ill try to optimize it even further to see if I can get a good speed

Oh my god I just found why it was running slow, its because I didnt have all of the other receiver pins connected to arduino because I was testing just 2 of the receiver inputs.

be aware that pulseIn() has an optional timeout parameter.

https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/

Comparing the return value with the timeout set would indicate a failing link.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.