'
'
'  COIL.BAS
'
'
'    DISCLAIMER: No claim is made for the accuracy or fitness of use of
'  this program.  The user is responsible for determining its accuracy and
'  fitness.
'
'    There are to major options in the program "Designing Coils" and
'  "Calculate Coil".  Generally, you want to use the "Designing Coils"
'  option it is more accurate and gives you the option to print the results.
'
'    The "Calculate Coil" option is `quick & dirty' and doesn't have all the
'  neccessary parameters to give accurate results.
'
'    To exit a subroutine, enter `e' when prompted for any given option.
'  Entering `0' or a negative number when prompted for numeric data will
'  cause the program to exit the subroutine.
'
'    This program does NOT calculate for coils with ferrite cores.  If you
'  use this program, you should have a basic understanding of coils.
'
'
  DIM Pi AS DOUBLE, Mhos AS DOUBLE, Wdiam AS DOUBLE
  DIM MicroH AS DOUBLE, Fdiam AS DOUBLE, a AS DOUBLE, c AS DOUBLE
  Pi = 3.1415926545#: Mhos = 1473061.855#: Fd$ = CHR$(13): FF$ = CHR$(12)
'
  CLS
  DO: PRINT Fd$; Fd$
    INPUT " (D)esign Coil, (C)alculate Coil or (e)xit:"; x$
    x$ = LCASE$(x$): IF x$ = "a" OR x$ = "q" OR x$ = "e" THEN EXIT DO
    IF x$ = "d" THEN GOSUB DesignC ELSE IF x$ = "c" THEN GOSUB CalcMC
  LOOP: IF p$ = "p" THEN PRINT #2, FF$;
  CLOSE #2: END
'
' * * * Design a Coil * * *
'
DesignC:
  DO: CLS : PRINT Fd$; Fd$; Fd$
    INPUT " Desired inductance in micro-Henries "; MicroH
    IF MicroH <= 0 THEN EXIT DO
    Tol = MicroH * .0025
    IF MicroH < 10 THEN Tol = MicroH * .005
    TolMin = MicroH - Tol: TolMax = MicroH + Tol
' 
    INPUT " Gauge or diameter of wire in inches"; Wdiam
    IF Wdiam <= 0 THEN EXIT DO
    IF Wdiam >= 1 THEN
      Wdiam = .46 / 1.1229283027# ^ (Wdiam + 3)
      PRINT " Wire Diameter  . . . . . . . . ";
      PRINT USING "##.####"; Wdiam; : PRINT " Inches"
    END IF
    IF Wdiam < .002 OR Wdiam > .011 THEN
      PRINT Fd$;
      PRINT "WARNING! This size of wire is not recommended for coils"; Fd$
    END IF
'
    INPUT " Diameter of coil form in inches    "; Fdiam
    IF Fdiam <= 0 THEN EXIT DO
'  
    INPUT " Maximum Length of coil "; MaxLen:
    IF MaxLen <= 0 THEN EXIT DO
    MaxLen = MaxLen + .00001
    CLS : Layers = 1: PRINT Fd$; Fd$; "Calculating ";
    SrCall = 0: x$ = " "
'
    DO: PRINT "."; : a = Wdiam * Layers + Fdiam: GOSUB DesignSR
      IF Fini > 0 AND Clength < MaxLen THEN EXIT DO
      IF Problem = 1 THEN
        Layers = Layers + 1: IF Layers > 99 THEN EXIT DO
      ELSE
        IF Clength > MaxLen THEN Layers = Layers + 1
      ELSE
        EXIT DO
      END IF
      SrCall = SrCall + 1: IF SrCall > 100 THEN Problem = 2: EXIT DO
    LOOP
    IF Turns > 9999 THEN PRINT Fd$; "ERROR! - Turn count > 10000"
    IF Problem > 0 THEN
      PRINT Fd$; " Calculation Error": BEEP
      PRINT Fd$; " Press `c' to continue"
      DO
        x$ = INKEY$:
      LOOP UNTIL x$ = "c" OR x$ = "a" OR x$ = "q" OR x$ = "e"
    ELSE
      Turns = INT(Turns): Clength = Turns * Wdiam / Layers
      Ltry = (.2 * a ^ 2 * Turns ^ 2) / (3 * a + 9 * Clength + 10 * c)
      Wlength = Turns * a * Pi: Warea = (Wdiam / 2) ^ 2 * Pi
      Resist = 1 / (Warea * Mhos) * Wlength
'
      CLS : CLOSE #2: p$ = "s": OPEN "scrn:" FOR OUTPUT AS #2
      GOSUB LresultSR: CLOSE #2: p$ = " "
      DO: PRINT Fd$
        INPUT " Print Results to Printer (y/n or e to exit"; p$
        p$ = LCASE$(p$): IF p$ = "y" OR p$ = "n" OR p$ = "e" THEN EXIT DO
      LOOP
      IF p$ = "y" THEN
        OPEN "lpt1:" FOR OUTPUT AS #2
        GOSUB LresultSR: CLOSE #2
      END IF
    END IF
    IF p$ = "e" OR x$ = "e" OR x$ = "q" THEN EXIT DO
'   
    PRINT Fd$, " Another Calculation?"
    DO: x$ = INKEY$: LOOP UNTIL x$ = "y" OR x$ = "n"
  LOOP WHILE x$ = "y"
'
RETURN
'
'    Calculate Turns
'
DesignSR:
  Tmin = 1: Tmax = 10000: c = Layers * Wdiam: Problem = 0: Fini = 0
  DO: Turns = (Tmax - Tmin) / 2 + Tmin: Clength = Turns * Wdiam / Layers
    Ltry = (.2 * a ^ 2 * Turns ^ 2) / (3 * a + 9 * Clength + 10 * c)
    IF INT(Tmax) = INT(Tmin) THEN EXIT DO
    IF Tmin >= 9999 THEN Problem = 1: EXIT DO
    IF Ltry > TolMin AND Ltry < TolMax THEN Fini = 1: EXIT DO
    IF Ltry < MicroH THEN Tmin = Turns ELSE IF Ltry > MicroH THEN Tmax = Turns
  LOOP: RETURN
'
' * * * Print Coil Results S/R
'
'
LresultSR:
'
  PRINT #2, Fd$; Fd$
  PRINT #2, " Coil Form Diameter  . . . . . ";
  PRINT #2, USING "##.####"; Fdiam; : PRINT #2, " Inches"
  PRINT #2, " Coil Length . . . . . . . . . ";
  PRINT #2, USING "##.####"; MaxLen; : PRINT #2, " Inches"
  PRINT #2, " Wire Diameter  . . . . . . . . ";
  PRINT #2, USING "##.####"; Wdiam; : PRINT #2, " Inches"
  PRINT #2, " "
  PRINT #2, " Overall Coil Diameter . . . . ";
  PRINT #2, USING "##.####"; Wdiam * Layers * 2 + Fdiam;
  PRINT #2, " Inches"
  PRINT #2, " Average Coil Diameter . . . . ";
  PRINT #2, USING "##.####"; a; : PRINT #2, " Inches"
  PRINT #2, " Depth of Coil . . . . . . . . ";
  PRINT #2, USING "##.####"; Wdiam * Layers; : PRINT #2, " Inches"
  PRINT #2, " Length of Coil. . . . . . . . ";
  PRINT #2, USING "##.####"; Clength; : PRINT #2, " Inches"
  PRINT #2, " Length of Wire (approx) . . . "; INT(Wlength / 12); "Feet";
  PRINT #2, INT((Wlength - INT(Wlength)) * 12); "Inches"
  PRINT #2, " Number of Layers. . . . . . . "; Layers
  PRINT #2, " Number of Turns . . . . . . . "; Turns
  PRINT #2, " Number of Turns per Layer . . "; INT(Turns / Layers)
  PRINT #2, " Actual Inductance . . . . . . ";
  PRINT #2, USING "###,###.####"; Ltry; : PRINT #2, " micro-Henries"
  PRINT #2, " Coil DC Resistance. . . . . . ";
  PRINT #2, USING "###,###.####"; Resist; : PRINT #2, " Ohms"; Fd$; Fd$
'
RETURN
'
' * * * Calculations for Existing Coils * * *
'
CalcMC:
  DO: PRINT Fd$
    INPUT " Calculated factor (M)icroHenries, (C)oil turns or (e)xit:"; x$
    x$ = LCASE$(x$): IF x$ = "a" OR x$ = "q" OR x$ = "e" THEN EXIT DO
    IF x$ = "m" THEN GOSUB CalcMH ELSE IF x$ = "c" THEN GOSUB CalcTurn
  LOOP: RETURN
'
'
'
CalcMH:
DO
  INPUT " Coil Form Diameter:"; Cdiam: IF Cdiam = 0 THEN EXIT DO
  Crad = Cdiam / 2
  INPUT " Coil Length:"; Clen: IF Clen = 0 THEN EXIT DO
  INPUT " Number of Turns on Coil:"; Turn: IF Turn = 0 THEN EXIT DO
  MicroH = (Crad ^ 2 * Turn ^ 2) / (9 * Crad + 10 * Clen)
  PRINT " Inductance ="; : PRINT USING "#,###,###.##"; MicroH;
  PRINT " micro-Henries"; Fd$
LOOP
RETURN
'
'
'
CalcTurn:
DO
  INPUT " Coil Form Diameter:"; Cdiam: IF Cdiam = 0 THEN EXIT DO
  Crad = Cdiam / 2
  INPUT " Coil Length:"; Clen: IF Clen = 0 THEN EXIT DO
  DO
    INPUT " Desired Inductance (micro-Henries):"; MicroH:
  LOOP UNTIL MicroH >= 0 AND MicroH < 10000000
  IF MicroH = 0 THEN EXIT DO
  Turn = SQR(MicroH * (Crad * 9 + Clen * 10) / Crad ^ 2)
  PRINT " Number of Turns on coil ="; : PRINT USING "###,###.##"; Turn;
  PRINT Fd$
LOOP
RETURN

