I do not inspect your code, but: if you use interrupts, the UNO and the MEGA is different! There is no the same pin assignment.
I do not inspect your code, but: if you use interrupts, the UNO and the MEGA is different! There is no the same pin assignment.