For loop preventing interrupt reading of PWM pins 18 & 19, Mega 2560.

Hi,

I've been working on some code for a custom flight controller for my drone and I've been reading the PWM values from a receiver using interrupts. However it randomly stopped working and I've found the issue to be within a for loop. But I cannot see any reason for this to be preventing the interrupt from working properly since it has not been affecting digital pins 2 & 3 and only digital pins 18 & 19.
I'm currently suspecting this to be an issue in my code.

If anyone can work out why this may be the case that would be great!

Thanks for your help in advance.

Please Note: PWM.hpp & PWM.cpp both need to be placed in the same folder as the code below. GitHub - xkam1x/Arduino-PWM-Reader: Library for reading PWM signal using interrupts.

#include "PWM.hpp"

PWM ch1, ch2, ch3, ch4;

int radio1_checker[40], radio2_checker[40], radio3_checker[40], radio4_checker[40];
int radio_checker_average;

void setup() {
  Serial.begin(9600); // Serial for debug
  ch1.begin(2, 1); // ch1 on pin 2 reading PWM HIGH duration
  ch2.begin(3, 1); // ch2 on pin 3 reading PWM HIGH duration
  ch3.begin(18, 1); // ch3 on pin 18 reading PWM HIGH duration
  ch4.begin(19, 1); // ch4 on pin 19 reading PWM HIGH duration
}

void loop() {
  //RADIO
  int radio1 = ch1.getValue();
  int radio2 = ch2.getValue();
  int radio3 = ch3.getValue();
  int radio4 = ch4.getValue();
  Serial.print(radio1);
  Serial.print("\t");
  Serial.print(radio2);
  Serial.print("\t");
  Serial.print(radio3);
  Serial.print("\t");
  Serial.println(radio4);
  
  radio1_checker[0] = radio1;
  radio2_checker[0] = radio2;
  radio3_checker[0] = radio3;
  radio4_checker[0] = radio4;
  radio_checker_average = 0;
  for(int i=40; i >= 1; i--){
    radio1_checker[i] = radio1_checker[i-1];
  }/*
  for (int i=40; i >= 1; i--){
    if(radio1_checker[i] == radio1_checker[i-1]){
      radio_checker_average++;
    }
  }
  for (int i=40; i >= 1; i--){
    radio2_checker[i] = radio2_checker[i-1];
  }
  for (int i=40; i >= 1; i--){
    if(radio2_checker[i] == radio2_checker[i-1]){
      radio_checker_average++;
    }
  }
  for (int i=40; i >= 1; i--){
    radio3_checker[i] = radio3_checker[i-1];
  }
  for (int i=40; i >= 1; i--){
    if(radio3_checker[i] == radio3_checker[i-1]){
      radio_checker_average++;
    }
  }
  for (int i=40; i >= 1; i--){
    radio4_checker[i] = radio4_checker[i-1];
  }
  for (int i=40; i >= 1; i--){
    if(radio4_checker[i] == radio4_checker[i-1]){
      radio_checker_average++;
    }
  }*/
  
  delay(100);
}

If the array bound is 40, your for loop should start at 39 because the array subscripts run from 0 to 39 for a 40 element array. You risk overwriting something.
Also, if you are shifting everything towards the top of the array, one element at a time, you should slot element 0 in after the shift, not before it, otherwise you always lose the last element written.

6v6gt:
If the array bound is 40, your for loop should start at 39 because the array subscripts run from 0 to 39 for a 40 element array. You risk overwriting something.
Also, if you are shifting everything towards the top of the array, one element at a time, you should slot element 0 in after the shift, not before it, otherwise you always lose the last element written.

That seems to have fixed the issue.

Thanks for your help!