Hello! I am in the process of making an RC craft, and have successfully used a Mega 2560, with pins 2 and 3 for interrupts.
The Mega 2560 is capable of 6 external interrupts, which are 0-5 on pins 2, 3, 21, 20, 19, 18 respectively. However I cannot seem to find these last 4 pins. I have pin 2, and 3 working quite well however. Some schematics led me to believe Pin 18 for example is Analog input 4.
Any help? Thanks.. here is my code so far. Throttle and Aileron give numbers but elevator stays at 0 and is not called.. the jumper for the elevator is currently plugged into analog in 4
#include <Servo.h>
volatile unsigned long timer_start;
volatile int last_interrupt_time;
volatile int throttle = 0;
volatile int rudder = 0;
volatile int elevator = 0;
volatile int aileron = 0;
volatile int mode = 0;
const int aileronCH = 0; //pin 2
const int throttleCH = 1; //pin 3
const int elevatorCH = 5; //pin 18
const int rudderCH = 4; //pin 19
const int modeCH = 3; // 20
const int motorOnePIN = 8;
Servo motorOne;
void setup(){
Serial.begin(9600);
attachInterrupt(throttleCH, calcThrottle, CHANGE);
attachInterrupt(aileronCH, calcAileron, CHANGE);
attachInterrupt(elevatorCH, calcElevator, CHANGE);
//attachInterrupt(rudderCH, calcRudder, CHANGE);
//attachInterrupt(modeCH, calcMode, CHANGE);
motorOne.attach(motorOnePIN);
}
void loop(){
Serial.println("----");
Serial.println(throttle);
Serial.println(aileron);
Serial.println(elevator);
delay(200);
}
void calcThrottle()
{
last_interrupt_time = micros();
if(digitalRead(3) == HIGH){
timer_start = micros();
}else{
if(timer_start > 0){
throttle = ((volatile int)micros() - timer_start);
timer_start = 0;
}
}
}
void calcAileron()
{
last_interrupt_time = micros();
if(digitalRead(2) == HIGH){
timer_start = micros();
}else{
if(timer_start > 0){
aileron = ((volatile int)micros() - timer_start);
timer_start = 0;
}
}
}
void calcElevator()
{
last_interrupt_time = micros();
if(digitalRead(18) == HIGH){
timer_start = micros();
}else{
if(timer_start > 0){
elevator = ((volatile int)micros() - timer_start);
timer_start = 0;
}
}
}