Code:
void setup() {
Serial.begin(115200);
while (!Serial) {}
...
}
Serial is not gonna be set when your flight computer is not plugged into USB. Consider looking into precompiler stuff (ie, "#define debug", "#ifdef debug", etc) so that when you aren't building this for debugging, it won't break your code. Otherwise your computer is just going to get stuck on that while loop and you'll have a guaranteed lawn dart.
Code:
float currentAltitude = bmp.readAltitude(1019); // Replace 1019 with your local pressure for accurate altitude readings
You'd need better input than a statically coded pressure. Barometric pressure can shift a lot over the course of the day so unless you plan on compiling your code right before flight, it won't be accurate. The better way to do it is take a reading at ground level/before launch and then take the difference between that and the latest altitude reading. That will give you the altitude above ground level which is what you need for safe deployment using a barometer.
Code:
254 if (currentAltitude > PARACHUTE_DEPLOYMENT_ALTITUDE) {
255 // Check if the parachute has not been deployed yet to avoid multiple deployments
256 if (!parachuteDeployed) {
257 // Check for safety conditions before deploying the parachute
258 if (isSafeToDeployParachute()) {
259 // Deploy the parachute using pyro1
260 deployParachute();
261 parachuteDeployed = true;
262 } else {
263 Serial.println("Parachute deployment conditions not met. Safety check failed.");
264 }
265 }
266 } else {
267 // Reset the flag when the altitude drops below the deployment threshold
268 parachuteDeployed = false;
269 }
On accent, this will deploy your parachute prematurely and you'll get a zippered rocket. What you need to do is a range: if you want to deploy at 300ft, your "currentAltitude" must be less than 325ft and greater than 275ft. That way you don't have to hit the specific altitude to a decimal point. Also, it doesn't really matter if you hit the deploy parachute trigger multiple times within an acceptable window because once the pyro, cord cutter, or chute release has deployed, you'll need to physically reinstall it for it to deploy again.
You may also want to monitor the vertical velocity because if it's not moving down, you absolutely should never deploy (it's a great safety... I have several programmed into my flight computer.) Also, if you're trying to deploy at apogee, you might not be able guaranteed to reach the desired altitude if you preprogram it. Sims are often right up to a certain margin. Ive gotten pretty good at accounting for everything on my rockets, but I still get about a 5% difference from the simulated altitude. You will have to monitor when you launch, and when you fall below apogee. The former is so you don't accidentally trigger your ejection charge by lifting and lowering your rocket on the ground, and the latter so that no matter what apogee you reach, it deploys after apogee.
Code:
273 bool isSafeToDeployParachute() {
274 // Implement safety checks here
275 // For example, check for noise voltage or other false triggers
276 float noiseVoltage = readNoiseVoltage(); // Implement a function to read noise voltage
277
278 if (noiseVoltage < MAX_NOISE_VOLTAGE) {
279 return true; // It's safe to deploy the parachute
280 } else {
281 Serial.println("Unsafe to deploy parachute. High noise voltage detected.");
282 return false;
283 }
284 }
285
286 float readNoiseVoltage() {
287 // Implement a function to read noise voltage
288 // Replace A1 with the actual analog pin used for measuring noise voltage
289 int sensorValue = analogRead(A1);
290 // Replace 1023 with the maximum value your analog-to-digital converter can produce
291 float voltage = sensorValue * (MAX_VOLTAGE / 1023.0);
292 return voltage;
293 }
What does "Noise Voltage" have to do with your parachute safety? You should check if the rocket is above the ground, falling, and is moving at a speed that is safe for parachute deployment (that can vary on the composition of the rocket because some tubes are more resilient than others; I use plastic/printed rockets and fiberglass and deploy around 30fps... ymmv).
Motorized fins are cool, but get the basics first. Dynamic stabilization is notoriously difficult to do from what I have read. You need to figure out safe deployment first and demonstrate it works on your rocket.
Another recommendation, implement something like a Kalman filter. You can get away with a rolling average (and I have my own way of doing it), but Kalman is the way to go. It's lightweight, low-impact. Your sensor data is going to vary a little if it's not preprocessed.
Also, consider looking minimizing the data footprint. Rather than storing information as raw text in a file, learn how to write and read byte files. It helps a ton if you want to consider live telemetry.