PWM switch

Hi,

I'm new to the Arduino forum and have a quick question. I have written some code to read the PWM signal from an rc receiver. See below. As expected the data ranges from 900 to 2000, or 0.9ms to 2.0ms. I would like to run a loop in the arduino if, or when the input value changes by say +- 50 (using the range 900-2000). Is this possible?? I have looked on this great site, but have got confused and bogged down by masses of code and info.

Or maybe it would be possible to just run the code I have previously written when a channel from the transmitter is on i.e. landing gear switch. This would therefore produce an output of either 900 or 2000. 0.9/2ms pwm.

Thanks

int pin = 8;  // set this to the pin connected to your receiver ch1
unsigned long duration;
int pin2 = 9;  // set this to the pin connected to your receiver ch2
unsigned long duration2;
int pin3 = 10;  // set this to the pin connected to your receiver ch3
unsigned long duration3;

void setup()
{
  Serial.begin(9600);
  pinMode(pin, INPUT);
    Serial.begin(9600);
  pinMode(pin2, INPUT);
   Serial.begin(9600);
  pinMode(pin3, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
  
  Serial.println(duration);
  delay(100);
  
  duration2 = pulseIn(pin2, HIGH);
  Serial.println(duration2);
  delay(100);
  
  duration3 = pulseIn(pin3, HIGH);
  Serial.println(duration3);
  delay(100);
}

You can save the value of duration, duration2, and duration3, as prevDuration1, prevDuration2, and prevDuration3. Then, when the difference between the current durationN value and the previous durationN value is greater than some value, you can do something.

I would like to run a loop in the arduino

This is one of the things you can do. Keep in mind, though, that whatever you do in this loop will interfere with receiving signals from the RC receiver.

Thanks for your reply pauls. Because I'm very new to this, please could you give me an idea of how to write the code? :-[ I have had a quick look but couldn't find any directly related to it. initially 1 channel would be great, and then I could duplicate it for the other channels if I need to.

Thank you

Robert