Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //imports Library
- #include <Wire.h>
- //sets LED as pin 13
- int LED = 10;
- //sets addr as 0x76(I2C adress for barometer)
- #define Addr 0x76
- //the variables for the barometer
- unsigned long Coff[6], Ti = 0, offi = 0, sensi = 0;
- unsigned int data[3];
- //the variables for the barometer 2
- unsigned long Coff2[6], Ti2 = 0, offi2 = 0, sensi2 = 0;
- unsigned int data2[3];
- //The variables for the gyroscope
- float elapsedTime, time, timePrev; // these are time
- int gyro_error =0;// used to calculate error
- float Gyro_rawX, Gyro_rawY, Gyro_rawZ; //the raw angle data
- float angleX, angleY;
- float Gyro_raw_errorX, Gyro_raw_errorY;
- float Total_angle_x, Total_angle_y;
- //The variables for the gyroscope 2
- int gyro_error2 =0;// used to calculate error
- float Gyro_rawX2, Gyro_rawY2, Gyro_rawZ2; //the raw angle data2
- float angleX2, angleY2;
- float Gyro_raw_errorX2, Gyro_raw_errorY2;
- float Total_angle_x2, Total_angle_y2;
- //The variables for the acceleromter
- int accel_error=0;
- float rad_to_degree = 180/3.14159264;//If you have been in math class recently you should know this
- float Accel_rawX, Accel_rawY, Accel_rawZ; //The raw accelerometer data
- float Accel_angleX, Accel_angleY; //This is the angle data that you get from the accelerometers data
- float Accel_angle_errorX, Accel_angle_errorY;//This is
- float Total_angleX, Total_angleY;
- float prev_accelX = 1000;
- //The variables for the acceleromter 2
- int accel_error2=0;
- float Accel_rawX2, Accel_rawY2, Accel_rawZ2; //The raw accelerometer data2
- float Accel_angleX2, Accel_angleY2; //This is the angle data2 that you get from the accelerometers data2
- float Accel_angle_errorX2, Accel_angle_errorY2;//This is
- float Total_angleX2, Total_angleY2;
- float prev_accelX2= 1000;
- float height, height2;
- float hi, hi2, hi3, hi4;
- float pressure, pressure2;
- float pressure_at_sealevel = 1012.8; //mbar;
- float change;
- //the variables for the barometer
- unsigned long Coff3[6], Ti3 = 0, offi3 = 0, sensi3 = 0;
- unsigned int data3[3];
- //the variables for the barometer 2
- unsigned long Coff4[6], Ti4 = 0, offi4 = 0, sensi4 = 0;
- unsigned int data4[3];
- //The variables for the gyroscope
- int gyro_error3 =0;// used to calculate error
- float Gyro_rawX3, Gyro_rawY3, Gyro_rawZ3; //the raw angle data
- float angleX3, angleY3;
- float Gyro_raw_errorX3, Gyro_raw_errorY3;
- float Total_angle_x3, Total_angle_y3;
- //The variables for the gyroscope 4
- int gyro_error4 =0;// used to calculate error
- float Gyro_rawX4, Gyro_rawY4, Gyro_rawZ4; //the raw angle data4
- float angleX4, angleY4;
- float Gyro_raw_errorX4, Gyro_raw_errorY4;
- float Total_angle_x4, Total_angle_y4;
- //The variables for the acceleromter
- int accel_error3=0;
- float Accel_rawX3, Accel_rawY3, Accel_rawZ3; //The raw accelerometer data
- float Accel_angleX3, Accel_angleY3; //This is the angle data that you get from the accelerometers data
- float Accel_angle_errorX3, Accel_angle_errorY3;//This is
- float Total_angleX3, Total_angleY3;
- //The variables for the acceleromter 4
- int accel_error4=0;
- float Accel_rawX4, Accel_rawY4, Accel_rawZ4; //The raw accelerometer data4
- float Accel_angleX4, Accel_angleY4; //This is the angle data4 that you get from the accelerometers data4
- float Accel_angle_errorX4, Accel_angle_errorY4;//This is
- float Total_angleX4, Total_angleY4;
- float prev_accelX4= 1000;
- float height3, height4;
- float h3, h4;
- float pressure3, pressure4;
- void setup(){
- //starting up the I2C device
- Wire.begin();
- Wire.beginTransmission(0x69);//This starts the transmission to the I2C device, while most things online will tell you that it's 0x68 it's not
- Wire.write(0x6B);
- Wire.write(0x00);//reset
- Wire.endTransmission(true);//end Transmission
- //Gyroscope configuration
- Wire.beginTransmission(0x69);
- Wire.write(0x1B);
- Wire.write(0x10);//for the gyroscope use 0x10 for the accelerometer use 0x18
- Wire.endTransmission(true);
- //Accelerometer configuration
- Wire.beginTransmission(0x69);
- Wire.write(0x1C);
- Wire.write(0x18);//this is +/- 16g if you want +/- 8g then use 0x10
- Wire.endTransmission(true);
- //starting up the serial monitor
- Serial.begin(9600);//for this we are using a 9600 baud rate
- time = millis();
- //This gets the data for the accelerometer
- if(accel_error==0)
- {
- for(int a=0; a<200; a++)
- {
- Wire.beginTransmission(0x69);
- Wire.write(0x3B); //acceleration in the X-axis
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_angle_errorX = Accel_angle_errorX +((atan((Accel_rawY)/sqrt(pow((Accel_rawX),2)+pow((Accel_rawZ),2)))*rad_to_degree));
- Accel_angle_errorY = Accel_angle_errorY +((atan(-1*(Accel_rawX)/sqrt(pow((Accel_rawY),2)+pow((Accel_rawZ),2)))*rad_to_degree));
- if(a==199)
- {
- Accel_angle_errorX = Accel_angle_errorX/200;
- Accel_angle_errorY = Accel_angle_errorY/200;
- accel_error=1;
- }
- }
- }
- if(accel_error2==0)
- {
- for(int a2=0; a2<200; a2++)
- {
- Wire.beginTransmission(0x69);
- Wire.write(0x3B); //acceleraTi2on in the X-axis
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_angle_errorX2= Accel_angle_errorX2+((atan((Accel_rawY2)/sqrt(pow((Accel_rawX2),2)+pow((Accel_rawZ2),2)))*rad_to_degree));
- Accel_angle_errorY2= Accel_angle_errorY2+((atan(-1*(Accel_rawX2)/sqrt(pow((Accel_rawY2),2)+pow((Accel_rawZ2),2)))*rad_to_degree));
- if(a2==199)
- {
- Accel_angle_errorX2= Accel_angle_errorX2/200;
- Accel_angle_errorY2= Accel_angle_errorY2/200;
- accel_error2=1;
- }
- }
- }
- //This gets the data for the gyroscope
- if(gyro_error==0)
- {
- for(int i=0; i<200; i++)
- {
- Wire.beginTransmission(0x69); //begin, Send the slave adress (in this case 68)
- Wire.write(0x43); //First adress of the Gyro data
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,4,true); //We ask for just 4 registers
- Gyro_rawX=Wire.read()<<8|Wire.read(); //Once again we shif and sum
- Gyro_rawY=Wire.read()<<8|Wire.read();
- /*---X---*/
- Gyro_raw_errorX = Gyro_raw_errorX + (Gyro_rawX/32.8);
- /*---Y---*/
- Gyro_raw_errorY = Gyro_raw_errorY + (Gyro_rawY/32.8);
- if(i==199)
- {
- Gyro_raw_errorX = Gyro_raw_errorX/200;
- Gyro_raw_errorY = Gyro_raw_errorY/200;
- gyro_error=1;
- }
- }
- }
- if(gyro_error2==0)
- {
- for(int i2=0; i2<200; i2++)
- {
- Wire.beginTransmission(0x69); //begin, Send the slave adress (in this case 68)
- Wire.write(0x43); //First adress of the Gyro data2
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,4,true); //We ask for just 4 registers
- Gyro_rawX2=Wire.read()<<8|Wire.read(); //Once again we shif and sum
- Gyro_rawY2=Wire.read()<<8|Wire.read();
- /*---X---*/
- Gyro_raw_errorX2= Gyro_raw_errorX2+ (Gyro_rawX2/32.8);
- /*---Y---*/
- Gyro_raw_errorY2= Gyro_raw_errorY2+ (Gyro_rawY2/32.8);
- if(i2==199)
- {
- Gyro_raw_errorX2= Gyro_raw_errorX2/200;
- Gyro_raw_errorY2= Gyro_raw_errorY2/200;
- gyro_error2=1;
- }
- }
- }
- //This gets the data for the barometer (includes temperature and pressure)
- for(int i = 0; i < 6; i++)
- {
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data register
- Wire.write(0xA2 + (2 * i));
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 2 bytes of data
- Wire.requestFrom(Addr, 2);
- // Read 2 bytes of data
- // Coff msb, Coff lsb
- if(Wire.available() == 2)
- {
- data[0] = Wire.read();
- data[1] = Wire.read();
- }
- // Convert the data
- Coff[i] = ((data[0] * 256) + data[1]);
- }
- //This gets the data2 for the barometer (includes temperature and pressure)
- for(int i = 0; i < 6; i++)
- {
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data2 register
- Wire.write(0xA2 + (2 * i));
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 2 bytes of data2
- Wire.requestFrom(Addr, 2);
- // Read 2 bytes of data2
- // Coff2 msb, Coff2 lsb
- if(Wire.available() == 2)
- {
- data2[0] = Wire.read();
- data2[1] = Wire.read();
- }
- // Convert the data2
- Coff2[i] = ((data2[0] * 256) + data2[1]);
- }
- //This gets the data for the accelerometer
- if(accel_error3==0)
- {
- for(int a3=0; a3<200; a3++)
- {
- Wire.beginTransmission(0x69);
- Wire.write(0x3B); //acceleration in the X-axis
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_angle_errorX3= Accel_angle_errorX3+((atan((Accel_rawY3)/sqrt(pow((Accel_rawX3),2)+pow((Accel_rawZ3),2)))*rad_to_degree));
- Accel_angle_errorY3 = Accel_angle_errorY3 +((atan(-1*(Accel_rawX3)/sqrt(pow((Accel_rawY3),2)+pow((Accel_rawZ3),2)))*rad_to_degree));
- if(a3==199)
- {
- Accel_angle_errorX3= Accel_angle_errorX3/200;
- Accel_angle_errorY3 = Accel_angle_errorY3/200;
- accel_error3=1;
- }
- }
- }
- if(accel_error4==0)
- {
- for(int a4=0; a4<200; a4++)
- {
- Wire.beginTransmission(0x69);
- Wire.write(0x3B); //acceleraTi2on in the X-axis
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_angle_errorX4= Accel_angle_errorX4+((atan((Accel_rawY4)/sqrt(pow((Accel_rawX4),2)+pow((Accel_rawZ4),2)))*rad_to_degree));
- Accel_angle_errorY4= Accel_angle_errorY4+((atan(-1*(Accel_rawX4)/sqrt(pow((Accel_rawY4),2)+pow((Accel_rawZ4),2)))*rad_to_degree));
- if(a4==199)
- {
- Accel_angle_errorX4= Accel_angle_errorX4/200;
- Accel_angle_errorY4= Accel_angle_errorY4/200;
- accel_error4=1;
- }
- }
- }
- //This gets the data for the gyroscope
- if(gyro_error3==0)
- {
- for(int i3=0; i3<200; i3++)
- {
- Wire.beginTransmission(0x69); //begin, Send the slave adress (in this case 68)
- Wire.write(0x43); //First adress of the Gyro data
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,4,true); //We ask for just 4 registers
- Gyro_rawX3=Wire.read()<<8|Wire.read(); //Once again we shif and sum
- Gyro_rawY3=Wire.read()<<8|Wire.read();
- /*---X---*/
- Gyro_raw_errorX3= Gyro_raw_errorX3+ (Gyro_rawX3/32.8);
- /*---Y---*/
- Gyro_raw_errorY3 = Gyro_raw_errorY3 + (Gyro_rawY3/32.8);
- if(i3==199)
- {
- Gyro_raw_errorX3= Gyro_raw_errorX3/200;
- Gyro_raw_errorY3 = Gyro_raw_errorY3/200;
- gyro_error3=1;
- }
- }
- }
- if(gyro_error4==0)
- {
- for(int i4=0; i4<200; i4++)
- {
- Wire.beginTransmission(0x69); //begin, Send the slave adress (in this case 68)
- Wire.write(0x43); //First adress of the Gyro data4
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,4,true); //We ask for just 4 registers
- Gyro_rawX4=Wire.read()<<8|Wire.read(); //Once again we shif and sum
- Gyro_rawY4=Wire.read()<<8|Wire.read();
- /*---X---*/
- Gyro_raw_errorX4= Gyro_raw_errorX4+ (Gyro_rawX4/32.8);
- /*---Y---*/
- Gyro_raw_errorY4= Gyro_raw_errorY4+ (Gyro_rawY4/32.8);
- if(i4==199)
- {
- Gyro_raw_errorX4= Gyro_raw_errorX4/200;
- Gyro_raw_errorY4= Gyro_raw_errorY4/200;
- gyro_error4=1;
- }
- }
- }
- //This gets the data for the barometer (includes temperature and pressure)
- for(int i3 = 0; i3 < 6; i3++)
- {
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data register
- Wire.write(0xA2 + (2 * i3));
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 2 bytes of data
- Wire.requestFrom(Addr, 2);
- // Read 2 bytes of data
- // Coff msb, Coff lsb
- if(Wire.available() == 2)
- {
- data3[0] = Wire.read();
- data3[1] = Wire.read();
- }
- // Convert the data
- Coff3[i3] = ((data3[0] * 256) + data3[1]);
- }
- //This gets the data2 for the barometer (includes temperature and pressure)
- for(int i3 = 0; i3 < 6; i3++)
- {
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data2 register
- Wire.write(0xA2 + (2 * i3));
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 2 bytes of data2
- Wire.requestFrom(Addr, 2);
- // Read 2 bytes of data2
- // Coff2 msb, Coff2 lsb
- if(Wire.available() == 2)
- {
- data4[0] = Wire.read();
- data4[1] = Wire.read();
- }
- // Convert the data2
- Coff4[i3] = ((data4[0] * 256) + data4[1]);
- }
- //beeper just to know if it's working
- pinMode(LED, OUTPUT);
- digitalWrite(LED, HIGH);
- delay(400);
- digitalWrite(LED, LOW);
- delay(400);
- }
- void loop(){
- timePrev =time;
- time= millis();
- elapsedTime = (time - timePrev)/1000;
- //Acceleration
- Wire.beginTransmission(0x69);
- Wire.write(0x3B);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_rawX=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawY=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawZ=(Wire.read()<<8|Wire.read())/2048.0;
- //Gyroscope
- Wire.beginTransmission(0x69);
- Wire.write(0x43);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Gyro_rawX=Wire.read()<<8|Wire.read();
- Gyro_rawY=Wire.read()<<8|Wire.read();
- Gyro_rawX = (Gyro_rawX/32.8) - Gyro_raw_errorX;
- Gyro_rawY = (Gyro_rawY/32.8) - Gyro_raw_errorY;
- angleX = Gyro_rawX * elapsedTime;
- angleY = Gyro_rawX * elapsedTime;
- Total_angle_x = 0.98 *(Total_angle_x + angleX) + 0.02*Accel_angleX;
- Total_angle_y = 0.98 *(Total_angle_y + angleY) + 0.02*Accel_angleY;
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Send reset command
- Wire.write(0x1E);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh pressure with the OSR = 256
- Wire.write(0x40);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data
- // ptemp_msb1, ptemp_msb, ptemp_lsb
- if(Wire.available() == 3)
- {
- data[0] = Wire.read();
- data[1] = Wire.read();
- data[2] = Wire.read();
- }
- // Convert the data
- unsigned long ptemp = ((data[0] * 65536.0) + (data[1] * 256.0) + data[2]);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh temperature with the OSR = 256
- Wire.write(0x50);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data
- // temp_msb1, temp_msb, temp_lsb
- if(Wire.available() == 3)
- {
- data[0] = Wire.read();
- data[1] = Wire.read();
- data[2] = Wire.read();
- }
- // Convert the data
- unsigned long temp = ((data[0] * 65536.0) + (data[1] * 256.0) + data[2]);
- // Pressure and Temperature Calculations
- // 1st order temperature and pressure compensation
- // Difference between actual and reference temperature
- unsigned long dT = temp - ((Coff[4] * 256));
- temp = 2000 + (dT * (Coff[5] / pow(2, 23)));
- // Offset and Sensitivity calculation
- unsigned long long off = Coff[1] * 65536 + (Coff[3] * dT) / 128;
- unsigned long long sens = Coff[0] * 32768 + (Coff[2] * dT) / 256;
- // 2nd order temperature and pressure compensation
- if(temp >= 2000)
- {
- Ti = 0;
- offi = 0;
- sensi = 0;
- }
- else if(temp < 2000)
- {
- Ti = (dT * dT) / (pow(2,31));
- offi = 5 * ((pow((temp - 2000), 2))) / 2;
- sensi = 5 * ((pow((temp - 2000), 2))) / 4;
- if(temp < -1500)
- {
- offi = offi + 7 * ((pow((temp + 1500), 2)));
- sensi = sensi + 11 * ((pow((temp + 1500), 2))) / 2;
- }
- }
- // Adjust temp, off, sens based on 2nd order compensation
- temp -= Ti;
- off -= offi;
- sens -= sensi;
- // Convert the final data
- ptemp = (ptemp * sens / 2097152 - off);
- ptemp /= 32768;
- float pressure = ptemp / 100.0;
- float ctemp = temp / 100.0;
- float fTemp = ctemp * 1.8 + 32.0;
- //This prints out stuff in the serial moniter
- height = ((pow(pressure_at_sealevel/pressure, (1/5.257))-1)*(ctemp+273.15))/0.0065;
- hi = 44330.0f * (1.0f - pow((double)pressure / (double)pressure_at_sealevel, 0.1902949f));
- change = hi2- hi;
- Serial.print("HI: ");
- Serial.println(hi,4);
- //This prints out time
- Serial.print("time: ");
- Serial.println(time/1000,4);
- //This prints out acceleration
- Serial.print("X accel: ");//in the x axis
- Serial.print(Accel_rawX, 4);
- Serial.print(" | ");
- Serial.print("Y accel: ");//in the y axis
- Serial.print(Accel_rawY, 4);
- Serial.print(" | ");
- Serial.print("Z accel: ");//in the z axis
- Serial.println(Accel_rawZ, 4);
- //This prints out rotation
- Serial.print("X rotation");//in the x axis
- Serial.println(angleX,4);
- Serial.print("Y rotation");//in the y axis
- Serial.println(angleY,4);
- //This prints out the temperature
- Serial.print("Temperature in Fahrenheit : ");
- Serial.print(fTemp,4);
- Serial.println(" F");
- Serial.print("Temperature in not Fahrenheit : ");
- Serial.print(ctemp,4);
- Serial.println(" F");
- //This prints out the presssure
- Serial.print("Pressure : ");
- Serial.print(pressure,4);
- Serial.println(" mbar");
- Serial.print("Xºgggg: ");
- Serial.print(Total_angle_x,4);
- Serial.print(" | ");
- Serial.print("Yºgggg: ");
- Serial.print(Total_angle_y,4);
- Serial.println(" ");
- Serial.print("height: ");
- Serial.println(height,4);
- delay(1000);
- //AcceleraTi2on b2
- Wire.beginTransmission(0x69);
- Wire.write(0x3B);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_rawX2=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawY2=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawZ2=(Wire.read()<<8|Wire.read())/2048.0;
- //Gyroscope 2
- Wire.beginTransmission(0x69);
- Wire.write(0x43);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Gyro_rawX2=Wire.read()<<8|Wire.read();
- Gyro_rawY2=Wire.read()<<8|Wire.read();
- Gyro_rawX2 = (Gyro_rawX2/32.8) - Gyro_raw_errorX2;
- Gyro_rawY2 = (Gyro_rawY2/32.8) - Gyro_raw_errorY2;
- angleX2= Gyro_rawX2 * elapsedTime;
- angleY2= Gyro_rawX2 * elapsedTime;
- Total_angleX2= 0.98 *(Total_angleX2+ angleX2) + 0.02*Accel_angleX2;
- Total_angleY2= 0.98 *(Total_angleY2+ angleY2) + 0.02*Accel_angleY2;
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Send reset command
- Wire.write(0x1E);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh pressure with the OSR = 256
- Wire.write(0x40);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data2 register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data2
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data2
- // ptemp_msb1, ptemp_msb, ptemp_lsb
- if(Wire.available() == 3)
- {
- data2[0] = Wire.read();
- data2[1] = Wire.read();
- data2[2] = Wire.read();
- }
- // Convert the data2
- unsigned long ptemp2 = ((data2[0] * 65536.0) + (data2[1] * 256.0) + data2[2]);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh temperature with the OSR = 256
- Wire.write(0x50);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data2 register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data2
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data2
- // temp_msb1, temp_msb, temp_lsb
- if(Wire.available() == 3)
- {
- data2[0] = Wire.read();
- data2[1] = Wire.read();
- data2[2] = Wire.read();
- }
- // Convert the data2
- unsigned long temp2 = ((data2[0] * 65536.0) + (data2[1] * 256.0) + data2[2]);
- // Pressure and Temperature CalculaTi2ons
- // 1st order temperature and pressure compensaTi2on
- // Difference between actual and reference temperature
- unsigned long dT2 = temp2- ((Coff2[4] * 256));
- temp2 = 2000 + (dT2 * (Coff2[5] / pow(2, 23)));
- // Offset and SensiTi2vitY2calculaTi2on
- unsigned long long off2 = Coff2[1] * 65536 + (Coff2[3] * dT2) / 128;
- unsigned long long sens2 = Coff2[0] * 32768 + (Coff2[2] * dT2) / 256;
- // 2nd order temperature and pressure compensaTi2on
- if(temp2 >= 2000)
- {
- Ti2 = 0;
- offi2 = 0;
- sensi2 = 0;
- }
- else if(temp2 < 2000)
- {
- Ti2 = (dT2 * dT2) / (pow(2,31));
- offi2 = 5 * ((pow((temp2 - 2000), 2))) / 2;
- sensi2 = 5 * ((pow((temp2 - 2000), 2))) / 4;
- if(temp2 < -1500)
- {
- offi2 = offi2 + 7 * ((pow((temp2 + 1500), 2)));
- sensi2 = sensi2 + 11 * ((pow((temp2 + 1500), 2))) / 2;
- }
- }
- // Adjust temp, off, sens based on 2nd order compensaTi2on
- temp2 -= Ti2;
- off2 -= offi2;
- sens2 -= sensi2;
- // Convert the final data2
- ptemp2 = (ptemp2 * sens2 / 2097152 - off2);
- ptemp2 /= 32768;
- float pressure2= ptemp2 / 100.0;
- float cTemp2 = temp2 / 100.0;
- float fTemp2 = cTemp2 * 1.8 + 32.0;
- //This prints out stuff in the serial moniter
- //This prints out Time
- height2= ((pow(pressure_at_sealevel/pressure2, (1/5.257))-1)*(cTemp2+273.15))/0.0065;
- hi2 = 44330.0f * (1.0f - pow((double)pressure2 / (double)pressure_at_sealevel, 0.1902949f));
- Serial.print("HI two: ");
- Serial.println(hi2,4);
- Serial.print("CHANGE IN HEIGHT: ");
- Serial.println(hi2-hi,4);
- //This prints out acceleraTi2on
- Serial.print("X2accel: ");//in the X2axis
- Serial.print(Accel_rawX2, 4);
- Serial.print(" | ");
- Serial.print("Y accel: ");//in the Y2axis
- Serial.print(Accel_rawY2, 4);
- Serial.print(" | ");
- Serial.print("Z accel: ");//in the Z2axis
- Serial.println(Accel_rawZ2, 4);
- //This prints out rotation
- Serial.print("X rotation: ");//in the X2axis
- Serial.println(angleX2,4);
- Serial.print("Y rotation: ");//in the Y2axis
- Serial.println(angleY2,4);
- //This prints out the temperature
- Serial.print("Fahrenheit two : ");
- Serial.print(fTemp2,4);
- Serial.println(" Fº");
- Serial.print("not Fahrenheit two : ");
- Serial.print(cTemp2,4);
- Serial.println(" Cº");
- //This prints out the presssure
- Serial.print("Pressure two : ");
- Serial.print(pressure2,4);
- Serial.println(" mbar");
- Serial.print("Xº: ");
- Serial.print(Total_angleX2,4);
- Serial.print(" | ");
- Serial.print("Yº: ");
- Serial.print(Total_angleY2,4);
- Serial.println(" ");
- Serial.print("height two: ");
- Serial.println(height2,4);
- delay(1000);
- //Acceleration
- Wire.beginTransmission(0x69);
- Wire.write(0x3B);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_rawX3=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawY3=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawZ3=(Wire.read()<<8|Wire.read())/2048.0;
- //Gyroscope
- Wire.beginTransmission(0x69);
- Wire.write(0x43);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Gyro_rawX3=Wire.read()<<8|Wire.read();
- Gyro_rawY3=Wire.read()<<8|Wire.read();
- Gyro_rawX3= (Gyro_rawX3/32.8) - Gyro_raw_errorX3;
- Gyro_rawY3 = (Gyro_rawY3/32.8) - Gyro_raw_errorY3;
- angleX3= Gyro_rawX3* elapsedTime;
- angleY3 = Gyro_rawX3* elapsedTime;
- Total_angle_x3= 0.98 *(Total_angle_x3+ angleX3) + 0.02*Accel_angleX3;
- Total_angle_y3 = 0.98 *(Total_angle_y3 + angleY3) + 0.02*Accel_angleY3;
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Send reset command
- Wire.write(0x1E);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh pressure with the OSR = 256
- Wire.write(0x40);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data
- // ptemp_msb1, ptemp_msb, ptemp_lsb
- if(Wire.available() == 3)
- {
- data3[0] = Wire.read();
- data3[1] = Wire.read();
- data3[2] = Wire.read();
- }
- // Convert the data
- unsigned long ptemp3 = ((data3[0] * 65536.0) + (data3[1] * 256.0) + data3[2]);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh temperature with the OSR = 256
- Wire.write(0x50);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data
- // temp_msb1, temp_msb, temp_lsb
- if(Wire.available() == 3)
- {
- data3[0] = Wire.read();
- data3[1] = Wire.read();
- data3[2] = Wire.read();
- }
- // Convert the data
- unsigned long temp3 = ((data3[0] * 65536.0) + (data3[1] * 256.0) + data3[2]);
- // Pressure and Temperature Calculations
- // 1st order temperature and pressure compensation
- // Difference between actual and reference temperature
- unsigned long dT3 = temp3 - ((Coff3[4] * 256));
- temp3 = 2000 + (dT3 * (Coff3[5] / pow(2, 23)));
- // Offset and Sensitivity calculation
- unsigned long long off3 = Coff3[1] * 65536 + (Coff3[3] * dT3) / 128;
- unsigned long long sens3 = Coff3[0] * 32768 + (Coff3[2] * dT3) / 256;
- // 2nd order temperature and pressure compensation
- if(temp3 >= 2000)
- {
- Ti3 = 0;
- offi3 = 0;
- sensi3 = 0;
- }
- else if(temp3 < 2000)
- {
- Ti3 = (dT3 * dT3) / (pow(2,31));
- offi3 = 5 * ((pow((temp3 - 2000), 2))) / 2;
- sensi3 = 5 * ((pow((temp3 - 2000), 2))) / 4;
- if(temp3 < -1500)
- {
- offi3 = offi3 + 7 * ((pow((temp3 + 1500), 2)));
- sensi3 = sensi3 + 11 * ((pow((temp3 + 1500), 2))) / 2;
- }
- }
- // Adjust temp, off, sens based on 2nd order compensation
- temp3 -= Ti3;
- off3 -= offi3;
- sens3 -= sensi3;
- // Convert the final data
- ptemp3 = (ptemp3 * sens3 / 2097152 - off3);
- ptemp3 /= 32768;
- float pressure3 = ptemp3 / 100.0;
- float ctemp3 = temp3 / 100.0;
- float fTemp3 = ctemp3 * 1.8 + 32.0;
- //This prints out stuff in the serial moniter
- height3 = ((pow(pressure_at_sealevel/pressure3, (1/5.257))-1)*(ctemp3+273.15))/0.0065;
- hi3 = 44330.0f * (1.0f - pow((double)pressure3 / (double)pressure_at_sealevel, 0.1902949f));
- //This prints out time
- Serial.print("time: ");
- Serial.println(time/1000,4);
- Serial.print("change two: ");
- Serial.println(hi3-hi2,4);
- //This prints out acceleration
- Serial.print("X3accel: ");//in the x axis
- Serial.print(Accel_rawX3, 4);
- Serial.print(" | ");
- Serial.print("Y accel: ");//in the y axis
- Serial.print(Accel_rawY3, 4);
- Serial.print(" | ");
- Serial.print("Z accel: ");//in the z axis
- Serial.println(Accel_rawZ3, 4);
- //This prints out rotation
- Serial.print("X3rotation");//in the x axis
- Serial.println(angleX3,4);
- Serial.print("Y rotation");//in the y axis
- Serial.println(angleY3,4);
- //This prints out the temperature
- Serial.print("Temperature in Fahrenheit : ");
- Serial.print(fTemp3,4);
- Serial.println(" F");
- //This prints out the presssure
- Serial.print("Pressure Three: ");
- Serial.print(pressure3,4);
- Serial.println(" mbar");
- Serial.print("Xº: ");
- Serial.print(Total_angle_x3,4);
- Serial.print(" | ");
- Serial.print("Yº: ");
- Serial.print(Total_angle_y3,4);
- Serial.println(" ");
- Serial.print("height three: ");
- Serial.println(height3,4);
- Serial.print ("hi three: ");
- Serial.println(hi3,4);
- delay(1000);
- //AcceleraTi2on b2
- Wire.beginTransmission(0x69);
- Wire.write(0x3B);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Accel_rawX4=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawY4=(Wire.read()<<8|Wire.read())/2048.0;
- Accel_rawZ4=(Wire.read()<<8|Wire.read())/2048.0;
- //Gyroscope 4
- Wire.beginTransmission(0x69);
- Wire.write(0x43);
- Wire.endTransmission(false);
- Wire.requestFrom(0x69,6,true);
- Gyro_rawX4=Wire.read()<<8|Wire.read();
- Gyro_rawY4=Wire.read()<<8|Wire.read();
- Gyro_rawX4 = (Gyro_rawX4/32.8) - Gyro_raw_errorX4;
- Gyro_rawY4 = (Gyro_rawY4/32.8) - Gyro_raw_errorY4;
- angleX4= Gyro_rawX4 * elapsedTime;
- angleY4= Gyro_rawX4 * elapsedTime;
- Total_angleX4= 0.98 *(Total_angleX4+ angleX4) + 0.02*Accel_angleX4;
- Total_angleY4= 0.98 *(Total_angleY4+ angleY4) + 0.02*Accel_angleY4;
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Send reset command
- Wire.write(0x1E);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh pressure with the OSR = 256
- Wire.write(0x40);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data2 register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data2
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data2
- // ptemp_msb1, ptemp_msb, ptemp_lsb
- if(Wire.available() == 3)
- {
- data4[0] = Wire.read();
- data4[1] = Wire.read();
- data4[2] = Wire.read();
- }
- // Convert the data2
- unsigned long ptemp4 = ((data4[0] * 65536.0) + (data4[1] * 256.0) + data4[2]);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Refresh temperature with the OSR = 256
- Wire.write(0x50);
- // Stop I2C Transmission
- Wire.endTransmission();
- delay(500);
- // Start I2C Transmission
- Wire.beginTransmission(Addr);
- // Select data2 register
- Wire.write(0x00);
- // Stop I2C Transmission
- Wire.endTransmission();
- // Request 3 bytes of data2
- Wire.requestFrom(Addr, 3);
- // Read 3 bytes of data2
- // temp_msb1, temp_msb, temp_lsb
- if(Wire.available() == 3)
- {
- data4[0] = Wire.read();
- data4[1] = Wire.read();
- data4[2] = Wire.read();
- }
- // Convert the data2
- unsigned long temp4 = ((data4[0] * 65536.0) + (data4[1] * 256.0) + data4[2]);
- // Pressure and Temperature CalculaTi2ons
- // 1st order temperature and pressure compensaTi2on
- // Difference between actual and reference temperature
- unsigned long dT4 = temp4- ((Coff4[4] * 256));
- temp4 = 2000 + (dT4 * (Coff4[5] / pow(2, 23)));
- // Offset and SensiTi2vitY2calculaTi2on
- unsigned long long off4 = Coff4[1] * 65536 + (Coff4[3] * dT4) / 128;
- unsigned long long sens4 = Coff4[0] * 32768 + (Coff4[2] * dT4) / 256;
- // 2nd order temperature and pressure compensaTi2on
- if(temp4 >= 2000)
- {
- Ti4 = 0;
- offi4 = 0;
- sensi4 = 0;
- }
- else if(temp4 < 2000)
- {
- Ti4 = (dT4 * dT4) / (pow(2,31));
- offi4 = 5 * ((pow((temp4 - 2000), 2))) / 2;
- sensi4 = 5 * ((pow((temp4 - 2000), 2))) / 4;
- if(temp4 < -1500)
- {
- offi4 = offi4 + 7 * ((pow((temp4 + 1500), 2)));
- sensi4 = sensi4 + 11 * ((pow((temp4 + 1500), 2))) / 2;
- }
- }
- // Adjust temp, off, sens based on 2nd order compensaTi2on
- temp4 -= Ti4;
- off4 -= offi4;
- sens4 -= sensi4;
- // Convert the final data2
- ptemp4 = (ptemp4 * sens4 / 2097152 - off4);
- ptemp4 /= 32768;
- float pressure4= ptemp4 / 100.0;
- float cTemp4 = temp4 / 100.0;
- float fTemp4 = cTemp4 * 1.8 + 32.0;
- //This prints out stuff in the serial moniter
- //This prints out Time
- height4= ((pow(pressure_at_sealevel/pressure4, (1/5.457))-1)*(cTemp4+273.15))/0.0065;
- hi4 = 44330.0f * (1.0f - pow((double)pressure4 / (double)pressure_at_sealevel, 0.1902949f));
- Serial.print("change 4: ");
- Serial.println(hi4-hi3,4);
- Serial.print("1-4 change: ");
- Serial.println(hi4-hi,4);
- //This prints out acceleraTi2on
- Serial.print("X accel four: ");//in the X2axis
- Serial.print(Accel_rawX4, 4);
- Serial.print(" | ");
- Serial.print("Y accel: ");//in the Y2axis
- Serial.print(Accel_rawY4, 4);
- Serial.print(" | ");
- Serial.print("Z accel: ");//in the Z2axis
- Serial.println(Accel_rawZ4, 4);
- //This prints out rotation
- Serial.print("X3rotation: ");//in the X2axis
- Serial.println(angleX4,4);
- Serial.print("Y rotation: ");//in the Y2axis
- Serial.println(angleY4,4);
- //This prints out the temperature
- Serial.print("Fahrenheit two : ");
- Serial.print(fTemp4,4);
- Serial.println(" Fº");
- Serial.print("not Fahrenheit two : ");
- Serial.print(cTemp4,4);
- Serial.println(" Cº");
- //This prints out the presssure
- Serial.print("Pressure four : ");
- Serial.print(pressure4,4);
- Serial.println(" mbar");
- Serial.print("Xº: ");
- Serial.print(Total_angleX4,4);
- Serial.print(" | ");
- Serial.print("Yº: ");
- Serial.print(Total_angleY4,4);
- Serial.println(" ");
- Serial.print("height four: ");
- Serial.println(height4,4);
- Serial.print("hi four: ");
- Serial.println(hi4,4);
- delay(1000);
- if(height<200){
- if((hi2-hi) >-20){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if((hi3-hi2) > -20){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi3-hi2 >-20){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi3 >20){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi >20){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- }
- if(400>height>200){
- if((hi2-hi) >15){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if((hi3-hi2) > 15){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi3-hi2 >15){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi3 >15){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi >15){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- }
- if(600>height>400){
- if((hi2-hi) >12){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if((hi3-hi2) > 12){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi3-hi2 >12){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi3 >12){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi >12){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- }
- if(800>height>600){
- if((hi2-hi) >8){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if((hi3-hi2) > 8){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi3-hi2 >8){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi3 >8){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi >8){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- }
- if(1000>height>800){
- if((hi2-hi) >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if((hi3-hi2) > 5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi3-hi2 >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi3 >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- }
- if(17167.25>height>1000){
- if((hi2-hi) >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if((hi3-hi2) > 5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi3-hi2 >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi3 >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi >5){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- }
- if(height>17167.25) {
- if((hi2-hi) >1){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if((hi3-hi2) > 1){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi3-hi2 >1){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi3 >1){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- if(hi4-hi >1){
- digitalWrite(LED, HIGH);
- delay(200);
- digitalWrite(LED, LOW);
- delay(200);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement