Skip to content

Commit 7b1b04c

Browse files
committed
fixed Nth poly
1 parent f8ac4d2 commit 7b1b04c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Source/ArduinoMenuCalibrator/ArduinoMenuCalibrator.ino

+17-11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void setup() {
5757
//Serial.setTimeout(10000); //set Serial Timeout
5858
Serial.println(F("Arduino Least Squares Fit Tool v.1.0 | Note: USE Newline or CR/LN in serial terminal program"));
5959

60+
6061
// //an IFDEF is used to activate the EEPROM defaulter if enabled
6162
// #ifdef DEFAULTTHEEEPROM
6263
// WriteDefaultEEPROM(); //run this for a vigin chip
@@ -377,7 +378,10 @@ void fabls_polynomial(unsigned int N, unsigned int n, double *px,double *py, dou
377378
{
378379
X[i]=0;
379380
for (j=0;j<N;j++)
380-
X[i]=X[i]+pow(px[j],i); //consecutive positions of the array will store N,sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
381+
{
382+
X[i]=X[i]+pow(px[j],i); //consecutive positions of the array will store N,sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n)
383+
}
384+
381385
}
382386
double B[n+1][n+2],a[n+1]; //B is the Normal matrix(augmented) that will store the equations, 'a' is for value of the final coefficients
383387
for (i=0;i<=n;i++)
@@ -460,8 +464,10 @@ void fabls_polyOutput(unsigned int N, unsigned int n, double *a, double *px, dou
460464
Serial.print(F("The fitted (order "));
461465
Serial.print(n);
462466
Serial.print(F(") Polynomial is given by:\n y ="));
467+
char arrOfSigns[N] = {0};
463468
for (int i = 0; i < (n + 1); i++) // total points is equal to n+1
464469
{
470+
arrOfSigns[i] = (a[i]<0) ? '-' : '+';
465471
if (i == 0) //supress the initial + sign in the display
466472
{
467473
Serial.print(F(" ("));
@@ -483,6 +489,7 @@ void fabls_polyOutput(unsigned int N, unsigned int n, double *a, double *px, dou
483489

484490
for (int j = 0; j < N; ++j) //for all points
485491
{
492+
486493
double y = 0; //set each term's calculation initialized at 0
487494
for (int q = 0; q < (n + 1); q++) //for all terms
488495
{
@@ -552,10 +559,8 @@ void fabls_polyOutput(unsigned int N, unsigned int n, double *a, double *px, dou
552559
writeToEEPROM( entryname, "quadratic", N+1, regressionterms, arrOfSigns, append, reportInvertedValues);
553560
}
554561
else
555-
{6
556-
562+
{
557563
writeToEEPROM( entryname, "quadratic", N+1, a, arrOfSigns, append, reportInvertedValues);
558-
559564
}
560565
// itoa(reportInvertedValues, invertedStatus, 10);
561566
// itoa(reportedConfiguredStatus, configuredStatus, 10);
@@ -1215,9 +1220,10 @@ int fitSelection(int fitChoice, uint8_t skipEntry)
12151220
{
12161221
dataEntrySelection(totalPoints);// select point entry method
12171222
}
1218-
double regCoeff[(polynomialDegree + 1)] = {0};
1223+
double* regCoeff = new double[(polynomialDegree + 1)];
12191224
fabls_polynomial(totalPoints, polynomialDegree, px, py, regCoeff); //calculate polynomial regressions
12201225
fabls_polyOutput(totalPoints, polynomialDegree, regCoeff, px, py);
1226+
delete[] regCoeff;
12211227

12221228
return 1; // successfully ran
12231229
}
@@ -1320,7 +1326,9 @@ int manualPointEntry (int i) //i is total points entered --needs to have total p
13201326
Serial.print("Input x-val");
13211327
Serial.print(i+1);
13221328
Serial.print(": ");
1323-
px[i] = (double)NumericFloatInput();
1329+
double xVal = (double)NumericFloatInput();
1330+
delay(1);
1331+
px[i] = xVal;
13241332
Serial.print(" Entered Val: ");
13251333
Serial.println(px[i]);
13261334
delay(250); //nice, easy transisitonal delay to next input
@@ -1329,7 +1337,9 @@ int manualPointEntry (int i) //i is total points entered --needs to have total p
13291337
Serial.print("Input y-val");
13301338
Serial.print(i+1);
13311339
Serial.print(": ");
1332-
py[i] = (double)NumericFloatInput();
1340+
double yVal = (double)NumericFloatInput();
1341+
delay(1);
1342+
py[i] = yVal;
13331343
Serial.print(" Entered Val: ");
13341344
Serial.println(py[i]);
13351345
delay(250);
@@ -1591,7 +1601,6 @@ void displayFitChoiceMenu()
15911601
Serial.println(F(" (5)Power - Min 3 pts, x != 0"));
15921602
Serial.println(F(" (6)Nth Order Polynomial - Min pts = order+1")); //a 2nd power requires 2 points, 3rd power requires 3, etc.
15931603
Serial.println(F(" (7)List Pts in Memory"));
1594-
Serial.println(F(" (7)Read out EEPROM"));
15951604
Serial.println(F(" (8)Manual Pt Entry"));
15961605
Serial.println(F(" (9)Delete Current Points"));
15971606
Serial.print(F(" (10)Toggle Use Cache Points on fits, current status: "));
@@ -1767,7 +1776,6 @@ void setupEEPROM()
17671776
// it will only be called if the first byte is not "1"
17681777
//FORMAT: configFlag#whereToWriteToIfAppending(will take up four bytes)#numberOfSensors( max number of sensors to 144(it most probably wont be more than that) which requires 1 byte only))
17691778
//1#0008#0
1770-
Serial.println("IN setupEEPROM function");
17711779
//EEPROM.begin();
17721780
//Configured flag is first byte
17731781
EEPROM.write(EEPROMPARTITIONOFFSET,'1');
@@ -2668,8 +2676,6 @@ void adHocPointEntry()
26682676

26692677
void loop()
26702678
{
2671-
2672-
26732679
bool dealocArrays=0;//keep arrays, flag is normal pos.
26742680
unsigned int selectedValue = 0; //initialize selection choice holder as 0
26752681
unsigned int runStatus = 0; // Status of previous function's run, if an error is returned, this will come back as zero, chose next action accordingly

0 commit comments

Comments
 (0)