overloading processor?

Delta_G: Are the contents of the arrays fixed where they could go into flash memory instead?

Yes, the contents of the arrays are fixed. I guess I'm not understanding how the Arduino works. I thought they were already stored in flash memory since they are part of the program.

The values are stored in flash and are copied to RAM when the program starts. The arrays are still variables and can be changed any time. The values are just initial values.

A little update, I cut the array sizes in half by using only even numbers for the temps & their corresponding pressures so things are working fine for now. Since there's a lot more code going into the finished product there's a good chance of overloading again but at least now I'm aware of the possible problem.

Thanks again to all for the help

leftyg: Yes, the contents of the arrays are fixed. I guess I'm not understanding how the Arduino works. I thought they were already stored in flash memory since they are part of the program.

The processor doesn't know that you don't intend to change them, so it puts them in RAM. There are ways of telling the compiler that these things should be left in program memory and read from there every time.

Google "Arduino PROGMEM"

That data looks to be very smooth and very low order (see attached JPG file) and I suspect you would do better to create simple low order polynomial approximations.

plot.jpg

leftyg: While we're at it I may as well ask this question also. I access this same chart in a mysql database using php and a similar function to interpolate whatever pressure number is entered to two decimals. Being new to the Arduino programming, the only way I could think to use the chart was to turn the database into arrays. Is there a better way?

If your input numbers (pressure) are linear, then you may look at the map() function. The function does integer math, so you would have to shift to support float numbers.

stowite: That data looks to be very smooth and very low order (see attached JPG file) and I suspect you would do better to create simple low order polynomial approximations.

If you're talking about a formula using the intervals from the chart I could get in the ballpark but I don't think it would be accurate enough for my purposes. I'm already dealing with the inaccuracies of the pressure sensors so i at least want to make the conversion to temperature pretty accurate.

Delta_G: The processor doesn't know that you don't intend to change them, so it puts them in RAM. There are ways of telling the compiler that these things should be left in program memory and read from there every time.

Google "Arduino PROGMEM"

Can you give me an example? I tried making the arrays constants but it didn't help.

Delta_G: Google "Arduino PROGMEM"

leftyg: Can you give me an example?

No problem. Try here.

leftyg: If you're talking about a formula using the intervals from the chart I could get in the ballpark but I don't think it would be accurate enough for my purposes. I'm already dealing with the inaccuracies of the pressure sensors so i at least want to make the conversion to temperature pretty accurate.

I have just fitted polynomials to your data. 'temp' can be approximated very accurately using a regression fit to a second order polynomial, 'r22' by a third order and 'r410a' by a forth order.

I could produce error statistics but you seem set against using a polynomial fit so there is no point.

leftyg: Can you give me an example? I tried making the arrays constants but it didn't help.

See: http://www.gammon.com.au/progmem

Using const isn't enough. That link will show you what to do. In particular see reply #3 on that page.

leftyg: the only way I could think to use the chart was to turn the database into arrays. Is there a better way?

Yes... Avoid using float an instead use long with values 10x or 100x in the array.

11.3 would be 113 or 1130 in your array. You only have to determine if any intermediate calculations overflow long max/min

[quote author=George Matthews link=msg=2376403 date=1440889417] Yes... Avoid using float an instead use long with values 10x or 100x in the array. [/quote] Floats and longs are the same size in AVR C++. This won't save any memory, which as I understand it, is the major problem here.

aarg: Floats and longs are the same size in AVR C++. This won't save any memory, which as I understand it, is the major problem here.

Perhaps I should have Googled that before making my suggestion to use long!

stowite: I have just fitted polynomials to your data. 'temp' can be approximated very accurately using a regression fit to a second order polynomial, 'r22' by a third order and 'r410a' by a forth order.

I could produce error statistics but you seem set against using a polynomial fit so there is no point.

I'm not dead against anything. I'm only speaking from a perspective of the limited knowledge & understanding i have in programming. I don't even know what "polynomials" is, so I am not against a formula using them if it was accurate. Looking at the chart intervals I just didn't see a how it could be accurate. If you would like to share an example of what you came up with I'd appreciate seeing it. It would be great if it works for my purposes but even if it didn't I'd still like to learn about the use of polynomials.

For an explanation of polynomials take a look at Polynomial - Wikipedia .

Your three data sets can be approximated by low order polynomials and the coefficients of these polynomials are dependent on the approximation criteria used. If one minimizes the mean squared error one will get one set of coefficients and if one minimizes the mean modulus error one will get another set of coefficients. I minimized the mean square error to produce

temp = -4.000000000000e+01 * n^2 + 1.000000000000e+00 * n + 0.000000000000e+00

r22 = -2.481298946362e-03 * n^3 + 4.497414533268e-01 * n^2 + 2.797375277604e-03 * n + 2.846855202869e-05

r410 a = 9.665115991248e+00 * n^4 + 7.746002147311e-01 * n^3 + 2.846743410771e-03 * b^2 + 5.633949163074e-05 * n + -2.953171222656e-08

These coefficients were calculated using Java double and have more precision that can be exploited on the Arduino but it does not hurt to keep the precision at this stage. The compiler will round them as appropriate.

A simple way of using these is to create a Polynomial class that is constructed with these coefficients and has a method to evaluate them for a particular ‘n’. As a starting point see

class Polynomial
{
  public:
    Polynomial(double* poly, uint8_t order): poly_(poly), order_(order)
    {
    }

    const double evaluate(const double x) const
    {
      double r = poly_[order_];
      for (int i = order_ - 1; i >= 0; i--)
      {
        r = r * x + poly_[i];
      }
      return r;
    }

  private:
    uint8_t order_;
    double* poly_;
};

static double tempPolyCoefs[] =
{
  -4.000000000000e+01,
  1.000000000000e+00,
  0.000000000000e+00
};
static Polynomial tempPoly(tempPolyCoefs, 2);

static double r22PolyCoefs[] =
{
  -2.481298946362e-03,
  4.497414533268e-01,
  2.797375277604e-03,
  2.846855202869e-05
};
static Polynomial r22Poly(r22PolyCoefs, 3);

static double r410aPolyCoefs[] =
{
  9.665115991248e+00,
  7.746002147311e-01,
  2.846743410771e-03,
  5.633949163074e-05,
  -2.953171222656e-08
};
static Polynomial r410aPoly(r410aPolyCoefs, 4);

void setup() {
  Serial.begin(115200);

  for (int i = 0; i < 200; i++)
  {
    char tempStr[20];
    dtostrf(tempPoly.evaluate(i), 1, 1, tempStr);
    char r22Str[20];
    dtostrf(r22Poly.evaluate(i), 1, 1, r22Str);
    char r410aStr[20];
    dtostrf(r410aPoly.evaluate(i), 1, 1, r410aStr);

    char buffer[80];
    sprintf(buffer, "%d %s %s %s\n",  i, tempStr, r22Str,  r410aStr);
    Serial.print(buffer);
  }
}

void loop() {

}

Obviously this implementation takes fewer resources than your implementation.

You have not said how you intend to use the data but I am betting that what you want is to actually compute ‘r22’ and ‘r410a’ for a given temperature. One could do this with the polynomials I have provided BUT it would be much better to create polynomials for ‘r22’ and ‘r410a’ with the independent variable being temperature. I will let you do this if you decide you need it; as I did you might want to make the use of http://www.arachnoid.com/polysolve/ .

Whether or not these approximations are accurate enough for your application depends on the application.

Wow stowite,

I have no idea how you came up with those numbers but I’m going to try & learn a little about polynomials & see if things become clearer.
Thanks agian