Does analogRead function slow down interrupt function?

Hi all,

I am trying to use interrupt function to copy signal from the input digital pin to the output digital pin of an Arduino. I am coding for my drone collision avoidance system. The input is from the radio receiver and the output is to the PPM encoder.

An infrared-red sensor is used to detect if there is an obstacle in front, should there be an obstacle, the Arduino will not copy the signal from the radio receiver to the PPM encoder, the Arduino will send a pulse to the PPM encoder to pitch up the drone to move it backwards.

The infrared-red sensor is GP2Y0A02YK0F. I got the distance equation from

The sensor uses analogRead function.

Below is the code with distance reading:

int FrontIRSensor = A0;                                    //Infrared-red sensor on the front of the drone, pitch axis
float FrontIRSensorValue;                                  //sensor detection ranges from 20cm to 150cm

const byte Encoder = 12;
const byte Receiver = 2;
int readsensor;

void setup()
{
  pinMode (Encoder, OUTPUT);
  pinMode (Receiver, INPUT);
}

void loop()
{
  digitalWrite (Encoder, HIGH);
  attachInterrupt (digitalPinToInterrupt(Receiver), off, LOW);
  readsensor = analogRead (FrontIRSensor);
  FrontIRSensorValue = 10650.08 * pow (readsensor, -0.935) - 10;

  /*
  if (FrontIRSensorValue <= 50)
  {
    detachInterrupt(digitalPinToInterrupt(Receiver));
    digitalWrite(Encoder, HIGH);
    delayMicroseconds(1400);                  //Pulse width of overwrite pitch output signal = 1.4 ms
    digitalWrite(Encoder, LOW);
    delayMicroseconds(12200);                 //Period of overwrite pitch output signal = 13.6 ms
  }
  */
}


void off ()
{
  digitalWrite (Encoder, LOW);
}

Below is the code without distance reading:

const byte Encoder = 12;
const byte Receiver = 2;

void setup()
{
  pinMode (Encoder, OUTPUT);
  pinMode (Receiver, INPUT);
}

void loop()
{
  digitalWrite (Encoder, HIGH);
  attachInterrupt (digitalPinToInterrupt(Receiver), off, LOW);
}

void off ()
{
  digitalWrite (Encoder, LOW);
}

From the above 2 codes, the drone motors jitter with the distance reading code (first code), the drone motors do not jitter without the distance reading code (second code). I am not sure if the analogRead function is slowing down the interrupt function or did I code wrongly? I need help, please help me. Thanks.

Joe

Does analogRead function slows down interrupt function?

No

Mark

The LOW interrupt fires ALL THE TIME that the pin is low. It is rare to use that mode. Your main code won't get any chance to run. The version without any real code in loop() will be fine because it doesn't have any code.

You probably should use FALLING instead.

Hi,

I tried the FALLING interrupt, but the led lit throughout at digital pin 13 of the Arduino when a square wave of 0.5 Hz is applied at the digital pin 2. I do not know how to make the digital pin 13 copy the waveform of the digital pin 2. Please help me. Thanks.

Here is the code:

const byte ledPin = 13;
const byte interruptPin = 2;

void setup ()
{
  pinMode (ledPin, OUTPUT);
  pinMode (interruptPin, INPUT);
}

void loop ()
{
  digitalWrite (ledPin, HIGH);
  attachInterrupt (digitalPinToInterrupt(interruptPin), off, FALLING);
}

void off ()
{
  digitalWrite (ledPin, LOW);
}
const byte ledPin = 13;
const byte interruptPin = 2;

void setup ()
{
  pinMode (ledPin, OUTPUT);
  pinMode (interruptPin, INPUT);
}

void loop ()
{
  digitalWrite (ledPin, digitalRead(interruptPin));
}

Hi,

analogRead function may not slow down interrupt function.

The GP2Y0A02YK0F infrared sensor can measure 20 cm to 150 cm. However, below 20 cm or above 150 cm, the sensor signal voltage may overlap with the voltage within 20 cm and 150 cm. This causes ambiguity in the voltage reading. The Arduino will detect wrong distance measurement when the sensor detects obstacle below 20 cm or above 150 cm.

For my collision avoidance system in drone application, the motors jitter when it senses obstacles above 150 cm probably due to distance above 150 cm being ambiguous with distance within 20 cm and 150 cm. The Arduino therefore detects the wrong distance and assumes that there is an obstacle in front and tries to overwrite the pitch signal of the drone, causing the drone motors to jitter.