Hi! I'm currently attempting to take a JS program (visible on codepen.io) and bring it into the Arduino C/C++ environment, whatever it's called officially.
I'm pasting the Arduino program I managed to come up with, you can see it's just meant to take some dummy numbers (already defined variables that'd normally be user-entered) and perform the calculations, then print them into the serial monitor for me to double check.
Right now I'm getting a lot of zeros and I'm certain that most of the problem arises from the quadratic equation where the bobbin's core size and the winding area are calculated. And also stuff that references back to that quadratic pile, like this:
COIL.layers = ((q.r1 < 0) ? q.r2 : q.r1) ;
I just don't know how that's supposed to look in C, I have it written out as a bool statement...regardless, it's no help if I'm not getting any meaningful answers out of the quadratic equation.
Anyway, I'm just hoping someone here who did better in math class can help me work this one out...it's been bothering me for a while now
#include <math.h>
/* results */
int rWinds[5];
int rLayers[5];
int rMaxLayers[5];
int rWPL[5];
int rMaxWinds[5];
int rWPLInch[5];
/* variables - bobbin & wire
normally, these would be user-entered in a form - for this example, we just have some default numbers to compare against the codepen.io example
*/
float cLength = 1.315;
float cWidth = 0.1875;
float cHeight = 0.34;
float cFlange = 0.975;
float cGauge = 42;
int cOhms = 5250;
int cFill[5] = {62,72,78,90,100};
float cDiameter = 0.0030;
float bDiameter = 0.0025;
float cOhms1000ft = 1659.261741783075;
int roots[2];
/* Currently Un-used wire specs */
float ga24[4] = {24, 0.0201, 0.0213, 0.0233};
float ga25[4] = {25, 0.0179, 0.0190, 0.0199};
float ga26[4] = {26, 0.0159, 0.0170, 0.0178};
float ga27[4] = {27, 0.0142, 0.0153, 0.0161};
float ga28[4] = {28, 0.0126, 0.0137, 0.0144};
float ga29[4] = {29, 0.0113, 0.0123, 0.0130};
float ga30[4] = {30, 0.0100, 0.0109, 0.0116};
float ga31[4] = {31, 0.0089, 0.0097, 0.0105};
float ga32[4] = {32, 0.0080, 0.0088, 0.0095};
float ga33[4] = {33, 0.0071, 0.0078, 0.0084};
float ga34[4] = {34, 0.0063, 0.0069, 0.0075};
float ga35[4] = {35, 0.0056, 0.0062, 0.0067};
float ga36[4] = {36, 0.0050, 0.0056, 0.0060};
float ga37[4] = {37, 0.0045, 0.0050, 0.0055};
float ga38[4] = {38, 0.0040, 0.0045, 0.0049};
float ga39[4] = {39, 0.0035, 0.0039, 0.0043};
float ga40[4] = {40, 0.0031, 0.0035, 0.0038};
float ga41[4] = {41, 0.0028, 0.0031, 0.0034};
float ga42[4] = {42, 0.0025, 0.0028, 0.0030};
float ga43[4] = {43, 0.0022, 0.0025, 0.0027};
float ga44[4] = {44, 0.0020, 0.0022, 0.0025};
float ga45[4] = {45, 0.00176, 0.00192, 0.00215};
float ga46[4] = {46, 0.00157, 0.00173, 0.00196};
float ga47[4] = {47, 0.00140, 0.00158, 0.00178};
float ga48[4] = {48, 0.00124, 0.00140, 0.00155};
float ga49[4] = {49, 0.00111, 0.00124, 0.00139};
float ga50[4] = {50, 0.00099, 0.00113, 0.00128};
float ga51[4] = {51, 0.00088, 0.00103, 0.00117};
float ga52[4] = {52, 0.00078, 0.00093, 0.00107};
float ga53[4] = {53, 0.00070, 0.00079, 0.00090};
float ga54[4] = {54, 0.00062, 0.00070, 0.00082};
float ga55[4] = {55, 0.00055, 0.00064, 0.00075};
void setup() {
Serial.begin(115200);
};
/* Constants */
float inchesPerMeter = 1000.0 / 25.4 ; /* 39.37007874015748 */
int INCHES_PER_1000FT = 1000 * 12 ; /* 12000 */
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 ;
};
void quadratic(int a,int b,int c){
int disc = b*b-4*a*c;
roots[0] = disc;
if (disc > 0){
if (b == 0){
int r = fabs(0.5 * sqrt(disc) / a);
roots[1] = -r;
roots[2] = r;
} else {
int sgnb;
if (b > 0){sgnb = 1;} else {sgnb = -1;}
int temp = -0.5 * (b + sgnb * sqrt(disc));
int r1 = temp / a ;
int r2 = c / temp ;
if (r1 < r2)
{
roots[1] = r1 ;
roots[2] = r2 ;
} else
{
roots[1] = r2 ;
roots[2] = r1 ;
}
}
Serial.print("roots[0] (disc) = ");
Serial.println(roots[0]);
Serial.print("roots[1] (r1) = ");
Serial.println(roots[1]);
Serial.print("roots[2] (r2) = ");
Serial.println(roots[2]);
}
if (disc == 0){
roots[1] = -0.5 * b / a ;
roots[2] = -0.5 * b / a ;
} else {
//do nothing
};
};
void calculateResults(){
for (int i = 0; i < 5; i++) {
float a2,b2,c2;
float lenInches ;
float wireDia = cDiameter;
float fill = cFill[i]/100;
lenInches = cOhms / (R1000ftA / INCHES_PER_1000FT) ;
wireDia = wireDia / fill ;
a2 = PI * wireDia ;
b2 = (2.0 * cLength) + ((PI - 2.0) * cWidth) + (PI * wireDia) ;
c2 = -((lenInches * wireDia) / cHeight) ;
quadratic(a2,b2,c2) ;
if (roots[0] < 0)
{
//trace("error: roots are unreal\n") ;
} else {
if (roots[1] < 0) {rLayers[i] = roots[2];
} else {rLayers[i] = roots[1];
};
};
/*MaxLayers*/
rMaxLayers[i] = ((cFlange - cWidth) / 2.0) / (cDiameter / (cFill[i]/100));
/*WindsPerLayer - rWPL*/
rWPL[i] = round(cHeight / (cDiameter / cFill[i]));
rWPLInch[i] = round(rWPL[i] * (1.0 / cHeight));
rWinds[i] = round(rWPL[i] * rLayers[i]);
rLayers[i] = round(rLayers[i]);
rMaxWinds[i] = round(rMaxLayers[i] * rWPL[i]);
/* if (COIL.layers > COIL.maxlayers) RED */
};
};
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("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("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("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("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("rMaxWinds[4] = ");
Serial.println(rMaxWinds[4]);
delay(25000);
/*
int rWinds[4];
int rLayers[4];
int rMaxLayers[4];
int rWPL[4];
int rMaxWinds[4];
int rWPLInch[4];
*/
};