How to add if altitude registers this than use this temperature

Hello. I wanted to know how to program the code that will read the BME280 altitude than use a given temperature for use in my formula. I have made a simple weather station and want to calculate density altitude. I can make a simple database that would have ISA temperature for each altitude. For example at 0ft temp is 59F and a 1000ft temp is 57, etc.
My thought was if (altitude >=0 and altitude <=1) use 59

The math formula I am using would calculate density altitude. Here it is:
String altitudeString = String((altitude + 120 * (temperature - 59)), 0); Where as "59" would be the ISA temp that changes with altitude.

Thank you so much, I am new to this and really having a great time learning.

make a look up table, for any (x,y) type calculations. a look up table is an array of constants so your altitude would be your index and your temp would be the constant. You can also use map to map a larger range to a smaller array.

unsigned char altitude;
const int temperatureLUT[] = {57, 58, 59};// etc.

//if range is larger than 3
unsigned char x = map(altitude, 0, 100, 0, 2);
int y = temperatureLUT[x];

As early as possible is a good time to abandon the String class and complicated math. Instead of Strings, use c-style strings (char arrays) and instead of complicated math which takes CPU cycles, if you have the RAM, use a look-up table. Your project will be much more responsive.

Thank you for the quick reply. I will research how to code that. Ill let you know how it turns out.

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