Hi @mikami2020
To wrap the barometer initialisation into a single function (with no parameters or return value):
void initialiseBarometer()
{
bmp280.begin(); // Default initialisation, place the BMP280 into SLEEP_MODE
bmp280.setPresOversampling(OVERSAMPLING_X4); // Set the pressure oversampling to X4
bmp280.setTempOversampling(OVERSAMPLING_X1); // Set the temperature oversampling to X1
bmp280.setIIRFilter(IIR_FILTER_4); // Set the IIR filter to setting 4
bmp280.setTimeStandby(TIME_STANDBY_125MS); // Set the standby time to 125 milliseconds
bmp280.startNormalConversion(); // Start BMP280 continuous conversion in NORMAL_MODE
}
In this line I imagined a readLaunchInput() function that's monitoring a (debounced) launch switch. It returns a bool (true or false) value stored in the launchInput variable.
When the lauch switch is thrown, the microcontroller then knows that the rocket has been launched. It can then move on to the LAUNCH phase of the state machine and start calculating the altitude above the launch site.
I used this line to simplify the state machine code, however if you're using the BMP280_DEV library then it works slightly differently. In this case, it's necessary to declare a floating point global variable before the setup() function:
float launchSiteAltitude;
Then pass it through to the library's getAltitude() function in the state machine:
bmp280.getAltitude(launchSiteAltitude);
The launchSiteAltitude variable is updated each time the function is called and a new barometer measurement is ready, otherwise the variable remains unchanged:
case ARMED:
launchInput = readLaunchInput();
bmp280.getAltitude(launchSiteAltitude);
if (launchInput == true)
{
stateMachine == LAUNCH;
}
break;