Looping Back to setup after void loop() has run a random number of times

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

}

setup() is just a function. If you count your loop executions, you should be able to do something like:

if(++loopExecutions > random(17)){
  setup();
  loopExecutions = 0;
} 

I don't quite understand what you're attempting to say here, But I do appreciate your help! Perhaps are you interpreting this as me wanting to know how many loop executions happen? Because as stated in my thesis, The issue I am attempting to resolve, is why is my loop function reverting back to the setup function after an unknown amount of loops.

Probably because the processor is resetting after some period of time. If that's true then the cause could be electrical noise or something in the code causing it to crash. Electrical noise comes from problems with circuit design or construction, to investigate this we'd need at least a schematic and photos of your project. I leave problems with your code to others.

Here is a photo of the breadboard and the Arduino uno that I got from a kit.

EDIT: The small purple board on the bottom left in the BMP 280, the one to the right of that is the MPU6050 and further to the right is the SD card reader. The MPU6050 is connected to Analog 4 and 5 for the SDA and SCL pins whilst the BMP280 is connected to the SCL and SDA pins on the top right of the Uno. The chip select pin for the SD card reader is on pin 4. The SD card reader and the MPU6050 are connected to the 5V power each and the BMP 280 is connected to 3.3V. the Piezo Speaker, or buzzer, is connected to pin 7 and the red and green LED are connected to pins 8 and 9 respectively. 220 ohms resistors are being used to prevent the lights from going Boom (Or arguably less cool, Sizzle).

I'm sorry I had it backwards, as if you wanted to invoke a random re-setup behavior.

One thing that causes a reset is executing a null function, and these pointer assignments look a little suspicious. What are you trying to do here?

I think 'temperature' might be zero or junk, and temperature.temperature might be something unexpected. Are you sure you are using all the references and pointers correctly?

You are all good, In this Section of code I am attempting to read values of data from each sensor, sensors_event_t (Temp, pressure etc:) is triggering the event to begin reading from those sensors, apologies because while I was troubleshooting I forgot to uncomment the section bmp_temperature->getEvent(&temperature);and //mpu_accel->getEvent(&accel);. the check for groundLevelPressure is to set the value of the pressure at the launchpad as our "Sea Level" Reference point instead of 1013.25 hPa, and the calculations for seaAlt 1 and 2 is just the equation for calculating altitude split into 2 segments because I couldn't figure out how to do it on one giant string, which, would be more memory efficient, and the same thing is happening for the Alt1 and Alt2 but instead of sea level pressure we are plugging in the pressure at ground level so we can get exact altitude measurements. The piece of code cut off at the very end is the beginning of the readouts from the flight computer with the telemetry, used for the serial plotter, and the ///* is there so I can uncomment the multi line comment faster. and temperature.temperature is just asking for the temperature readings, from the temperature sensor, same thing with pressure.pressure asking for the pressure values, from the pressure sensor.

Compiling for an Uno with the code as posted, with verbose compilation output on, gives several warnings on using the unintialized variables and finishes with:

Sketch uses 28794 bytes (89%) of program storage space. Maximum is 32256 bytes.
Global variables use 1723 bytes (84%) of dynamic memory, leaving 325 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

325 bytes might not be much memory for local variables, and could be causing problems. You might look into reducing your memory usage with the F() macro.

This small and simple fix, Solved my Issue! Thank you so much for your help!

1 Like

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