Hello - I've been having problems with a little Javascript program that I'm trying to get functioning on an Arduino, but I cannot figure out why the math keeps coming out wrong.
Anyway, here's the JS version of the application on codepen.io - just ignore the jQuery styling stuff at the top.
And here is a thread that has gone deep into the task (and still turns out the wrong answers).
You will see that I have already made a translation that should theoretically be turning out the correct numbers, but unfortunately that isn't the case.
What I would like is for someone to make this application in the Arduino IDE, in such a way that it turns out the same exact numbers. I would like the math do be done in the serial readout, and I would like it to be done using some fixed user input variables rather than bringing all those tables over.
You can see a good example here in my own program - unfortunately, the math is incorrect. If you can take this program and make it work, that's perfectly acceptable too. As long as it's turning out the same exact results for the same settings (42awg heavy insulation):
#include <math.h>
float roundoff(float parnumber, int parplaces)
{
float number = parnumber - floor(parnumber);
number = number * pow(10, parplaces);
number = round(number);
number = number / pow(10, parplaces);
number = number + floor(parnumber);
return number;
}
struct quadraticRoot {
float discriminant;
float r1;
float r2;
};
/* results */
float rWinds[5];
float rLayers[5];
float rMaxLayers[5];
float rWPL[5];
float rMaxWinds[5];
float rWPLInch[5];
float cLength = 1.315;
float cWidth = 0.1875;
float cHeight = 0.34;
float cFlange = 0.975;
float cGauge = 42;
float cOhms = 5250;
const float cFill[5] = {0.62, 0.72, 0.785, 0.907, 1.00};
float cDiameter = 0.0030;
float bDiameter = 0.0025;
float cOhms1000ft = 1659.2617;
float calcWindsPerLayer(float cHeight, float cDiameter, float fill)
{
return (cHeight / (cDiameter / fill));
}
void setup() {
Serial.begin(115200);
circularArea();
Rin();
R1000ft();
};
/* Constants */
const float inchesPerMeter = 1000.0 / 25.4 ; /* 39.37007874015748 */
const float INCHES_PER_1000FT = 1000 * 12 ; /* 12000 */
const float COPPER_RESISTIVITY = 1.724e-8 ; /* 0.00000001724 */
float circularAreaA;
float RinA;
float R1000ftA;
void circularArea()
{
float rad = bDiameter / 2.0 ;
circularAreaA = rad * rad * 3.1415926 ;
};
void Rin ()
{
RinA = COPPER_RESISTIVITY / (12.0 * circularAreaA) ;
};
void R1000ft()
{
R1000ftA = (COPPER_RESISTIVITY * inchesPerMeter * INCHES_PER_1000FT) / circularAreaA ;
};
struct quadraticRoot quadratic (float a, float b, float c)
{
float disc = b * b - 4 * a * c;
quadraticRoot roots = {disc, 0, 0};
if (disc > 0)
{
if (b == 0)
{
float r = abs (0.5 * sqrt(disc) / a);
roots.r1 = -r;
roots.r2 = r;
}
else
{
float sgnb = (b > 0 ? 1 : -1);
float temp = -0.5 * (b + sgnb * sqrt(disc));
float r1 = temp / a ;
float r2 = c / temp ;
if (r1 < r2)
{
roots.r1 = r1 ;
roots.r2 = r2 ;
}
else
{
roots.r1 = r2 ;
roots.r2 = r1 ;
}
}
}
else if (disc == 0)
{
roots.r1 = -0.5 * b / a ;
roots.r2 = -0.5 * b / a ;
}
return roots;
};
void calculateResults(){
for (int i = 0; i < 5; i++) {
float a;
float b;
float c;
float lenInches ;
float fill = cFill[i];
float wireDia = cDiameter / fill;
lenInches = cOhms / (R1000ftA / INCHES_PER_1000FT) ;
a = PI * wireDia ;
b = (2.0 * cLength) + ((PI - 2.0) * cWidth) + (PI * wireDia) ;
c = -((lenInches * wireDia) / cHeight) ;
quadraticRoot q = {a,b,c};
if (q.discriminant < 0)
{
//do nothing
} else {
rLayers[i] = ((q.r1 < 0) ? q.r2 : q.r1);
rMaxLayers[i] = ((cFlange - cWidth) / 2.0) / (cDiameter / (cFill[i]/100));
rWPL[i] = cHeight / (wireDia / cFill[i]);
rWPLInch[i] = rWPL[i] * (1.0 / cHeight);
rWinds[i] = rWPL[i] * rLayers[i];
rMaxWinds[i] = rMaxLayers[i] * rWPL[i];
};
};
};
void loop() {
delay(100);
calculateResults();
Serial.print("--------------");
Serial.print("Loose | Scatter - 62%");
Serial.print("--------------");
Serial.print("rWinds[0] = ");
Serial.println(rWinds[0]);
Serial.print("rLayers[0] = ");
Serial.println(rWinds[0]);
Serial.print("rMaxLayers[0] = ");
Serial.println(rMaxLayers[0]);
Serial.print("rWPL[0] = ");
Serial.println(rWPL[0]);
Serial.print("rWPLInch[0] = ");
Serial.println(rWPLInch[0]);
Serial.print("rMaxWinds[0] = ");
Serial.println(rMaxWinds[0]);
Serial.print("--------------");
Serial.print("Tight | Scatter - 72%");
Serial.print("--------------");
Serial.print("rWinds[1] = ");
Serial.println(rWinds[1]);
Serial.print("rLayers[1] = ");
Serial.println(rWinds[1]);
Serial.print("rMaxLayers[1] = ");
Serial.println(rMaxLayers[1]);
Serial.print("rWPL[1] = ");
Serial.println(rWPL[1]);
Serial.print("rWPLInch[1] = ");
Serial.println(rWPLInch[1]);
Serial.print("rMaxWinds[1] = ");
Serial.println(rMaxWinds[1]);
Serial.print("--------------");
Serial.print("Loose | Oscillate - 78%");
Serial.print("--------------");
Serial.print("rWinds[2] = ");
Serial.println(rWinds[2]);
Serial.print("rLayers[2] = ");
Serial.println(rWinds[2]);
Serial.print("rMaxLayers[2] = ");
Serial.println(rMaxLayers[2]);
Serial.print("rWPL[2] = ");
Serial.println(rWPL[2]);
Serial.print("rWPLInch[2] = ");
Serial.println(rWPLInch[2]);
Serial.print("rMaxWinds[2] = ");
Serial.println(rMaxWinds[2]);
Serial.print("--------------");
Serial.print("Tight | Oscillate - 90%");
Serial.print("--------------");
Serial.print("rWinds[3] = ");
Serial.println(rWinds[3]);
Serial.print("rLayers[3] = ");
Serial.println(rWinds[3]);
Serial.print("rMaxLayers[3] = ");
Serial.println(rMaxLayers[3]);
Serial.print("rWPL[3] = ");
Serial.println(rWPL[3]);
Serial.print("rWPLInch[3] = ");
Serial.println(rWPLInch[3]);
Serial.print("rMaxWinds[3] = ");
Serial.println(rMaxWinds[3]);
Serial.print("--------------");
Serial.print("Perfect | Oscillate - 100%");
Serial.print("--------------");
Serial.print("rWinds[4] = ");
Serial.println(rWinds[4]);
Serial.print("rLayers[4] = ");
Serial.println(rWinds[4]);
Serial.print("rMaxLayers[4] = ");
Serial.println(rMaxLayers[4]);
Serial.print("rWPL[4] = ");
Serial.println(rWPL[4]);
Serial.print("rWPLInch[4] = ");
Serial.println(rWPLInch[4]);
Serial.print("rMaxWinds[4] = ");
Serial.println(rMaxWinds[4]);
delay(25000);
};