Unintentional delay with Arduino

Hi! I've a setup with an arduino mega, some servos and motors and a FS-I6 RC receiver + transmitter. The program with just the motors works perfect, but once I add the conditionals for controlling the servos. The Arduino starts to work with 1s of delay, so when I press something it makes it one second later, any suggestion? I'm I struggling the Arduino or something?

Maybe post your code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

#include <AFMotor.h>
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
  #include <Adafruit_SSD1306.h>

#define channel1 32
#define channel2 26
#define channel3 43
#define channel5 50
#define channel6 24


Servo serbandera;
Servo serviento;

Adafruit_SSD1306 oled(128, 64, &Wire, 1);

AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
AF_DCMotor Motor3(3);
AF_DCMotor Motor4(4);

int intbandera = 0;
int val;
int viento;

int intvelocidad;
int velocidad;
int velocidad_ent;
int velneg;

int intgiro;
int giro;
int giro_ent;
int gironeg;

int puntuacion;

void setup()
{
  Serial.begin(115200);   // remove comment from this line if you change the Serial port in the next line
  pinMode (channel1, INPUT);// initialises the channels
  pinMode (channel2, INPUT);
  pinMode (channel3, INPUT);
  pinMode (channel5, INPUT);// iBUS connected to Serial0 - change to Serial1 or Serial2 port when required
  pinMode (channel6, INPUT);

  serbandera.attach(40);
  // attaches the servo on pin 9 to the servo object
  Motor1.run(RELEASE);
  Motor2.run(RELEASE);
  Motor3.run(RELEASE);
  Motor4.run(RELEASE);

  Wire.begin();          // inicializa bus I2C
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  Serial.println("Start IBus2PWM");
}


void loop()
{
  intvelocidad = pulseIn (channel2, HIGH);
  intgiro = pulseIn (channel1, HIGH);
  viento = pulseIn (channel5, HIGH);
  val = pulseIn (channel6, HIGH);
  puntuacion = pulseIn (channel3, HIGH);
  velocidad_ent = map(intvelocidad, 1000, 2000, 0, 510);
  velneg = (velocidad_ent - 255);
  velocidad = abs(velneg);

  giro_ent = map(intgiro, 1000, 2000, 0, 510);
  gironeg = (giro_ent - 255);
  giro = abs(gironeg);


  if (velocidad <= 15)
  {
    velocidad = 0;
  }

  if (giro <= 15)
  {
    giro = 0;
  }

  Motor1.setSpeed(velocidad);
  Motor2.setSpeed(velocidad);
  Motor3.setSpeed(velocidad);
  Motor4.setSpeed(velocidad);
  if ((velneg >= 10) && (gironeg >= -35) && (gironeg <= 35))
  {
    Motor1.run(FORWARD);
    Motor2.run(FORWARD);
    Motor3.run(FORWARD);
    Motor4.run(FORWARD);
  }
  else if ((velneg <= -10) && (gironeg >= -35) && (gironeg <= 35))
  {
    Motor1.run(BACKWARD);
    Motor2.run(BACKWARD);
    Motor3.run(BACKWARD);
    Motor4.run(BACKWARD);
  }
  else  if ((gironeg <= -35) && (velneg >= 2))
  {
    Motor1.setSpeed(255);
    Motor2.setSpeed(255);
    Motor3.setSpeed(255);
    Motor4.setSpeed(255);
    Motor1.run(BACKWARD);
    Motor2.run(FORWARD);
    Motor3.run(BACKWARD);
    Motor4.run(FORWARD);
  }
  else if ((gironeg >= 35) && (velneg >= 2))
  {
    Motor1.setSpeed(255);
    Motor2.setSpeed(255);
    Motor3.setSpeed(255);
    Motor4.setSpeed(255);
    Motor1.run(FORWARD);
    Motor2.run(BACKWARD);
    Motor3.run(FORWARD);
    Motor4.run(BACKWARD);
  }
  else  if ((gironeg <= -35) && (velneg <= -2))
  {
    Motor1.setSpeed(255);
    Motor2.setSpeed(255);
    Motor3.setSpeed(255);
    Motor4.setSpeed(255);
    Motor1.run(FORWARD);
    Motor2.run(BACKWARD);
    Motor3.run(FORWARD);
    Motor4.run(BACKWARD);
  }
  else if ((gironeg >= 35) && (velneg <= -2))
  {

    Motor1.setSpeed(255);
    Motor2.setSpeed(255);
    Motor3.setSpeed(255);
    Motor4.setSpeed(255);
    Motor1.run(BACKWARD);
    Motor2.run(FORWARD);
    Motor3.run(BACKWARD);
    Motor4.run(FORWARD);
  }
  else
  {
    Motor1.run(RELEASE);
    Motor2.run(RELEASE);
    Motor3.run(RELEASE);
    Motor4.run(RELEASE);
  }
}

This is at power-on or after reset? If so, that is normal because of the Arduino's bootloader. There are ways to stop this but you need extra equipment and it is less convenient when you need to change your sketch.

Or is the delay present at all times?

pulseIn() is a blocking function. Your 5 pulsein() may take a long time, depending on the type of pulses at your inputs.

You might try adding some Serial.print lines in critical points to determine where the delays are occurring.

You may want to look at the pulseIn timeout…

If you knock it down to say 100mS from the default, the world may run a little faster.

They should not take a long time with an RC receiver. I suspect that one of the channels is not receiving a pulse at all, and the pulseIn() function is timing out after 1s.

The delay is present all the time, I think it has something to do with the pulseIn()s, if it doesn't recognize a value change it waits one second, so I need another way to read te pulseins

If I understand correctly how RC receivers work, that is not true. Even if the value is not changing, the pulses should still be received, many times per second. One of your channels is not receiving any pulses, I suspect. Check your wiring!

A normal RC input should work with a timeout of about 20ms, and have a legal duration of about 1 to 2ms. Implementing that timeout should help a lot.

Simultaneously measuring 6 ~2ms pulses without blocking would be “an interesting challenge”

Most often, a radio channel will have each "channel" sequencually in the data stream, rather than on separate signals...

Didn't you try the 20 ms timeout last night and found it didn't work for you?

-jim lee