Arduino and I2C sensors

Hi everyone, I want to combine rocket thrust vectoring (TVC) gyro stabilization code with pressure sensor BMP180 code. How can I combine this codes ?. I am looking for using one Arduino Uno board with this 2 sensors. Here are the codes:

//1. Gyro stabilization:

#include <MPU6050.h>
#include <Servo.h>
#include <Wire.h>
#include <I2Cdev.h>

MPU6050 mpu;
int16_t ax, ay, az; // Creates a 16 bit integer for accelerometer values
int16_t gx, gy, gz; // Creates a 16 bit integer for gyro values
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int val1;
int val2;
int val3;
int val4;
int prevVal1;
int prevVal2;
int prevVal3;
int prevVal4;

void setup()
{
Wire.begin();
Serial.begin(38400); // Initiates the wire address
Serial.println("Initialize MPU"); // Display message
mpu.initialize(); // Initialises MPU 6050
Serial.println(mpu.testConnection() ? "Connection Successful" : "Connection Failed"); // Test gyro connection
servo1.attach(9); // Attaches Servo 1 to pin 9
servo2.attach(10); // Attaches Servo 2 to pin 10
servo3.attach(3); // Attaches Servo 3 to pin 3
servo4.attach(11); // Attaches Servo 4 to pin 11

}

void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Gets data from gyro and accelerometer

val1 = map(ax, -17000, 17000, 0, 179); // range of values from sensor based on sensitivity
if (val1 != prevVal1) // if val1 is not equal to prevVal1
{
servo1.write(val1); // sets servo to value
prevVal1 = val1;
}
val2 = map(ax, -17000, 17000, 179, 0); // Servo 2
if (val2 != prevVal2)
{
servo2.write(val2); // sets servo to value
prevVal2 = val2;
}
val3 = map(ay, -17000, 17000, 0, 179); // Servo 3
if (val3 != prevVal3)
{
servo3.write(val3); // sets servo to value
prevVal3 = val3;
}
val4 = map(ay, -17000, 17000, 179, 0); // Servo 4
if (val4 != prevVal4)
{
servo4.write(val4); // sets servo to value
prevVal1 = val4;
}
delay(50); // Delays for 50 milliseconds
}

//2. PPRESSURE SENSOR (BMP180) CODE

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Servo.h>

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); // sets up the BMP180 USING library from Adafruit

int altitude;
int lastAltitude;
int groundAltitude;

Servo myservo; // declaring servo name

void setup() {

Serial.begin(9600); // Begins the serial monitor for debugging, for usb communication Ardiuo IDE to Uno board, 9600 quick

if(!bmp.begin()) // Initializes the BMP180
{
while(1);
}
myservo.attach(9); // Mounts the servo to pin 9
myservo.write(0); // Writes starting pos of servo

sensors_event_t event; // Starts a reading
bmp.getEvent(&event);
groundAltitude = bmp.pressureToAltitude(1013,event.pressure); //reads altitude from pressure sensor

}

void loop() {

getAltitude(); // Gets current altitude

if(altitude - lastAltitude <= -1) // Checks for a 1 meter drop in altitude
{

myservo.write(45); // Moves servo to deploy parachute
    Serial.println(" Deploying a parachute ");
  }

else
{
lastAltitude = altitude; // False reading. Sets current altitude to the last measured altitude
Serial.println(" Didnt fall, hope yu backed up manually !!!");
}
}

void getAltitude() // Reads the BMP180 and returns an altitude in meters
{
sensors_event_t event; // Starts a reading
bmp.getEvent(&event);
altitude = bmp.pressureToAltitude(1013,event.pressure); // Sets the altitude to int 'altitude'
}

Please show us your best effort, and/or a specification of how the combined code should perform.

(Guidance of model rockets is forbidden in most right-thinking jurisdictions)

Please remember to use code tags when posting code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.