Dear Arduino Forum,
I am try to build a bottle rocket which automatically deploys a parachute using a servo motor when a pressure sensor(MPL3115a2) detects decreasing altitude. I have figured out how to make everything work when connected via USB Mini B to the PC. (I am using arduino Nano) This is the code which works:
#include <I2C.h>
#include <Wire.h>
#include "SparkFunMPL3115A2.h"
#include <Servo.h>
//Create an instance of the object
MPL3115A2 myPressure;
//Define the led pin
int led = 9;
//Define servo pin
int servoPin = 5;
//Create servo object
Servo myServo;
//Define floats
float curAlt;
float prevAlt;
void setup()
{
Wire.begin(); // Join i2c bus
Serial.begin(9600); // Start serial for output
myPressure.begin(); // Get sensor online
//Configure the sensor
myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
//myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
//test LED
pinMode(led, OUTPUT);
//Attach servo
myServo.attach(servoPin);
myServo.write(0);
}
void loop()
{
float altitude = myPressure.readAltitude(); //get the value of the sensor
prevAlt = curAlt; //change previous altitude to current altitude
curAlt = altitude; //before changing current altitude to the (current) altitude sensor reading
//print all values
Serial.print(" Altitude(M):");
Serial.print(altitude);
Serial.print(" ");
Serial.print("PrevAlt:");
Serial.print(prevAlt);
Serial.print(" ");
Serial.print("CurAlt:");
Serial.print(curAlt);
Serial.println();
//this is only for use later in my project (when the rocket falls, activate something)
if (curAlt - prevAlt < -0.7) {
myServo.write(180);
digitalWrite(led, HIGH);
delay(5000);
digitalWrite(led, LOW);
myServo.write(0);
}
delay(500);
}
This code works when connected to my Macbook and I have been able to activate the sensor and make it turn back. As soon as I connect a 9v battery to GND and Vin it does not work anymore (only twitching slightly) I have written this code to test the servo:
#include<Servo.h>
Servo myServo;
int pos = 0;
void setup() {
myServo.attach(5);
Serial.begin(9600);
}
void loop() {
myServo.write(pos);
if (pos == 0) {
pos = 180;
myServo.write(pos);
delay (1000);
}
Serial.println(pos);
if (pos == 180) {
pos = 0;
myServo.write(pos);
delay (1000);
}
Serial.println(pos);
}
Now to the things I have tried:
-
I have powered arduino using 9v battery and power servo using 5+ arduino
-
I have bought a 3.7v LiPo and had it power the arduino and servo through 5+ from arduino
-
I have used a step up converter together with the 3.7v LiPo (arduino started smoking/was fried and I changed all the parts involved)
-
I have connected 9v battery to power arduino and 3.7v LiPo separately to power servo (so that it does not take too much power and stall the arduino)
-
I have connected the 9v battery to power the arduino using GND and 5+ while directly taking from GND and Power from the battery. (not using power from arduino)
All of these things have not worked and I have reached the limit of what I can think of doing differently.
I'd very much appreciate any help
Thanks in advance
Nick