#!/usr/bin/env perl

## approximate number of turns to form a coil of a certain inductance
## $l need to be in microhenries

## This file Copyright 2000 <contact@gbppr.org> under the GPL
## NO WARRANTY. Please send bug reports / patches / reports.

sub Ind {
undef $_;
while (!m/^([0-9.]+)(n|N|m|M|u|U+)$/) {
        print "\nn = nanohenries\n".
	      "u = microhenries\n".
	      "m = millihenries\n\n".
	      "Enter inductance  [value][n,u,m]: ";
	chomp($_ = <STDIN>);

if (m/^([0-9.]+)(n|N|m|M|u|U+)$/) {
        $l = $1;
        $unit = $2;
}
if ($unit =~ /n/i) {
        $l = $l / 1000;
}                              
elsif ($unit =~ /u/i) {
        $l = $l;
}
elsif ($unit =~ /m/i) {
        $l = $l * 1000;
  }
 }
}        

sub Get {
undef $_; undef $val; undef $unit; undef $valu;
while (!m/^([0-9.]+)(mm|MM|cm|CM|in|IN+)$/) {
	print "\nmm = millimeters\n".
	      "cm = centimeters\n".
	      "in = inches\n\n".
	      "Enter coil $VAL  [value][mm,cm,in]: ";
	chomp($_ = <STDIN>);
}

if (m/^([0-9.]+)(mm|MM|cm|CM|in|IN+)$/) {
        $val = $1;
	$unit = $2;  
}
if ($unit =~ /cm/i) {
	$unit = "centimeters";
        $valu = $val / 2.54;
}
elsif ($unit =~ /mm/i) {
	$unit = "millimeters";
        $valu = $val / 25.4;
}            
elsif ($unit =~ /in/i) {
	$unit = "inches";
        $valu = $val;
 } 
}

print "\n\n\t\tNumber of Turns Calculator\n\n";

&Ind;

&Get($VAL = "DIAMETER");
$dia = $valu;
$diav = $val;
$diau = $unit;

&Get($VAL = "LENGTH");
$len = $valu;
$lenv = $val;
$lenu = $unit;

$ansr = sqrt (($l * ((18 * $dia) + (40 * $len))) / $dia);

printf "\n\n     Coil inductance : %.6f mH\n".
       "                     : %.6f uH\n".
       "                     : %.6f nH\n\n", $l / 1000, $l, $l * 1000;
printf "       Coil diameter : %s %s\n".
       "         Coil length : %s %s\n\n".
       " Approximately turns : %.2f\n\n", $diav, $diau, $lenv, $lenu, $ansr;
