Arduino Micro Interrupts Problems

Hi,
I’m currently working on writing codes on Arduino micro for Futaba Radio Controller to navigate a Quadcopter. I’m having trouble using Interrupts for my program.
For throttle,i set channel 3 of receiver for it.Pin 2 is the input pin and pin 3 is the output pin which is connects to naza.Using the code below,I can’t detect a changing pulse width at Pin 3 cor-response to Pin2 when input pin pin 2 change.It means when pulse width of pin2 =1.5millicrosecond, there has no pulse width of 1.5millisecond at pin3.
Anyone can help me???Pleaseeeeeee.Thanks.

code:
#include <PinChangeInt.h>
#include <Servo.h>
Servo servoThrottle;
uint32_t rcThrottle;
uint32_t rcThrottleStart;
void setup() {

** rcThrottle = 0;**
** rcThrottleStart = 0;**

** servoThrottle.attach( 3 );**
** PCintPort::attachInterrupt(2, calcThrottle, CHANGE );**
** Serial.begin( 9600 );**
}
void loop() {
** Serial.println( rcThrottle );**
** servoThrottle.write(rcThrottle);**
}
void calcThrottle()
{
** // if the pin is high, its a rising edge of the signal pulse, so lets record its value**
** if( digitalRead( 2 ) == HIGH ) {**
** rcThrottleStart = micros();**
** } else {**
** // else it must be a falling edge, so lets get the time and subtract the time of the rising edge**
** // this gives use the time between the rising and falling edges i.e. the pulse duration.**
** if( rcThrottleStart > 0 ) {**
** rcThrottle =( uint32_t )( micros() - rcThrottleStart );**
** rcThrottleStart = 0;**
** }**
** }**
}

... when input pin pin 2 change.It means when pulse width of pin2 =1.5millicrosecond ...

You lost me at "millicrosecond".

Please use code tags.

pin 3 is the output pin which is connects to naza.

What is "naza"?

opps :o i meant milliseconds[quote author=Nick Gammon link=msg=2157815 date=1427352955] You lost me at "millicrosecond".

Please use code tags. [/quote]

[quote author=Nick Gammon link=msg=2157817 date=1427353046] What is "naza"? [/quote] NAZA is a flight controller

Any variables changed by the ISR should be declared volatile.

I am intrigued as to what you are actually trying to do that the Naza can't do on its own. What are you going to use the servo for ?

UKHeliBob: Any variables changed by the ISR should be declared volatile.

I am intrigued as to what you are actually trying to do that the Naza can't do on its own. What are you going to use the servo for ?

The NAZA will be connected to the quadcopter to operate it.what i'm doing now is to write a complete code which will be programmed in arduino micro. As a result,I can control the quadcopter by Futaba radio control sysytem.

The NAZA will be connected to the quadcopter to operate it.what i’m doing now is to write a complete code which will be programmed in arduino micro. As a result,I can control the quadcopter by Futaba radio control sysytem.

Sorry but I don’t get it. Surely you connect the receiver outputs to the Naza inputs and the Naza outputs to the ESCs. You control the quadcopter using the radio and the Naza provides stabilisation and other functions if you activate them.

Where does the Arduino come into it ? Are you aiming to replace the Naza ?

If you are worried about delays in passing on the signal, and there will be one, why not connect the Arduino directly to the receiver output ?

UKHeliBob:
Sorry but I don’t get it. Surely you connect the receiver outputs to the Naza inputs and the Naza outputs to the ESCs. You control the quadcopter using the radio and the Naza provides stabilisation and other functions if you activate them.

Where does the Arduino come into it ? Are you aiming to replace the Naza ?

If you are worried about delays in passing on the signal, and there will be one, why not connect the Arduino directly to the receiver output ?

I write some code for the arduino micro ,so the quadcopter can switch to auto or manual mode .Not to replace the Naza but connect the arduino to naza.

i changed my code ,now I can get the same pulse width but different duty cycle ,it means the output and input has different period .since the output period is longer than input period ,can I consider the difference as delay???

the data for input:pulse width-1.91ms,duty cycle-12%,period-16ms

the data for output:pulse width-1.91ms,duty cycle-9%,period-20ms

The code is :

#include <PinChangeInt.h>

#define CHANNEL_3_PIN 2,3
#include<Servo.h>
volatile unsigned long timer_start;
volatile int pulse_time;
volatile int last_interrupt_time;

Servo servoThrottle;

void calcSignal()
{
last_interrupt_time = micros();
if(digitalRead(2) == HIGH)
{
timer_start = micros();
}
else
{
if (timer_start > 0)
{
pulse_time = ((volatile int)micros()-timer_start);

}
}
}

void setup()
{
timer_start = 0;
servoThrottle.attach( 3 );
PCintPort:attachInterrupt(1,calcSignal,CHANGE);
Serial.begin(115200);
}

void loop()
{
servoThrottle.write(pulse_time);
Serial.println(pulse_time);

}

[quote author=Nick Gammon link=msg=2157815 date=1427352955] Please use code tags. [/quote]

Please use code tags. Do you understand what I am saying?

You've done two more posts, without code tags, after I asked you to use them. Would it help if I just deleted your account? Would that help you to read the instructions?

http://forum.arduino.cc/index.php/topic,148850.0.html

UKHeliBob:
Any variables changed by the ISR should be declared volatile.

I am intrigued as to what you are actually trying to do that the Naza can’t do on its own. What are you going to use the servo for ?

I try add one more channel ,when the code is like this ,channel 3 can work,channel 1 can’t.The funny part i don’t understand is

PCintPort:attachInterrupt(1,calcThottle,CHANGE);

servoRoll.attach( 5 );
PCintPort::attachInterrupt(1,calcRoll,CHANGE);

This one,I can compile successfully when I only put “:” for first interrupt but put “::” for second interrupt,only in this case,i can get desired output for channel 3.

#include <PinChangeInt.h>

#include <PinChangeInt.h>

#define CHANNEL_3_PIN 2,4
#define CHANNEL_1_PIN 3,5

#include<Servo.h>
volatile unsigned long throttle_timer_start,roll_timer_start;
volatile int throttle_pulse_time,roll_pulse_time;
volatile int throttle_last_interrupt_time,roll_last_interrupt_time;

Servo servoThrottle,servoRoll;

void calcThottle()
{
throttle_last_interrupt_time = micros();
if(digitalRead(2) == HIGH)
{
throttle_timer_start = micros();
}
else
{
if (throttle_timer_start > 0)
{
throttle_pulse_time = ((volatile int)micros()-throttle_timer_start);

}
}
}

void calcRoll()
{
roll_last_interrupt_time = micros();
if(digitalRead(3) == HIGH)
{
roll_timer_start = micros();
}
else
{
if (roll_timer_start > 0)
{
roll_pulse_time = ((volatile int)micros()-roll_timer_start);

}
}
}

void setup()
{
throttle_timer_start = 0;
roll_timer_start = 0;
servoThrottle.attach( 4 );
PCintPort:attachInterrupt(1,calcThottle,CHANGE);

servoRoll.attach( 5 );
PCintPort::attachInterrupt(1,calcRoll,CHANGE);
Serial.begin(115200);
}

void loop()
{
servoThrottle.write(throttle_pulse_time);
servoRoll.write(roll_pulse_time);
Serial.println(throttle_pulse_time);
Serial.println(roll_pulse_time);

}

I write some code for the arduino micro ,so the quadcopter can switch to auto or manual mode .Not to replace the Naza but connect the arduino to naza.

You can put the Naza into manual mode with all stabilisation turned off in the same way that you can turn GPS mode, Headlock, Return to Home etc on and off. You do this by connecting the Naza between the receiver and the ESCs as I suggested and using spare channels to change modes. No need for the Arduino.

UKHeliBob: You can put the Naza into manual mode with all stabilisation turned off in the same way that you can turn GPS mode, Headlock, Return to Home etc on and off. You do this by connecting the Naza between the receiver and the ESCs as I suggested and using spare channels to change modes. No need for the Arduino.

It's my lecturer asked me to use arduino micro,it's compulsory.

What exactly have you been asked to do ? Has the Naza been a red herring throughout this thread ? It sounds now as though your project involves connecting the Arduino between the receiver and ESCs and that the Naza is not present. Is that right ?

UKHeliBob: What exactly have you been asked to do ? Has the Naza been a red herring throughout this thread ? It sounds now as though your project involves connecting the Arduino between the receiver and ESCs and that the Naza is not present. Is that right ?

https://mail.google.com/mail/u/0/?ui=2&ik=8361d51b17&view=fimg&th=14c5a52d747a48af&attid=0.1&disp=inline&safe=1&attbid=ANGjdJ-KlloIrL6E2rkTkxV9vRcod_zOnsqkfixZJZ59udhqXqcCaSrFE68eipTOVviUyiT4DEsjQH3EYtCHflyym2hSo6JJcLo6t0sa1pni2mzHreTtCMemIobIDzQ&ats=1427444539580&rm=14c5a52d747a48af&zw&sz=w1010-h461

Another post without code tags. Unbelievable.

navenda: https://mail.google.com/mail/u/0/?ui=2&ik=8361d51b17&view=fimg&th=14c5a52d747a48af&attid=0.1&disp=inline&safe=1&attbid=ANGjdJ-KlloIrL6E2rkTkxV9vRcod_zOnsqkfixZJZ59udhqXqcCaSrFE68eipTOVviUyiT4DEsjQH3EYtCHflyym2hSo6JJcLo6t0sa1pni2mzHreTtCMemIobIDzQ&ats=1427444539580&rm=14c5a52d747a48af&zw&sz=w1010-h461

See if u can understand by this image.

I cannot access that url

Your client does not have permission to get URL

It's a test. Are we passing?