# Arduino magnetic declination with GPS

hi everybody, i try to do a code to calculate with lattitude, longitude and time from a GPS the magnetic declination. I found some java code on the internet and in convert it to arduino, but i am blocked with the multiple array can anybody help me ? (this is just the code for magnetic declination not the gps parser).magDec2010 and 2020 are in array tab of 72 columns and 35 rows ... :) you can find it here : http://www.satellite-calculations.com/Satellite/scripts/magneticdeclination.online.sat.calc :

``````    int interval = 5;
int timeA,timeB, timeNow;

double interpolate(double x1, double x2, double y1, double y2,double xn) {
double m,b;
double q = x2-x1;
if(q != 0.0) {
m = (y2-y1)/q;
} else {
m = 1e99;
}
b = y1-(x1*m);
return (m*xn)+b;
}

double getArrayVal(double array[35][72],int y,int x) {

double v = array[(y % 170)/interval][(x % 360)/interval];
return v;
}

double getLatVal(double array[35][72],int la1,int la2,int ln,double dla) {
double y1 = getArrayVal(array,la1,ln);
double y2 = getArrayVal(array,la2,ln);
return interpolate(la1,la2,y1,y2,dla);
}

double get2dVal(double array[35][72],int la1, int la2,int ln1,int ln2,double dla, double dln) {
double y1,y2;
y1 = getLatVal(array,la1,la2,ln1,dla);
y2 = getLatVal(array,la1,la2,ln2,dla);
return interpolate(ln1,ln2,y1,y2,dln); // interpolate in 2D
}

double getDeclination(double la, double ln) {
la += 85;
ln += 180;
double resA,resB;
int la1 = (int) la;
la1 = (la1/interval) * interval;
int la2 = la1+interval;
if(la2 >= 170) {
la1 -= interval;
la2 -= interval;
}
int ln1 = (int) ln;
ln1 = (ln1/interval) * interval;
int ln2 = ln1 + interval

if(ln2 >= 360) {
ln2 -= interval;
ln1 -= interval;
}
resA = get2dVal(magDec2010,la1,la2,ln1,ln2,la,ln);
resB = get2dVal(magDec2020,la1,la2,ln1,ln2,la,ln);
return interpolate(timeA,timeB,resA,resB,timeNow);
}

}*/

void setup()
{}
void loop()
{}
``````

You won't get that to work this way:

``````double array[35][72]
``````

This uses more memory (about 10kB) than any Arduino has (2-8kB). You might get it to run if you store the arrays in PROGMEM (http://arduino.cc/en/Reference/PROGMEM) and access it through the appropriate function calls. Both arrays are 20kB itself, leaving not much space for the rest of your program so you might have to consider upgrading to a Mega2560. You might have to do some pointer calculations. Floats (double is a float on the AVR platform) use 4 bytes.

BTW: that code is not Java but Python.