As no preview is available at the given links (for YMFC-AL_setup.ino and YMFC-AL_Flight_controller.ino at drive.google.com) and I don't want to download the code to my system, I haven't looked at the whole program; however, from the code included in the question, it's clear that it uses direct IO rather than Arduino API IO.
Direct IO expressions like PINB & B00000001
[see Edit 1, Note 1] affect differently-numbered digital pins on the Mega than on the Uno or Nano. (Or, more generally, on boards based on ATmega256x vs ATmega328x or 168x vs ATmega32U4 or 16U4.)
On the Arduino Nano and Uno, PB0–PB5 are assigned to Arduino digital pins 8–13.
On the Arduino Micro with 32U4, PB0–PB3 are not assigned to Arduino digital pins; they connect to SS, SCK, MOSI, MISO, while PB4–PB7 are assigned to Arduino digital pins 8–11.
On the Arduino Mega2560 with ATmega2560, PB0–PB3 are assigned to Arduino digital pins 53, 52, 51, 50 respectively, and PB4–PB7 are assigned to 10–13.
Summary: To adapt direct IO code written for an Uno to a Mega, you may need to change ports and pins numbers. Just moving wires around to corresponding pins may or may not fix problems; if the code uses a mix of direct IO and API IO (with digitalWrite()
etc.) the code may need to be changed as well. For example, in setup()
it might use API pin identifiers, and in ISRs might use direct IO, an approach that will malfunction when transferring code to a differently-mapped board.
Note, see numerous excellent pinout diagrams at pighixxx.com.
Edit 1:
Note 1: In a file called binary.h
, the Arduino IDE's programming environment defines a set of 510 constants whose names consist of a B
followed by from 1 to 8 zeroes and ones, with values equal to corresponding binary numbers. The value of the expression PINB & B00000001
is equal to the low-order bit (PB0) of input register PINB. The value of PINB & B00001000
is 8 (ie, 1<<3) if PB3 is high, and is 0 if PB3 is low. Typically, these binary constants are used as bit masks for bit operations (AND &, OR |, XOR ^).
Note 2: When including code in a stackexchange question or answer, first paste the code into the question or answer editing box; then in that editing box, highlight the code and press ctrl-k. (Alternately, highlight the code and click the {}
icon in the toolbar at the top of the editing box.)
Rather large sections of code can be pasted into questions and answers. Generally, paste in at least enough to be compilable and exhibit the problem. If any variable or procedure definitions, #include
lines, etc., are left out, people may have to play guessing games to answer the question, and that's generally undesirable as it wastes time and does not elicit good answers.
As no preview is available at the given links (for YMFC-AL_setup.ino and YMFC-AL_Flight_controller.ino at drive.google.com) and I don't want to download the code to my system, I haven't looked at the whole program; however, from the code included in the question, it's clear that it uses direct IO rather than Arduino API IO.
Direct IO expressions like PINB & B00000001
affect differently-numbered digital pins on the Mega than on the Uno or Nano. (Or, more generally, on boards based on ATmega256x vs ATmega328x or 168x vs ATmega32U4 or 16U4.)
On the Arduino Nano and Uno, PB0–PB5 are assigned to Arduino digital pins 8–13.
On the Arduino Micro with 32U4, PB0–PB3 are not assigned to Arduino digital pins; they connect to SS, SCK, MOSI, MISO, while PB4–PB7 are assigned to Arduino digital pins 8–11.
On the Arduino Mega2560 with ATmega2560, PB0–PB3 are assigned to Arduino digital pins 53, 52, 51, 50 respectively, and PB4–PB7 are assigned to 10–13.
Summary: To adapt direct IO code written for an Uno to a Mega, you may need to change ports and pins numbers. Just moving wires around to corresponding pins may or may not fix problems; if the code uses a mix of direct IO and API IO (with digitalWrite()
etc.) the code may need to be changed as well. For example, in setup()
it might use API pin identifiers, and in ISRs might use direct IO, an approach that will malfunction when transferring code to a differently-mapped board.
Note, see numerous excellent pinout diagrams at pighixxx.com.
As no preview is available at the given links (for YMFC-AL_setup.ino and YMFC-AL_Flight_controller.ino at drive.google.com) and I don't want to download the code to my system, I haven't looked at the whole program; however, from the code included in the question, it's clear that it uses direct IO rather than Arduino API IO.
Direct IO expressions like PINB & B00000001
[see Edit 1, Note 1] affect differently-numbered digital pins on the Mega than on the Uno or Nano. (Or, more generally, on boards based on ATmega256x vs ATmega328x or 168x vs ATmega32U4 or 16U4.)
On the Arduino Nano and Uno, PB0–PB5 are assigned to Arduino digital pins 8–13.
On the Arduino Micro with 32U4, PB0–PB3 are not assigned to Arduino digital pins; they connect to SS, SCK, MOSI, MISO, while PB4–PB7 are assigned to Arduino digital pins 8–11.
On the Arduino Mega2560 with ATmega2560, PB0–PB3 are assigned to Arduino digital pins 53, 52, 51, 50 respectively, and PB4–PB7 are assigned to 10–13.
Summary: To adapt direct IO code written for an Uno to a Mega, you may need to change ports and pins numbers. Just moving wires around to corresponding pins may or may not fix problems; if the code uses a mix of direct IO and API IO (with digitalWrite()
etc.) the code may need to be changed as well. For example, in setup()
it might use API pin identifiers, and in ISRs might use direct IO, an approach that will malfunction when transferring code to a differently-mapped board.
Note, see numerous excellent pinout diagrams at pighixxx.com.
Edit 1:
Note 1: In a file called binary.h
, the Arduino IDE's programming environment defines a set of 510 constants whose names consist of a B
followed by from 1 to 8 zeroes and ones, with values equal to corresponding binary numbers. The value of the expression PINB & B00000001
is equal to the low-order bit (PB0) of input register PINB. The value of PINB & B00001000
is 8 (ie, 1<<3) if PB3 is high, and is 0 if PB3 is low. Typically, these binary constants are used as bit masks for bit operations (AND &, OR |, XOR ^).
Note 2: When including code in a stackexchange question or answer, first paste the code into the question or answer editing box; then in that editing box, highlight the code and press ctrl-k. (Alternately, highlight the code and click the {}
icon in the toolbar at the top of the editing box.)
Rather large sections of code can be pasted into questions and answers. Generally, paste in at least enough to be compilable and exhibit the problem. If any variable or procedure definitions, #include
lines, etc., are left out, people may have to play guessing games to answer the question, and that's generally undesirable as it wastes time and does not elicit good answers.
As no preview is available at the given links (for YMFC-AL_setup.ino and YMFC-AL_Flight_controller.ino at drive.google.com) and I don't want to download the code to my system, I haven't looked at the whole program; however, from the code included in the question, it's clear that it uses direct IO rather than Arduino API IO.
Direct IO expressions like PINB & B00000001
affect differently-numbered digital pins on the Mega than on the Uno or Nano. (Or, more generally, on boards based on ATmega256x vs ATmega328x or 168x vs ATmega32U4 or 16U4.)
On the Arduino Nano and Uno, PB0–PB5 are assigned to Arduino digital pins 8–13.
On the Arduino Micro with 32U4, PB0–PB3 are not assigned to Arduino digital pins; they connect to SS, SCK, MOSI, MISO, while PB4–PB7 are assigned to Arduino digital pins 8–11.
On the Arduino Mega2560 with ATmega2560, PB0–PB3 are assigned to Arduino digital pins 53, 52, 51, 50 respectively, and PB4–PB7 are assigned to 10–13.
Summary: To adapt direct IO code written for an Uno to a Mega, you may need to change ports and pins numbers. Just moving wires around to corresponding pins may or may not fix problems; if the code uses a mix of direct IO and API IO (with digitalWrite()
etc.) the code may need to be changed as well. For example, in setup()
it might use API pin identifiers, and in ISRs might use direct IO, an approach that will malfunction when transferring code to a differently-mapped board.
Note, see numerous excellent pinout diagrams at pighixxx.com.