Hi everyone, I'm working on an attiny85 based altimeter for a model rocket and I could use some help getting nudged in a more efficient direction.
What I'm trying to do is calculate the vertical speed of the rocket based on bme280 altitude readings in the least amount of space since an attiny is limited in pretty much every respect.
I've gone through a couple different iterations and I'm sure that if I had paid more attention in algebra this would be trivial, so what I know is that I need two altitude readings, separated by a known time, then its just distance traveled divided by time.
My actual code is all scrambled at the moment so I'm just going to type up some pseudocode of what I'm trying out, since I'm just looking for a general idea of a decent function instead of the entire program.
So the clunky way of doing this is something like this:
alt1 = bme280reading()
alt1filtered = filterfunction(alt1)
delay(100);
alt2 = bme280reading()
alt2filtered = filterfunction(alt2)
vertical_speed = (alt2 - alt1) / 0.1
but that involves two calls to the sensor reading function and two calls to the filter function to get rid of noise. Ideally I would only have one call both functions, but I'm having trouble figuring out how to best set up a millis() if statement to get 100 miliseconds between each reading.
Maybe something like this:
int period = 100
bool run = true
float alt1
float alt2
unsigned long time_now = 0
void loop()
{
alt = bme280reading()
altfiltered = filterfunction(alt)
if run = true
{
alt1 = altfiltered
run = false
}
if (millis() >= time_now + period)
{
time_now += period;
alt2 = altfiltered
run = true
}
float vertical_speed = (alt2 - alt1) / 0.1
That code only makes one call to the sensor and filter function, sets alt1 to the sensor reading, waits 100 milliseconds, sets alt2 to the most recent sensor reading, calculates vertical speed and sets the bool run to true to update alt1 on the next loop. It has the advantage of more accurate timing of millis() instead of delay.
I'm not sure how much more efficient that is considering its many more lines, but having only one call to the filter and sensor functions should really help. Is there a better way to do what I'm trying to accomplish?