Does code written for UNO translate directly to code writted for MEGA?

The subject says it all. I just want to know if there are any differences. Thanks!

More or less.
If things like Direct Port Manipulation was used, you may have to adjust some pin usage as the 328P ports aren't a 1:1 match with Mega ports. PWM pins on Uno may not be PWM pins on the Mega, things like that.

CrossRoads:
More or less.
If things like Direct Port Manipulation was used, you may have to adjust some pin usage as the 328P ports aren't a 1:1 match with Mega ports. PWM pins on Uno may not be PWM pins on the Mega, things like that.

I see. I asked because I am using just some digital output pins and interrupt pins to control motor escs. Would this be a problem is the code was originally written for Uno and then transferred to Mega?

Interrupt might be different. Post your code for a critique.

#include <Wire.h> //Include the Wire.h library so we can communicate with the gyro

//Declaring variables
int cal_int;
unsigned long UL_timer;
double gyro_pitch, gyro_roll, gyro_yaw;
double gyro_roll_cal, gyro_pitch_cal, gyro_yaw_cal;
byte highByte, lowByte;

//Setup routine
void setup(){
  Wire.begin();                                      //Start the I2C as master
  Serial.begin(9600);                                //Start the serial connetion @ 9600bps

  //The gyro is disabled by default and needs to be started
  Wire.beginTransmission(105);                       //Start communication with the gyro (adress 1101001)
  Wire.write(0x20);                                  //We want to write to register 20
  Wire.write(0x0F);                                  //Set the register bits as 00001111 (Turn on the gyro and enable all axis)
  Wire.endTransmission();                            //End the transmission with the gyro
  Wire.beginTransmission(105);                       //Start communication with the gyro (adress 1101001)
  Wire.write(0x23);                                  //We want to write to register 23
  Wire.write(0x80);                                  //Set the register bits as 10000000 (Block Data Update active)
  Wire.endTransmission();                            //End the transmission with the gyro


  delay(250);                                        //Give the gyro time to start
  
  //Let's take multiple samples so we can determine the average gyro offset
  Serial.print("Starting calibration...");           //Print message
  for (cal_int = 0; cal_int < 2000 ; cal_int ++){    //Take 2000 readings for calibration
    gyro_signalen();                                 //Read the gyro output
    gyro_roll_cal += gyro_roll;                      //Ad roll value to gyro_roll_cal
    gyro_pitch_cal += gyro_pitch;                    //Ad pitch value to gyro_pitch_cal
    gyro_yaw_cal += gyro_yaw;                        //Ad yaw value to gyro_yaw_cal
    if(cal_int%100 == 0)Serial.print(".");           //Print a dot every 100 readings
    delay(4);                                        //Wait 4 milliseconds before the next loop
  }
  //Now that we have 2000 measures, we need to devide by 2000 to get the average gyro offset
  Serial.println(" done!");                          //2000 measures are done!
  gyro_roll_cal /= 2000;                             //Divide the roll total by 2000
  gyro_pitch_cal /= 2000;                            //Divide the pitch total by 2000
  gyro_yaw_cal /= 2000;                              //Divide the yaw total by 2000
  
}
//Main program
void loop(){
  delay(250);                                        //Wait 250 microseconds for every loop  
  gyro_signalen();                                   //Read the gyro signals
  print_output();                                    //Print the output
}

void gyro_signalen(){
  Wire.beginTransmission(105);                       //Start communication with the gyro (adress 1101001)
  Wire.write(168);                                   //Start reading @ register 28h and auto increment with every read
  Wire.endTransmission();                            //End the transmission
  Wire.requestFrom(105, 6);                          //Request 6 bytes from the gyro
  while(Wire.available() < 6);                       //Wait until the 6 bytes are received
  lowByte = Wire.read();                             //First received byte is the low part of the angular data
  highByte = Wire.read();                            //Second received byte is the high part of the angular data
  gyro_roll = ((highByte<<8)|lowByte);               //Multiply highByte by 256 and ad lowByte
  if(cal_int == 2000)gyro_roll -= gyro_roll_cal;     //Only compensate after the calibration
  lowByte = Wire.read();                             //First received byte is the low part of the angular data
  highByte = Wire.read();                            //Second received byte is the high part of the angular data
  gyro_pitch = ((highByte<<8)|lowByte);              //Multiply highByte by 256 and ad lowByte
  gyro_pitch *= -1;                                  //Invert axis
  if(cal_int == 2000)gyro_pitch -= gyro_pitch_cal;   //Only compensate after the calibration
  lowByte = Wire.read();                             //First received byte is the low part of the angular data
  highByte = Wire.read();                            //Second received byte is the high part of the angular data
  gyro_yaw = ((highByte<<8)|lowByte);                //Multiply highByte by 256 and ad lowByte
  gyro_yaw *= -1;                                    //Invert axis
  if(cal_int == 2000)gyro_yaw -= gyro_yaw_cal;       //Only compensate after the calibration
}

void print_output(){
  Serial.print("Pitch:");
  if(gyro_pitch >= 0)Serial.print("+");
  Serial.print(gyro_pitch/57.14286,0);               //Convert to degree per second
  if(gyro_pitch/57.14286 - 2 > 0)Serial.print(" NoU");
  else if(gyro_pitch/57.14286 + 2 < 0)Serial.print(" NoD");
  else Serial.print(" ---");
  Serial.print("  Roll:");
  if(gyro_roll >= 0)Serial.print("+");
  Serial.print(gyro_roll/57.14286,0);                //Convert to degree per second
  if(gyro_roll/57.14286 - 2 > 0)Serial.print(" RwD");
  else if(gyro_roll/57.14286 + 2 < 0)Serial.print(" RwU");
  else Serial.print(" ---");
  Serial.print("  Yaw:");
  if(gyro_yaw >= 0)Serial.print("+");
  Serial.print(gyro_yaw/57.14286,0);                 //Convert to degree per second
  if(gyro_yaw/57.14286 - 2 > 0)Serial.println(" NoR");
  else if(gyro_yaw/57.14286 + 2 < 0)Serial.println(" NoL");
  else Serial.println(" ---");
}

cnburke:

(snip)

Did you post the sketch you meant to?

Earlier you said you used digital outputs and interrupts; the sketch you posted uses only I2C and serial, and only uses interrupts insofar as they are used under the covers to make I2C and serial work.

Oh, and delay() is in milliseconds - your comment suggests you think it's in microseconds (there's a delayMicroseconds() for that).

Crap I did post the wrong one. This one is the correct one. I had to attach as a file because it is very long.

YMFC-3D_Flight_controller.ino (24.9 KB)

All the places where you work with PORTx, PINx or DDRx, you'll need to review, since the ports don't always correspond exactly to the same port on the Mega (in fact, they're often totally different). So you will likely need to use either different pin numbers (so they match the code) , or modify the code (so it matches the pins you want to use).
You do this in a lot of places, but once you pick which pins to use on the Mega, you're just making the same changes in a bunch of places.

The places where you write to PCICR and PCMSK - again, same deal, the PCINT 0~3 are on different pins, see the pinout diagram. In this case, you may want to use a different set of PCINTs, since PCINT0~3 are on the SPI pins. PCINT 4,5,6,7 would be good, and they're all right next to eachother, and are in the 0'th set of PCINTs, so the rest of the interrupt-related code would be unchanged (except where you use PORTx/PINx registers in the ISR)

I will try this and see if I can make any headway.