Skip to main content
edited title
Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Why won't this code work on an Arduino Mega 2560, (It works on an Arduino Uno)?

deleted 2520 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

The below code below was taken online and was written for the Arduino uno,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(){
  //Setup 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

   void Wire.beginTransmissionset_gyro_registers(gyro_address);                       {
  //Set up the MPU-6050
  if(eeprom_data[31] == 1) {
    Wire.beginTransmission(gyro_address); //Start communication with the address found during search.
    Wire.write(0x1C0x6B);        //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 ACCEL_CONFIGGYRO_CONFIG register (1A1B hex)
    Wire.write(0x100x08);         //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    

  }  

The below code was taken online and was written for the Arduino uno, I know C++ however 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(){
  //Setup 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    

  }  

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
  }
Bumped by Community user
Bumped by Community user
code formatting
Source Link
KIIV
  • 4.9k
  • 1
  • 14
  • 23

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(){ //Setup 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    

}

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(){
  //Setup 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    

  }  

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(){ //Setup 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    

}

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(){
  //Setup 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    

  }  
Source Link
Loading