Engineering 100-980
Programming Tips
Short reminders and useful information for the Arduino code in this class.
Contents
TLDR
- Use
delay()very frugally in flight code. Use amillis()timer when possible instead. - Log the accelerometer at 100 Hz or faster. Here’s why.
- Never wait on the BME680. Use
beginReading()/endReading(), notperformReading(). - Do your math with floats. Write
5.0 / 1023.0, not5 / 1023.
Timing Your Loop Without delay()
So far, most of our starter code has ended loop() with a delay(). While this has worked for our labs, delay() freezes the whole Arduino: nothing gets read or logged while it waits. It also doesn’t actually set your sample rate. Your real loop time is the delay plus however long it takes to read your sensors and write to the microSD card, so delay(10) does not give you 100 Hz.
Instead, we can use millis(), which returns the number of milliseconds since the Arduino turned on, to check if enough time has passed before taking the next sample:
const unsigned long SAMPLE_MS = 10; // 10 ms between samples = 100 Hz
unsigned long lastSample = 0;
void loop() {
unsigned long now = millis();
if (now - lastSample >= SAMPLE_MS) {
lastSample = now;
// read sensors + write one line to the microSD card
}
// other jobs (like checking the BME680) go here, outside the if
}
Note: Always store times in an unsigned long. An int can only hold about 32 seconds worth of milliseconds before it overflows!
A few more things to keep in mind:
- Write the check as
now - lastSample >= SAMPLE_MS - If you need more than one rate, use one timer per job, like sampling every 10 ms and checking the battery every 1000 ms.
- To see what rate you are actually getting, log
millis()as the first column of your CSV and look at the gaps between rows.
Why Sample Fast?
Your rocket’s motor only burns for about 1-2 seconds, and that is the most important part of the flight! The Lab 4 starter code logs at about 2 Hz, which would give you only about 3 data points during the entire burn.
There is also a rule called the Nyquist rate: to capture a signal, you need to sample at least twice as fast as the highest frequency in that signal. The ADXL335 module filters its outputs at about 50 Hz, so we want to log at 100 Hz or faster. This is also the requirement for your Go/No-Go tests.
Sampling fast matters even more once you integrate your acceleration data to get velocity and altitude, since the error grows as the time between samples gets larger.
The BME680 does not need to keep up with the accelerometer. Pressure changes much more slowly than acceleration, so log the accelerometer every sample and just repeat the most recent BME680 values until a new reading comes in.
The BME680
All of the BME680 functions used below come from Adafruit’s library. You can find what each one does in the Adafruit BME680 library documentation.
performReading() Freezes Your Code
The BME680-Example code uses performReading(), which does not return until the measurement is completely finished. With the example’s settings, each call freezes the Arduino for about a third of a second. This happens for two reasons:
- The gas heater (320 °C for 150 ms) runs after the temperature, pressure, and humidity measurements.
- The library waits twice the remaining time, just to be safe.
This means your whole loop, including the accelerometer, can only run at about 3 Hz.
Using Async Functions
To fix this, we can use the BME680’s async functions. An async function lets the Arduino start a task and keep doing other things while that task finishes, instead of waiting around for it.
// in loop(), outside your sample timer
bme.beginReading();
if (bme.remainingReadingMillis() == 0) {
bme.endReading();
temperature = bme.temperature;
pressure = bme.pressure;
}
In words, this code:
- Starts a reading with
beginReading(). If a reading is already running, it does nothing. - Checks if the reading is done by checking if
remainingReadingMillis()equals 0. - If it is done,
endReading()grabs the results, which we save into our own variables. - If it is not done, the code just moves on, so the loop can keep reading the accelerometer and other stuff.
The Gas Heater
If your team doesn’t need gas data, turning the heater off makes the BME680 much faster.
Watch out: setGasHeater(0, 0) turns the heater off, but the library still waits the old 150 ms anyway! To actually speed things up, shrink the heater time first:
bme.setGasHeater(320, 1); // set the heater time the library waits for to 1 ms...
bme.setGasHeater(0, 0); // ...then switch the heater off
| Setup | Approximate BME680 Rate |
|---|---|
performReading(), heater on |
about 3 Hz |
| Async, heater on | about 6 Hz |
| Async, heater off (the trick above) | 50+ Hz |
These rates assume 1x oversampling, like in the BME680-Example code. Higher oversampling (the library’s default if you don’t set it) is less noisy, but slower.
Other BME680 Tips
bme.pressureis in Pa, not hPa. Divide by100.0(not100!) to get hPa.- Avoid
readAltitude(). It needs today’s sea-level pressure, and it runs another blocking reading. - Keep the IIR filter small (0-3). A large filter smooths the data, but makes it lag behind, which smears out apogee. You can always smooth your data afterwards in your spreadsheet.
- If
bme.begin()fails, double-check that the CS pin in your code matches your wiring, and that SDI goes to pin 11 and SDO goes to pin 12. These get swapped a lot!
Analog Sensors
The TMP36, the voltage divider, and the ADXL335 are all read with analogRead(), so they all share the same common mistakes. For a refresher on how this works, see How Analog to Digital Converters (ADCs) Work in Lab 3.
Integer math truncates! analogRead(A1) * 5 / 1023 can only ever be 0, 1, 2, 3, 4, or 5. Instead, write analogRead(A1) * (5.0 / 1023.0).
MicroSD Card Logging
- Filenames must be in 8.3 format: at most 8 characters, a dot, then at most 3 characters.
FLIGHT.CSVworks, butflightdata.csvwill fail andSD.open()will return false. - Some writes randomly take 10-100+ ms while the card erases a block. This is normal, and is one more reason to log
millis()on every line.