The code below was taken online and was written for the Arduino Uno. I know C++ however I am a beginner with Arduino so I don't understand this compatibility issue or how to fix it. I am very thankful for any help. Here is part of the code - the variable declarations: (I can upload the entire code if that would be more helpful).
ISR(PCINT0_vect) {
current_time = micros();
//Channel 1=========================================
if(PINB & B00000001) { //Is input 8 high?
if(last_channel_1 == 0) { //Input 8 changed from 0 to 1.
last_channel_1 = 1; //Remember current input state.
timer_1 = current_time; //Set timer_1 to current_time.
}
} else if(last_channel_1 == 1) { //Input 8 is not high and changed from 1 to 0.
last_channel_1 = 0; //Remember current input state.
receiver_input[1] = current_time - timer_1; //Channel 1 is current_time - timer_1.
}
//Channel 2=========================================
if(PINB & B00000010 ) { //Is input 9 high?
if(last_channel_2 == 0) { //Input 9 changed from 0 to 1.
last_channel_2 = 1; //Remember current input state.
timer_2 = current_time; //Set timer_2 to current_time.
}
} else if(last_channel_2 == 1) { //Input 9 is not high and changed from 1 to 0.
last_channel_2 = 0; //Remember current input state.
receiver_input[2] = current_time - timer_2; //Channel 2 is current_time - timer_2.
}
//Channel 3=========================================
if(PINB & B00000100 ) { //Is input 10 high?
if(last_channel_3 == 0) { //Input 10 changed from 0 to 1.
last_channel_3 = 1; //Remember current input state.
timer_3 = current_time; //Set timer_3 to current_time.
}
} else if(last_channel_3 == 1) { //Input 10 is not high and changed from 1 to 0.
last_channel_3 = 0; //Remember current input state.
receiver_input[3] = current_time - timer_3; //Channel 3 is current_time - timer_3.
}
//Channel 4=========================================
if(PINB & B00001000 ) { //Is input 11 high?
if(last_channel_4 == 0) { //Input 11 changed from 0 to 1.
last_channel_4 = 1; //Remember current input state.
timer_4 = current_time; //Set timer_4 to current_time.
}
} else if(last_channel_4 == 1) { //Input 11 is not high and changed from 1 to 0.
last_channel_4 = 0; //Remember current input state.
receiver_input[4] = current_time - timer_4; //Channel 4 is current_time - timer_4.
}
}
void set_gyro_registers() {
//Set up the MPU-6050
if(eeprom_data[31] == 1) {
Wire.beginTransmission(gyro_address); //Start communication with the address found during search.
Wire.write(0x6B); //We want to write to the PWR_MGMT_1 register (6B hex)
Wire.write(0x00); //Set the register bits as 00000000 to activate the gyro
Wire.endTransmission(); //End the transmission with the gyro.
Wire.beginTransmission(gyro_address); //Start communication with the address found during search.
Wire.write(0x1B); //We want to write to the GYRO_CONFIG register (1B hex)
Wire.write(0x08); //Set the register bits as 00001000 (500dps full scale)
Wire.endTransmission(); //End the transmission with the gyro
Wire.beginTransmission(gyro_address); //Start communication with the address found during search.
Wire.write(0x1C); //We want to write to the ACCEL_CONFIG register (1A hex)
Wire.write(0x10); //Set the register bits as 00010000 (+/- 8g full scale range)
Wire.endTransmission(); //End the transmission with the gyro
//Let's perform a random register check to see if the values are written correct
Wire.beginTransmission(gyro_address); //Start communication with the address found during search
Wire.write(0x1B); //Start reading @ register 0x1B
Wire.endTransmission(); //End the transmission
Wire.requestFrom(gyro_address, 1); //Request 1 bytes from the gyro
while(Wire.available() < 1); //Wait until the 6 bytes are received
if(Wire.read() != 0x08) { //Check if the value is 0x08
digitalWrite(12,HIGH); //Turn on the warning led
while(1)delay(10); //Stay in this loop for ever
}
Wire.beginTransmission(gyro_address); //Start communication with the address found during search
Wire.write(0x1A); //We want to write to the CONFIG register (1A hex)
Wire.write(0x03); //Set the register bits as 00000011 (Set Digital Low Pass Filter to ~43Hz)
Wire.endTransmission(); //End the transmission with the gyro
}
-
5Please edit your question and define "doesn't work". I'm sure the code runs just fine on a Mega. Of course, port B maps to different pins on a Mega than an Uno, ie to digital pins 53, 52, 51... instead of to 12, 13, 14... so it won't do at all the same things on the Mega as the Uno. ¶ In general, include a Minimal, Complete, and Verifiable example of code, not just snippets. Or provide a link to the online code you referred to.James Waldby - jwpat7– James Waldby - jwpat702/18/2017 18:23:35Commented Feb 18, 2017 at 18:23
-
Thank you, this is my first time posting. Here is a link to the code.Colin Murphy– Colin Murphy02/18/2017 18:47:12Commented Feb 18, 2017 at 18:47
-
drive.google.com/file/d/0B5tJTKkHim16VDFhdHhNUDVOcTA/…Colin Murphy– Colin Murphy02/18/2017 18:47:18Commented Feb 18, 2017 at 18:47
-
When I upload to the Arduino Mega, it seems like the code just doesn't run, I'm not entirely sure what the issue is. Here is a link to the code that must be run before the above link's code can be uploaded and run.Colin Murphy– Colin Murphy02/18/2017 18:49:30Commented Feb 18, 2017 at 18:49
-
1Break the problem down. Put some serial prints in there, see how far it's getting. If it's really just not running at all, make sure a trivial sketch runs on that device, then find how much you need to strip out of your code to make it basically run. Essentially, as @jwpat7 suggests, create a minimal, complete, verifiable example.Mark Smith– Mark Smith02/18/2017 21:19:31Commented Feb 18, 2017 at 21:19
2 Answers 2
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.
-
Thank you for your response, I will look at the Pinout diagrams for both micro controllers. I apologize for the lack of a preview and understand why you wouldn't want to download the file, how would I show a preview for future reference? Also what are the numbers such as B00000100, are they addresses?Colin Murphy– Colin Murphy02/18/2017 23:02:00Commented Feb 18, 2017 at 23:02
-
@ColinMurphy, see edit 1James Waldby - jwpat7– James Waldby - jwpat702/19/2017 02:12:16Commented Feb 19, 2017 at 2:12
The sketch uses I2C to communicate with the Gyro / Accelerometer. On a Uno these pins are A4 (SDA) and A5 (SCL). On a Mega these pins are 20 (SDA) and 21 (SCL).