//FinalExamProgram_AC.cpp //Andy Cobaugh //5-10-02 //Purpose: Provide the amateur Tesla coil builder with a convenient way to design a Tesla coil #include #include //needed for islower and toupper functions #include //file I/O #include //sqrt function #include //output formatting #include //getch() function #include"vector.h" //vector class for primary coil data void display_menu(int design_done); float toroid_capacitance(float dia, float cord); //declare a structure that will hold specifications for a transformer struct xfmr { float ma; //current float kv; //voltage float power; //power in watts float matched_cap; //matched capacitor value float arc_length; //theoretical arc length }; //PI declared for use in calculations double const PI = 3.14159; int main() { //*NOTE: Most variables are self-explanitory xfmr my_xfmr; //declare a variable as type xfmr //variables for secondary coil float sec_length; float sec_diameter; float sec_wire_dia; float sec_turns; float sec_wire_length; float sec_frequency; //variables for primary coil float pri_sec_spacing; float pri_edge_to_edge; float pri_turns; float pri_wire_dia; float pri_wire_total; vector pri_inductance(1); //inductance at each turn in micro-henrys vector pri_total_wire_length2(1); //length of wire in each turn vector pri_radius(1); //radius of each turn float tank_cap; //tank circuit capacitance in micro-farads //toroid variables float toroid_dia; float toroid_cord; float toroid_cap; int menu_choice; //user's choice in beginning of program and for menu int sec, pri, tran, tor, cap; //ints to keep track of what the user has done int design_total; //total number of different menu options the user has selected int i; //standard counter variable used for all loops float average_pri_radius; //average radius used when calculating inductance int pri_turns2; //turns used during each indutance calculation float pri_width; //width used during each indutance calculation char filename[13]; //filename used for file I/O char user_name[80] = ""; //the user's name ifstream infile; //file input pointer ofstream outfile; //file output pointer int Ch; //used with getch() //display an intro to the program and some general guidlines cout<<"Welcome to the Tesla Coil Designer!"<>menu_choice; //if it is an old design, open the file and store the variables if (menu_choice == 2) goto line200; system("CLS"); cin.ignore(80, '\n'); //flush input stream so it doesn't flow into the nect cin statement cout<<"Enter your name: "; cin.get(user_name, 80); //input the user's name cin.ignore(80, '\n'); //ignore any characters after the user's name for (i=0; i<=80; i++) //loop as many times as there are characters (80) { //if the current positon is the first character of a name or the first character of the string if((islower(user_name[i]) && user_name[i-1]== ' ') || i==0) { user_name[i] = toupper(user_name[i]); //use the toupper function to convert the character to uppercase } } system("CLS"); do //continuous loop { //add up total num of different options selected design_total = sec + pri + tran + tor + cap; //display the menu telling it whether it will display option 6 which can only be used when design_total is 5 display_menu(design_total); do { //prompt for the menu selection cout<>menu_choice; //invalid selection checking if (menu_choice <1 || menu_choice > 9 || (menu_choice == 6 && design_total != 5)) { system("CLS"); cout<<"Invalid Selection. Please choose another. \n\n"; display_menu(design_total); } //loop as long as it takes to get a valid selection }while(menu_choice < 1 || menu_choice > 9 || (menu_choice == 6 && design_total != 5)); switch (menu_choice) { //xfmr case 1: system("CLS"); //prompt for all transformer specs cout<<"Transformer voltage in kilovolts (KV) : "; cin>>my_xfmr.kv; cout<>my_xfmr.ma; //calculate power in watts my_xfmr.power = my_xfmr.kv * my_xfmr.ma; //calculate the matched capacitor value my_xfmr.matched_cap = 1/(2 * PI * (my_xfmr.kv / my_xfmr.ma) * .00006) / 1000000; //calculate theoretical arc length my_xfmr.arc_length = sqrt(my_xfmr.power) * 1.7; system("CLS"); tran = 1; //transformer option has been selected break; //secondary coil case 2: //enter all secondary coil specs system("CLS"); cout<<"Secondary coil length in inches (in): "; cin>>sec_length; cout<>sec_diameter; cout<>sec_wire_dia; //calculate number of turns on the secondary sec_turns = sec_length / sec_wire_dia; //calculate length of wire used on secondary sec_wire_length = sec_turns * sec_diameter * PI / 12; //calculate the resonant frequency of the secondary sec_frequency = 300000 / (sec_turns * sec_diameter * PI * (.9144 / 36) * 4); system("CLS"); sec = 1; //secondary coil option has been selected break; //primary coil case 3: //enter primary coil specs system("CLS"); cout<<"Space between bottom of secondary coil and inner turn of primary in inches (in): "; cin>>pri_sec_spacing; cout<>pri_wire_dia; cout<>pri_edge_to_edge; cout<<"Number of turns in the primary coil (whole number): "; cin>>pri_turns; //*NOTE: Vectors are used with turn 1 being position 1 //resize the radius and inductamce vector to the appropriate size (pri_turns) pri_radius.resize(pri_turns + 1); pri_inductance.resize(pri_turns + 1); //calculate radius at each turn and store in pri_radius for(i = 1; i <= pri_turns; i++) pri_radius[i] = (i - 1) * pri_wire_dia + sec_diameter / 2 + pri_sec_spacing + (i - 1) * pri_edge_to_edge; //calculate inductance at each turn and store in pri_inductance for(i = 1; i<= pri_turns; i++) pri_inductance[i] = ( ((pri_radius[1] + pri_radius[i]) / 2) * ((pri_radius[1] + pri_radius[i]) / 2) * i * i ) / (8 * ((pri_radius[1] + pri_radius[i]) / 2) + 11 * (pri_radius[i] - sec_diameter / 2 - pri_sec_spacing)); //calculate total wire length of primary for(i = 1; i<= pri_turns; i++) pri_wire_total = 2 * PI * pri_radius[i]; system("CLS"); pri = 1; //primary coil option has been selected break; //tank circuit capacitor case 4: //enter primary capacitor value used system("CLS"); cout<<"The resonant capacitor value for your transformer is "<>tank_cap; system("CLS"); cap = 1; //capacitor option has been selected break; //toroid case 5: //enter dimensions of toroid used system("CLS"); cout<<"Enter the outside diameter of your toroid in inches (in): "; cin>>toroid_dia; cout<>toroid_cord; //call function to calculate the toroid capacitance toroid_cap = toroid_capacitance(toroid_dia, toroid_cord); system("CLS"); tor = 1; //toroid option has been selected break; //display results case 6: system("CLS"); //format output to begin with (setpresicion is # of places to right of //decimal point when ios::fixed is used cout.setf(ios::fixed); cout<= ((1/(2 * PI * sec_frequency / 1000 * tank_cap)) / (2 * PI * sec_frequency / 1000))) break; } //display tapping point cout<<"Primary Coil tapped between turn "<>filename; //open file for output outfile.open(filename, ios::out); if (outfile) //if there was no problem opening the file { outfile<>filename; //open file for input infile.open(filename, ios::in); if (infile) //if there was no error in opening file { infile.get(user_name, 80); infile>>my_xfmr.ma; infile>>my_xfmr.kv; infile>>my_xfmr.power; infile>>my_xfmr.matched_cap; infile>>my_xfmr.arc_length; infile>>sec_length; infile>>sec_diameter; infile>>sec_wire_dia; infile>>sec_turns; infile>>sec_wire_length; infile>>sec_frequency; infile>>pri_sec_spacing; infile>>pri_edge_to_edge; infile>>pri_turns; infile>>pri_wire_dia; infile>>pri_wire_total; infile>>tank_cap; infile>>toroid_dia; infile>>toroid_cord; infile>>toroid_cap; //since the options have previously been selected, make it so sec = 1; pri = 1; tran = 1; tor = 1; cap = 1; //vectors are troublesome to store in files, so they are calculated //after the rest of the data has been read from the file pri_radius.resize(pri_turns + 1); pri_inductance.resize(pri_turns + 1); for(i = 1; i <= pri_turns; i++) pri_radius[i] = (i - 1) * pri_wire_dia + sec_diameter / 2 + pri_sec_spacing + (i - 1) * pri_edge_to_edge; for(i = 1; i<= pri_turns; i++) pri_inductance[i] = ( ((pri_radius[1] + pri_radius[i]) / 2) * ((pri_radius[1] + pri_radius[i]) / 2) * i * i ) / (8 * ((pri_radius[1] + pri_radius[i]) / 2) + 11 * (pri_radius[i] - sec_diameter / 2 - pri_sec_spacing)); infile.close(); //close the file system("CLS"); } else //if there was an error opening the file { cout<