Hey Guys,
So I'm writing out some code for an Arduino nano that's hooked up to two Servos and the BMP180, the idea is that when the Altimeter realizes its hit apogee its will wait until it reaches a certain altitude to release the parachutes by turning the servo arm 90 degrees. I think the code should work, but its my first time programming with Arduino and I'd love some guidance.
So a bit of background knowledge of the code.
- The code sets the starting altitude as zero
- It reads out its code to the computer (for testing)
- It sees if theirs a drop in altitude and if does it'll wait until it reaches its set altitude to deploy the servos
//---This sketch will make your altitude zero when you press the reset button---//
//---Allowing you to know increases and decreases in altitude based on your initial location---//
//---Altitude is in Meters---//
// Include the Wire library for I2C access.
#include <Wire.h>
// Include the BMP180 library.
#include <BMP180.h>
//inlcudes the Servo library
#include <Servo.h>
//------------ Store an instance of the BMP180 sensor. ----------------//
BMP180 barometer;
//------------ Use the on board LED for an indicator. -----------------//
int indicatorLed = 13;
//--- Set current pressure & altitude, in this example it will be zero when we press reset button. ---//
int initialPressure = 0;
int initialAltitude = 0;
int lastAltitude;
Servo myservo; //create the Servo object.
void setup()
{
initialPressure = barometer.GetPressure();
initialAltitude = barometer.GetAltitude(initialPressure);
long currentPressure = barometer.GetPressure();
float altitude = barometer.GetAltitude(initialPressure);
// Start the serial library to output our messages.
Serial.begin(9600);
// Start the I2C on the Arduino for communication with the BMP180 sensor.
Wire.begin();
// Set up the Indicator LED.
pinMode(indicatorLed, OUTPUT);
// Check to see if connected to the sensor.
if(barometer.EnsureConnected())
{
Serial.println("Connected to BMP180."); // Output - connected to the computer.
digitalWrite(indicatorLed, HIGH); // Set our LED.
// When connected, reset the device to ensure a clean start.
barometer.SoftReset();
// Initialize the sensor and pull the calibration data.
barometer.Initialize();
}
else
{
Serial.println("Not connected to BMP180.");
digitalWrite(indicatorLed, LOW); // Set our LED.
}
myservo.attach( , ); //pins yet to be assigned
myservo.write(0); //starting position is zero
}
void loop()
{
if(barometer.IsConnected)
{
// Retrive the current pressure in Pascals.
long currentPressure = barometer.GetPressure();
// Retrive the current altitude (in meters).
float altitude = barometer.GetAltitude(currentPressure);
// Print out the Altitude.
Serial.print("Altitude: ");
Serial.print(altitude);
Serial.print(" m");
// Retrive the current temperature in degrees celcius.
float currentTemperature = barometer.GetTemperature();
Serial.println(); // Start a new line.
delay(100); // Show new results every half second.
}
get altitude(); //gets the current altitude.
if(altitude - lastaltitude <= -1) //checks for a 2 meter drop in altitude
{
delay(100); //enough time to allow for the rocket to drop another amount to verify apogee was passed.
get altitude(); //Gets altitude reading again
if(altitude -lastAltitude <= -2) // checks to see if it decreased by 2 meters.
{
delay(150);
get altitude;
if(altitude - lastAltitude <= -3) //checks to see if it decreased y 3 meters.
{
if(altitude == 150) //altitude is not set but for now it works.
myservo.write(90); //moves servo to open nose cone.
}
else
{
lastAltitude = altitude; //didnt fall 3 meters
}
}
else
{
lastAltitude = altitude; //didnt fall 2 meters
}
}
else
{
lastAltitude = altitude; //didnt fall 1 meters
}
}
Thank You