At the moment I am currently trying to make a computer as a payload for one of my newer model rockets. I am quite new to Arduino and have only been following a project series but finally decided to order an Adafruit MPU6050, an Adafruit BMP 280, and a SD card reader so I can get information from the rocket back to my computer for analysis. However, I have encountered many problems. starting from the I2C bus only accepting 2 inputs before my computer stops running code, from either the MPU or the BMP in any combination, but that isn't the topic of this problem. I recently have tried to make it so my data can be exported as a CSV so I can easily graph the data so I can better understand the rockets trajectory during the flight. Unbeknownst to me, it writes this data a random number of times, and then goes back to the setup function, both the setup and loop are void to they don't request anything from each other. and I also coded a system so my flight computer would chirp and flash lights according to what issue it is currently facing, i;e if the SD card reader doesn't have the card installed or if the barometer is disconnected. So after going back through all of those checks, it goes to the loop function again and repeats that a couple of times, before going BACK to the setup function. I have been troubleshooting for 2 hours now and am completely lost. Any advice? My code is pasted below for anybody who thinks they can help. (You May notice a massive comment in the middle of my loop. that was my old way of writing data to the SD card, it is still there because that still works without the weird looping bug)
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include <math.h>
#include <SD.h>
#include <Adafruit_MPU6050.h>
/*
Connections:
A Green LED is connected to pin 9. and a Red LED is connected to pin 8
The barometric pressure sensor is connected to the SCL and SDA Pins on the top left.
The barometric pressure sensor is connected to 3.3V directly from the board.
The barometric pressure sensor is also directly connected to the ground pin.
A buzzer is connected to pin 7
The SD Card is connected to the same power source as the Barometer.
CS is on Pin 4
MOSI is on pin 5
CLK is on pin 6
and MISO is connected to pin 7
The Gyro/Accelerometer is connected to the SCL and SDA Pins on the Top left
*/
File myFile; // Naming the root value to resort back to easier
Sd2Card card;
Adafruit_BMP280 bmp;
Adafruit_Sensor *bmp_temperature = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
Adafruit_MPU6050 mpu;
Adafruit_Sensor *mpu_temp, *mpu_accel, *mpu_gyro;
float groundLevelPressure = 0;
float lastAltitude = 0;
int lastVelocity = 0;
float Altitude = 0;
float Alt1 = 0;
float Alt2 = 0;
int buzzer = 7;
int greenLED = 9;
int redLED = 8;
const int chipSelect = 4;
int newStandardCalc = 44330;
float seaAlt1 = 0;
float seaAlt2 = 0;
float seaAltitude = 0;
float pi = 3.1415926;
float flightTime = 0;
void setup() {
Serial.begin(9600); // Starts Serial Monitor at 9600 Baud
pinMode(buzzer, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
while( !Serial ) {
delay(100);
}
unsigned status;
//Status is to begin the BMP
Serial.println("Starting testing...");
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(1000);
status = bmp.begin();
if(!status) {
Serial.println("Barometer Disconnected! Check Connections and Recycle Power!");
digitalWrite(greenLED, HIGH);
while(1) {
digitalWrite(redLED, HIGH);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(100);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
digitalWrite(redLED, LOW);
delay(100);
}
}
Serial.println("Barometer Good!");
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(1000);
if(!mpu.begin()) {
Serial.println("Gyroscope Disconnected! Check Connections and Recycle Power!");
while(1) {
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, HIGH);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(100);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
delay(100);
}
}
Serial.println("Gyro Good!");
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(1000);
if(!card.init(SPI_HALF_SPEED, chipSelect)) { //Used for checking if the SD Card file exists or if the SD card reader is out of place
Serial.println("SD Card Disconnected! Check Connections and Recycle Power!");
digitalWrite(redLED, HIGH);
while(1) {
digitalWrite(greenLED, HIGH);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(100);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
digitalWrite(greenLED, LOW);
delay(100);
}
}
Serial.println("SD test 1 Good!");
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, // Operating Mode
Adafruit_BMP280::SAMPLING_X2, // Temperature oversampling
Adafruit_BMP280::SAMPLING_X16, // Pressure oversampling
Adafruit_BMP280::FILTER_X16, // Filtering
Adafruit_BMP280::STANDBY_MS_500); // Standby time
mpu_temp = mpu.getTemperatureSensor();
mpu_accel = mpu.getAccelerometerSensor();
mpu_gyro = mpu.getGyroSensor();
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(1000);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
digitalWrite(redLED, HIGH);
while (1) {
digitalWrite(greenLED, HIGH);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(100);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
digitalWrite(greenLED, LOW);
delay(100);
}
}
Serial.println("SD Test 2 Good!");
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(1000);
myFile = SD.open("logs.txt", FILE_WRITE);
myFile.println();
myFile.println("-------------------------");
myFile.println(" Logging New Flight Data ");
myFile.println("-------------------------");
myFile.println();
Serial.println();
Serial.println("-------------------------");
Serial.println(" Logging New Flight Data ");
Serial.println("-------------------------");
Serial.println();
//Serial.println("TimeStamp, Temperature, Pressure, Altitude, Gyro X, Gyro Y, Gyro Z, Accel X, Accel Y, Accel Z");
//myFile.println("TimeStamp, Temperature, Pressure, Altitude, Gyro X, Gyro Y, Gyro Z, Accel X, Accel Y, Accel Z");
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(1000);
digitalWrite(greenLED, HIGH);
tone(buzzer, 5000);
delay(3000);
noTone(buzzer);
delay(100);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(100);
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(100);
delay(1000);
myFile.close();
delay(3000);
Serial.println("Starting Data Logging...");
}
void loop() {
delay(50);
myFile = SD.open("logs.txt", FILE_WRITE);
//Gets the information from both the temperature pressure gyro, and accelerometer sensors.
sensors_event_t temperature;
sensors_event_t pressure;
sensors_event_t accel;
sensors_event_t gyro;
//bmp_temperature->getEvent(&temperature);
bmp_pressure->getEvent(&pressure);
mpu_gyro->getEvent(&gyro);
//mpu_accel->getEvent(&accel);
if(groundLevelPressure == 0) {// Sets the permanent ground level
groundLevelPressure = pressure.pressure;
}
seaAlt1 = 44330*(1-(pressure.pressure/1013.25));
seaAlt2 = 1/5.255;
seaAltitude = pow(seaAlt1, seaAlt2);
Alt1 = newStandardCalc*(1-(pressure.pressure/groundLevelPressure));
Alt2 = (1/5.255);
Altitude = pow(Alt1,Alt2);
//Format more akined to the serial plotter / graphs
///*
Serial.print(flightTime);
Serial.print(",");
Serial.print((temperature.temperature*9/5)+32);
Serial.print(",");
Serial.print(pressure.pressure/1000);
Serial.print(",");
Serial.print(Altitude);
Serial.print(",");
Serial.print(seaAltitude);
Serial.print(",");
Serial.print(gyro.gyro.x*180/pi);
Serial.print(",");
Serial.print(gyro.gyro.y*180/pi);
Serial.print(",");
Serial.print(gyro.gyro.z*180/pi);
Serial.print(",");
Serial.print(accel.acceleration.x);
Serial.print(",");
Serial.print(accel.acceleration.y);
Serial.print(",");
Serial.print(accel.acceleration.z);
Serial.println();
//-------------------------------------------------------------
myFile.print(flightTime);
myFile.print(",");
myFile.print((temperature.temperature*9/5)+32);
myFile.print(",");
myFile.print(pressure.pressure/1000);
myFile.print(",");
myFile.print(Altitude);
myFile.print(",");
myFile.print(seaAltitude);
myFile.print(",");
myFile.print(gyro.gyro.x*180/pi);
myFile.print(",");
myFile.print(gyro.gyro.y*180/pi);
myFile.print(",");
myFile.print(gyro.gyro.z*180/pi);
myFile.print(",");
myFile.print(accel.acceleration.x);
myFile.print(",");
myFile.print(accel.acceleration.y);
myFile.print(",");
myFile.print(accel.acceleration.z);
myFile.println();
//*/
//--------------------------------------------------------------
/*
//Old Method of Saving Data
Serial.println(); // makes new line
Serial.print(F("Temperature = ")); //Tells us which measurement we are looking at.
Serial.print(((temperature.temperature*9/5)+32)); //Tells us the approximate temperature in Farenheight
Serial.println(F("*F")); //Declares what unit is being used to measure Temperature
myFile.print(F("Temperature = ")); //Tells us which measurement we are looking at.
myFile.print(((temperature.temperature*9/5)+32)); //Tells us the approximate temperature in Farenheight
myFile.println(F("*F")); //Declares what unit is being used to measure Temperature
myFile.println(); // makes new line
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.print(F("Pressure = ")); //Tells us which measurement we are looking at.
Serial.print((pressure.pressure/1000)); // tells us that we are measuring pressure in bar
Serial.println(F( "Bar")); //Declares what unit is being used to measure Pressure
myFile.print(F("Pressure = ")); //Tells us which measurement we are looking at.
myFile.print((pressure.pressure/1000)); // tells us that we are measuring pressure in bar
myFile.println(F( "Bar")); //Declares what unit is being used to measure Pressure
myFile.println(); // makes new line
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.print(F("Altitude = ")); //Tells us which measurement we are looking at.
Serial.print(Altitude);//tells us our approximate altitude in meters
Serial.println(F(" Meters")); //Declares what unit is being used to measure Altitude
myFile.print(F("Altitude = ")); //Tells us which measurement we are looking at.
myFile.print(Altitude);//tells us our approximate altitude in meters
myFile.println(F(" Meters")); //Declares what unit is being used to measure Altitude
myFile.println(); // makes new line
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.print(F("Sea Level Altitude = ")); //Tells us which measurement we are looking at.
Serial.print(seaAltitude);//tells us our approximate altitude in meters
Serial.println(F(" Meters")); //Declares what unit is being used to measure Altitude
myFile.print(F("Sea Level Altitude = ")); //Tells us which measurement we are looking at.
myFile.print(seaAltitude);//tells us our approximate altitude in meters
myFile.println(F(" Meters")); //Declares what unit is being used to measure Altitude
myFile.println(); // makes new line
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.print(F("Gyro X = "));
Serial.print(gyro.gyro.x*180/pi);
Serial.println(F(" Degrees"));
myFile.print(F("Gyro X = "));
myFile.print(gyro.gyro.x*180/pi);
myFile.println(F(" Degrees"));
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.println();
myFile.println(); // makes new line
Serial.print(F("Gyro Y = "));
Serial.print(gyro.gyro.z*180/pi);
Serial.println(F(" Degrees"));
myFile.print(F("Gyro Y = "));
myFile.print(gyro.gyro.x*180/pi);
myFile.println(F(" Degrees"));
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.println();
myFile.println(); // makes new line
Serial.print(F("Gyro Z = "));
Serial.print(gyro.gyro.z*180/pi);
Serial.println(F(" Degrees"));
myFile.print(F("Gyro Z = "));
myFile.print(gyro.gyro.x*180/pi);
myFile.println(F(" Degrees"));
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.println();
myFile.println(); // makes new line
Serial.print(F("Lateral Acceleration (X) = "));
Serial.print(accel.acceleration.x);
Serial.println(F(" M/s"));
myFile.print(F("Lateral Acceleration (X) = "));
myFile.print(accel.acceleration.x);
myFile.println(F(" M/s"));
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.println();
myFile.println(); // makes new line
Serial.print(F("Vertical Acceleration (Y) = "));
Serial.print(accel.acceleration.y);
Serial.println(F(" M/s"));
myFile.print(F("Vertical Acceleration (Y) = "));
myFile.print(accel.acceleration.y);
myFile.println(F(" M/s"));
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.println();
myFile.println(); // makes new line
Serial.print(F("Roll Acceleration (Z) = "));
Serial.print(accel.acceleration.z);
Serial.println(F(" M/s"));
myFile.print(F("Roll Acceleration (Z) = "));
myFile.print(accel.acceleration.z);
myFile.println(F(" M/s"));
Serial.println(); // makes new line
myFile.println(); // makes new line
Serial.println();
myFile.println(); // makes new line
Serial.print(F("Timestamp = "));
Serial.print(flightTime + 0.5);
Serial.println(F(" Seconds"));
myFile.print(F("Timestamp = "));
myFile.print(flightTime + 0.5);
myFile.println(F(" Seconds"));
Serial.println(); // makes new line
myFile.println(); // makes new line
*/
Serial.println(); // makes new line
lastVelocity =((Altitude - lastAltitude)*2);
lastAltitude = (Altitude); //saves the last altitude recorded
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
myFile.close();
flightTime += 0.1;
delay(50); //Waits a 10th of a second before continuing
}