Electronic – arduino – AttatchInterrupt Constantly interrupts

arduino

I am a new to electronics. I am trying to setup the radio control for the arduino. I hooked up the Reciever's Channel 1 to the Arduino Mega 2560's Digital pin 2, Reciever Vin to Arduino 5V, and Ground to Ard Ground. Here is my Source Code.

void setup() {
        Serial.begin(9600);
        attachInterrupt(0 , blink, CHANGE);
     }

void loop() {
   Serial.println("I AM IN THE LOOP");        

}


void blink()
{

     Serial.println("I AM IN THE INTERRUPT");  
}

I tried using other interrupt pins,3.3 V instead of 5 and also tried Arduino Uno and differnt kinds of Interrupt Values like RISING, LOW and FALLING but it constantly prints " I AM IN THE INTERRUPT" without me touching the transmitter. Any suggestions.

[UPDATE]
After a great deal of discussion on
http://forum.arduino.cc/index.php?topic=184283.0
I came to this conclusion. Radio generates PWM signal .. If you have an interrupt pin connected to a channel. It will interrupt regardless of what you do because it is constantly generating a PWM. So I am leaning towards interrupt is not a good idea for radio control.

Best Answer

As you've determined that kind of receiver will have a PWM output to control a servo, the Wikipedia Servo Control article has some good information about it. If you wanted to use it anyway the normal neutral position will be 1.5mS so you could use something like the following code that uses the pulseIn function to treat a pulse of 1.75mS or longer as being on:

#define MY_PIN 0

void setup() {
    Serial.begin(9600);
    pinMode(MY_PIN, INPUT);
}

void loop() {
   if (pulseIn(MY_PIN, HIGH) >= 1750)
      Serial.println("TX on");        
}

Note that the typical period between pulses is 20mS so your code will be stalled in pulseIn for around that period. If that's a problem you could go back to using the interrupt and set it for CHANGE and keep track of the time between when the line goes high and low again. It would be something like the following untested code:

#define MY_PIN 0 // Make sure this matches the interrupt line
unsigned long start_micros = 0;
unsigned long last_duration = 0;

void setup() {
    Serial.begin(9600);
    pinMode(MY_PIN, INPUT);
    attachInterrupt(0 , PWM_handler, CHANGE);
}

void loop() {
   if (last_duration >= 1750)
      Serial.println("TX on");        
}

void PWM_hander()
{
    if (digitalRead(MY_PIN) == HIGH)
        start_micros = micros();
    else
        last_duration = micros() - start_micros;
}