//Oct 23, 1999
//I have been hanging on to this source for a long time.
//and I tend not to
//want to give away something I work hard at.  But here it
//is.  You are free do what you may with it....but one thing...
//If it helps you in coming up with a cool encoder reading
//program I would like to receive a copy.
//Below there is some text talking about that this is a
//dll for Visual Basic...  It is not!..   When I started this program
//that is what I was trying but it ended up being just a c++
//dos program.  It was written with Microsoft Visual C++ version 1.5
//You will probably have to do some messing with it to get it to
//compile in newer or other c++ compilers.  The first part of
//the file tells helpful info learned while writing the program.  
//You will discover that I cannot spell....but compilers don't care as 
//long as you consistenaly missell. My screen was set at 1280x1024 
//while writing....and I seem to make the lines very long...  
//So be sure you have line wrap shut
//off with whatever you use to view this code.
//Steve Lindsay  slindsay@lindsayengraving.com

/* November 25, 2000   AGE1
After having received and used this program (version A), I am attempting to make some minor changes/additions to increase
the functionality for me (and others if they desire). The changes that I am trying to accompolish are:

	1.  Add a machine name to the top of the screen as I intend to use the same PC and monitor for a readout
	    and have a switchbox for switching between machines. This will require "Manual" editing of the machine
	    name in the origional cfg file
	    At this time, I intend to have a DOS screen driven menu to copy the intended cfg file to the
	    default one and then load the program. A concious decision has been made NOT to include 
	    a menu driven config file input as I or others may want a different naming convention. If 
	    at some time my programing skills become sufficient enough to allow program naming and retrevial,
	    I may attempt that also.
	2.	As there are two versions of the program DRO40 and DRO40A, I hope to combine all functionality
		into one so the user can simply toggle the proper bit in the control port to allow one 
		program to be used on various machines. (See Steves Discussion on how parallel ports are not created equal)
	3.	Possible LOOOONG term may be to add a fourth encoder?

After trying to compile in Version 5.0, I gave up and dug out my old copy of 1.52 to compile the program.
NOTE: If I can remember, any changes made to the original cpp (DRO40.cpp) file obtained from Steve will be comented with the usual
notation of AGE1

I can be reached at axtein@surfsouth.com

*/ 


 /*
This is a dll.  It will send a value to the lpt port. 
The files that go this are this file inoutdll.cpp and inoutdll.def 
 
Here are two declares that work for Visual Basic: 
Declare Function InPort Lib "inoutdll.dll" (ByVal PortNum%) As Integer
Declare Sub OutPort Lib "inoutdll.dll" (ByVal PortNum%, ByVal ValueOut%)

Here is how to use them in the Visual Basic program code.
OutPort 888, 4          //send a 4 to the lpt1 port
Value = InPort(888)     //read the lpt1 port and put it in Value 


 				            data	status		control 
lpt1 addresses in decimal		888		889			890
lpt2 addresses in decimal		632		633			634    
              
          ***************************************************     
                 What the 25 pins do on a lpt port
  
1	STROBE  			(CONTROL)
2	DATA BIT 0
3	DATA BIT 1
4 	DATA BIT 2
5	DATA BIT 3
6	DATA BIT 4
7	DATA BIT 5
8	DATA BIT 6
9	DATA BIT 7
10	ACKNOWLEDGE			(STATUS)
11	BUSY				(STATUS)
12	PE (OUT OF PAPER)	(STATUS) 
13	PRINTER ON LINE		(STATUS)
14	AUTO LINEFEED		(CONTROL)
15	PRINTER ERROR		(STATUS)
16	INITIALIZE PRINTER	(CONTROL)
17	SELECT\DESELECT 	(CONTROL)
18-25 UNUSED OR GROUND      

 I can turn the data pins on or off and read them.  Also with the data pins I can
 add power to them and read that.
 With the control pins I can turn them on or off and read that but I can not
 add power and read that there is power there.
 With the status pins I can not figure out any good use for.  I can not change them.



      How two pins go on and off for a quaduture encoder
		binary		decimal
		cw ccw		cw	ccw
		00 00       0	0
		10 01       2   1
		11 11       3   3
		01 10       1	2
		00 00       0   0
  
_____________________________________________________________________________________
                 BITWISE STUFF
  
you can use the bitwise and (&) to a decimal number to learn if a certain bit
is 0 or 1.  

binary counting places are as follows (like with base 10 it is 1s 10s 100s 1000s)
1=1
10=2
100=4
1000=8
10000=16
100000=32
1000000=64
10000000=128  

to use the bitwise and (&) do this:
Like if you have a
decimal  236 (11101100)
and you want to know what the fifth bit over is do this:

check=236&16;
if (check == 0) it is a 0 otherwise it is a one
			   //check will not hold a one. It will only hold a zero and if not you assume
			   //it is a one.

to write back and just change a certain bit you can do this after you know the current decimal number
Like with the above example if you want to change the fifth bit from a 0 to a 1 do this;

change=236+16 now it will be a decimal  
252 or in bits: 11111100   

of course to change the same bit from a 1 to a 0 just subtract 16 from the decimal
  
 *****************************************************************************************
									INTERRUPT STUFF
Most computers lpt1 port is irq7 (bit 7) in the interrupt mask address (8259 chip) (decimal 33)(hex 21) 
Older computer with different types of video cards like cga or ega have there lpt1 irq somewhere else.									


Typical Assignments base addresses

Addr	MDPA   no MDPA
 0x3BC	LPT1	n/a	Monochrome Display and Printer Adapter (MDPA)
 0x378	LPT2	LPT1	Primary Printer Adapter
 0x278	LPT3	LPT2	Secondary Printer Adapter

Name	MDPA	no MDPA       
 LPT1	0x3BC	 0x378
 LPT2	0x378	 0x278
 LPT3	0x278	  n/a

Typicaly irqs are assigned like this
lpt1 = irq7
lpt2 = irq5
lpt3 = ?

Example to turn on the 8259 chip so it will use irq5. You have to turn off the mask bit 5
at base address of decimal 33 from a 1 to a zero.  REMEMBER the first bit is bit 0 up to bit 7
that makes 8 bits in all.  Be sure to first find out what all the bits are and just change bit 5 though
or you will turn off or on some other irqs the will screw up the puter.


It seems the only way I can keep the lpt port from I think going into a non bidirectoinal mode while changeing
the lpt base+2 (the control base address) is when I send the _outp to that address turning on (1) the irq enable
ack (pin 10) (fourth bit over) I have to send a decimal with a zero at the bidirectinal control bit (bit 5) even
though it is suppose to be biodirectional when it is a 1.  I doesn't read a 1 after you write a 0 to it but if you
write a 1 to it it will act like it is in unidirectional mode and like the data pins will all be on and you cannot
reset them to zerro.  Also it they won't take input it will just read 255 because they are all stuck on.
This may be that it is just my particular lpt port.  This problem should be tested on several cards and make
sure they are all doing this samething before in case the software should be slightly different to accomidate the 
way others work.								
*/  
/*Added by AGE on 11-25-00 */


  
#include <dos.h>
#include <stdio.h>                                                
#include <conio.h>    
#include <string.h>
#include <stdlib.h>  
#include <iostream.h>  
#include <graph.h>        
#include <bios.h> 
#include <iomanip.h> 
#include <fstream.h> 

//#include <time.h>

#define ESC    27
#define PAGEDOWN 81
#define PAGEUP  73
#define DOWNARROW 80
#define UPARROW 72 
//#define interrupt_address 0x0f //interrupt decimal 15 (irq 7) the printer 1 ack pin
int interrupt_address=15;//interrupt decimal 15 (hex 0x0f)(irq 7) the printer 1 ack pin   // (0x0D is irq 5 (decimal 13)) address to the ack pin

long double x_unit, y_unit, z_unit;
long double checkbacklash_X (long double, long double);    
long double checkbacklash_Y (long double, long double); 
long double checkbacklash_Z (long double, long double);    
long double encoder1 = 0.000;
long double encoder2 = 0.000;
long double encoder3 = 0.000;
long double encoder4 = 0.000; 
int zero_all_X=0,zero_all_Y=0,zero_all_Z=0;     //for rezeroing when the backlash is on 
int zero_all_X_mode=0,zero_all_Y_mode=0,zero_all_Z_mode=0;  
int mywatch=0;  //this will turn on various couts so of varibles so i can see what is going on  (press "ctr z") from the encoder setup screen to turn it on.
//int Portnum = 888	; //888 is lpt1 and 632 is lpt2   
int timedisplay;
int check_key();   
int dataport = 888, statusport = 889, controlport = 890;    
int just_returned_from_irq;   
int use_irq;   //user selects rather to use IRQ or use no IRQ  (1 is yes, 0 is no)  
//time_t checktime;   
//long int holdtime, currenttime;  //for watching the time to rezero because this is a demo
void draw_encode_screen();    
void encoder_setup(char); 
void mode_setup(char);  
void rotary_setup(char);
void quick_rezero(char);
long double numbers_only(int, int, char message[]);             //function to input just a few long double numbers  
int intnumbers_only(int row, int colum, char message[]);         //function to input just a few int numbers    
void InPort (int, int); 
long double checkit(int, long double, long double, int);
char *decimal_to_degrees(long double); 
long double degrees_to_decimal(char string_degrees[]);
long double auto_degrees_decimal(char message[],int,int,int,int);    //char=message to send, int,int is textpostion for message and int,int is place for user to type in numbers
void open_scroll_help();  
void overlay_box ();  
void bigoverlay_box ();
void InPort (int);
void (__interrupt __far  __cdecl *oldhandler) (void); //pointer to the oldhandle on the interrupt
void __cdecl __interrupt __far  handler(int, int); //declare what is to come//don't need this if you put the function above the main() function


//ofstream   ERRORout;//used to watch errors.  I can turn it on by pressing ctr Z in setup.  will write a text file                   

/* AGE1 added information for showing config file used on screen and Program version   */

char machine[8] = "Machine" ;       /* Default Config File */
char version1[8] = "DRO40  "  ;       /* Default Version string*/                                              
char version2[8] = "DRO40A "  ;       /* Version string  DRO40A*/  
int versiontype =0 ;				/* default to version 40*/
int versioncontrol =0 ;              /* default to output to controlport*/
char machinecfg[12] = "Encode.cfg" ; /* default cfg file name */
char machinetemp[8]                ; /* used to allow testing of Machine Name */  
void smalloverlay_box ();            /* used for Machine routines */   
void small()                       ; /* use in getting characters in Machine routines */     
void smallend()                    ; /* termination of getting characters */  
void small1()                       ; /* use in getting characters in Machine routines */     
void smallend1()                    ; /* termination of getting characters */ 
/* AGE1 End of added global variables */



typedef struct
	{ 
    	int PrevValue;  
    	int PrevPrevValue;
    	int PrevPrevPrevValue;  
    	int possible_error;           //on off switch for if the error should be displaed
    	long int possible_error_cnt;  //where the total amount of errors is stored 
    	int cnt_updown;   //for the user to toggle +- in the way it counts (up or down) //used to swap the encoders neg to positive and positive to negative and to reverse the direction of the counting
        int changed;  //for the calling function to let it know which coder changed so it only updates that one on the screen.  It can update them all but if it does it has more to do and will be slower
        int decimal_places_shown;
        int conversion; 
        int error_changed;   //same as above "changed" only to let the calling funtion know if there was an error and let it decide if it should be displayed
     
	} CODER; 
CODER coder1, coder2, coder3;	    

 
typedef struct
	{
		//int inout;
		long double currently_displayed;  //what is currently on the screen no matter if it is currenly compensating
		long double dist; //how much to compensate for backlash
		int onoff;       //is this encoder backlash turned on or off
		int rightleft;    //direction to compensate for backlash  //this is not user changeable.  It is part of the backlash function itself 
		//int user_reset;  //tell if the user has just reset the left right(if so want to call whatever is on the screen as the new change point  
		int tension;  //tension can be 0 or 1    This has to do with what way to take up backlash.  The user can set this to whatever
	    int swapsign;  //turned on by user in encode setup to swap the sign of the number +/-
	    int far_enough;
	}BACKLASH; 
BACKLASH backlash_X, backlash_Y, backlash_Z;                       
                    

typedef struct
	{ 
	 long double xoffset; 
	 long double yoffset;
	 long double zoffset;
	
	}MODE;
MODE mode1, mode2, mode3, mode4;
int mode=0;  //0 is master mode 1 is mode1, 2 is mode2, 3 is mode3, 4 is mode4 	
                      
typedef struct
	{  
     long int x_number_of_blips;   
     long int y_number_of_blips;
     long int z_number_of_blips;
     int x_onoff;
     int y_onoff;
     int z_onoff; 
     int x_display_min;  //1 is display minutes and secs. and 0 is display decimal 
     int y_display_min;
     int z_display_min;  
     int x_show_revolutions;
     int y_show_revolutions;
     int z_show_revolutions;  
     int x_cnt_rotations;
     int y_cnt_rotations; 
     int z_cnt_rotations;
     
    }ROTARY;
ROTARY rotary;     
           
void InPort(int timedisplay)  
//void __cdecl __interrupt __far  handler(void)
{
    //UDT udtRet;
 //	int strobe;
 	static int counter, valuechanged;
 	static int FirstTime = 1;
    int t;
    int Value;
    static int loopTheDllAgain=0;
    //static long saveTick, save_incase_bug0, save_incase_bug1;
    //static int FirstAfter, SecondAfter, ThirdAfter, FourthAfter, FifthAfter, 
    static int GotOne, ctr; 
    static int LeaveAlone = 0;  
    int binary [10]; //the binary number from last time the port was read 
    int remainder, temp; 
    int Value1, Value2, Value3;  
    static int comparValue1, comparValue2, comparValue3;      
    static int inport_prevValue;
    static int  encoder1_prev, encoder2_prev, encoder3_prev;  
    static int  encoder1_prevprev, encoder2_prevprev, encoder3_prevprev;    
    
while (1 == 1)//loop forever
 { 
 	if (_bios_keybrd (_KEYBRD_READY)) return;                                        

     
////////////////////////////////////////////////////////////////     
   if ((rotary.x_onoff==1)&&(valuechanged==1))
      { //_settextposition (3,3); cout<<encoder1_prevprev<<"  "<<encoder1_prev<<flush;
       if (encoder1==0) //x_cnt_rotations counts the times 360 is hit   //when 360 is hit rezero the encoder   
	      {if ((encoder1_prev==1)&&(encoder1_prevprev== 2))    rotary.x_cnt_rotations--;  }   //cnt_rotations counts how many times we went past 360 and which way so it can be displayed if the user wants
       if (encoder1>=rotary.x_number_of_blips) {encoder1=0; rotary.x_cnt_rotations++; } 
	   if (encoder1 <= -1) encoder1=rotary.x_number_of_blips-1;	                            
	   encoder1_prevprev=encoder1_prev;
	   encoder1_prev=(int)encoder1;
	  }
   if ((rotary.y_onoff==1)&&(valuechanged==1))
      {
       if (encoder2==0) //x_cnt_rotations counts the times 360 is hit      
	      {if ((encoder2_prev==1)&&(encoder2_prevprev== 2))    rotary.y_cnt_rotations--;  }
       if (encoder2>=rotary.y_number_of_blips) {encoder2=0; rotary.y_cnt_rotations++; } 
	   if (encoder2 <= -1) encoder2=rotary.y_number_of_blips-1;	
	   encoder2_prevprev=encoder2_prev;
	   encoder2_prev=(int)encoder2;
	  }	    
   if ((rotary.z_onoff==1)&&(valuechanged==1))
      {
       if (encoder3==0) //x_cnt_rotations counts the times 360 is hit      
	      {if ((encoder3_prev==1)&&(encoder3_prevprev== 2))    rotary.z_cnt_rotations--;  }
       if (encoder3>=rotary.z_number_of_blips) {encoder3=0; rotary.z_cnt_rotations++; } 
	   if (encoder3 <= -1) encoder3=rotary.z_number_of_blips-1;	
	   encoder3_prevprev=encoder3_prev;
	   encoder3_prev=(int)encoder3;
	  }	     	    	    
    
 //new stuff here  
 //Value=_inp (dataport); //read the port  
 
 //  if (valuechanged==1)
 //	   {
 		//if (ctr>timedisplay){valuechanged=0; ctr=0; return; }
 		//ctr=0;  
 		//valuechanged=0;
 //      }
    Value=_inp (dataport); //read the port  
 	if (Value==inport_prevValue)  //this if will count up how many times the port was read (if counter ==0 will only let it go right after it has hit a 3 and is still a 3(may be a little more time this way))
    	{valuechanged=0;
    	 ctr++;			                 //and if the ctr is larger than the timedisplay (set by user setup) it will  
  	     if (ctr>(10*timedisplay))  //this will update the screen if the user just sits there after moveing it fast
             {ctr=0; return;}
        }  
           
    
 
 //end of new stuff   
   
   
  // Value=_inp (dataport); //read the port    //original read here
  
    
    //if (Value==inport_prevValue) ctr++;
    if (Value!=inport_prevValue)
    	{ctr=0;
    	valuechanged=1; //ctr=0; 
    	temp=Value;
    	for (t=0; t<=5; t++)  //should be 7 for figuring out all of the 0 and 1s but it is set to 5 because we only need the first 6 0 and 1s for 3 encoders
			{ remainder = temp % 2;   //find if there is anything left after trying to fit twos into the number
	     	temp = temp / 2;      //divide by 2 and save it to use here the next time around
		 	binary [t] = remainder;  //put the 1 or 0 into the binary array	
			} 
		binary[8] = '\0';   //terminate string with null character:	
	
	
	    if (coder1.cnt_updown==1)
		   {if ((binary[0]==0)&&(binary[1]==0)) Value1=0;
			if ((binary[0]==1)&&(binary[1]==0)) Value1=2;  //to make it change the direction it counts swap the 1 and 2 around
			if ((binary[0]==0)&&(binary[1]==1)) Value1=1;  //also you will need flip backlash.rightleft (1 and 2) around in the checkbacklash rezero  'if' right in the first part of checkbacklash
			if ((binary[0]==1)&&(binary[1]==1)) Value1=3;} 
		else 
		   {if ((binary[0]==0)&&(binary[1]==0)) Value1=0;
			if ((binary[0]==1)&&(binary[1]==0)) Value1=1;  //to make it change the direction it counts swap the 1 and 2 around
			if ((binary[0]==0)&&(binary[1]==1)) Value1=2;  //also you will need flip backlash.rightleft (1 and 2) around in the checkbacklash rezero  'if' right in the first part of checkbacklash
			if ((binary[0]==1)&&(binary[1]==1)) Value1=3;} 	
	    if (coder2.cnt_updown==1)
		   {if ((binary[2]==0)&&(binary[3]==0)) Value2=0;
			if ((binary[2]==1)&&(binary[3]==0)) Value2=2;
			if ((binary[2]==0)&&(binary[3]==1)) Value2=1;
			if ((binary[2]==1)&&(binary[3]==1)) Value2=3;}   //these figure out if we should work with a 0 1 2 or 3 for the encoders.  even 2 or 3 encoders can change and it should keep up
		else
	       {if ((binary[2]==0)&&(binary[3]==0)) Value2=0;
			if ((binary[2]==1)&&(binary[3]==0)) Value2=1;
			if ((binary[2]==0)&&(binary[3]==1)) Value2=2;
			if ((binary[2]==1)&&(binary[3]==1)) Value2=3;}                                                
		if (coder3.cnt_updown==1)
		   {if ((binary[4]==0)&&(binary[5]==0)) Value3=0;
			if ((binary[4]==1)&&(binary[5]==0)) Value3=2;
			if ((binary[4]==0)&&(binary[5]==1)) Value3=1;
			if ((binary[4]==1)&&(binary[5]==1)) Value3=3;}
	    else
	       {if ((binary[4]==0)&&(binary[5]==0)) Value3=0;
			if ((binary[4]==1)&&(binary[5]==0)) Value3=1;
			if ((binary[4]==0)&&(binary[5]==1)) Value3=2;
			if ((binary[4]==1)&&(binary[5]==1)) Value3=3;}
	 
	//	if (mywatch==1) //I can turn this on from the encoder setup screen by pressing ctr z
//			{_settextposition (1,1); 
//			cout <<binary<<"\n"<<flush;
	//		cout <<Value1<<Value2<<Value3<<"\n"<<flush;  }
	
		if (comparValue1!=Value1)  
			{coder1.changed=1;   //for the calling function to let it know which coder changed so it only updates that one on the screen.  It can update them all but if it does it has more to do and will be slower
			encoder1=checkit(Value1, x_unit, encoder1, 1); 
	    	}
		if (comparValue2!=Value2)  
			{coder2.changed=1; 
			encoder2=checkit(Value2, y_unit, encoder2, 2);		 
			}
		if (comparValue3!=Value3)  
			{coder3.changed=1; 
			encoder3=checkit(Value3, z_unit, encoder3, 3);;
			}   
	
		comparValue1=Value1; comparValue2=Value2; comparValue3=Value3;//save the values for the next time back for comparison	
	
	    
		inport_prevValue=Value;             //save the new value into prevValue      //might need to do it here**********
		}  //end if Value!=inport_prevValue  
	
		
  }//end while 
/*	
  inport_prevValue=Value;	
  strobe=_inp(dataport+2);      //these three lines are to check the strobe (pin 1) and change its status from 0 to 1 or from 1 to 0.  This is so my hardware will reset its self and be ready for another interupt
  if ((strobe&1)!=0) _outp (dataport+2,16); 
  else _outp (dataport+2,16+1); 
  just_returned_from_irq==1;	
  _outp(32,32);  //nonspecific end of interrupt//this interrupt is done go wait for another	  
*/	
}//end function	
			                       
//void InPort(int Portnum, int timedisplay)  
void __cdecl __interrupt __far  handler(void)
{
    //UDT udtRet;
 	int strobe;
 	static int counter;
 	static int FirstTime = 1;
    int t;
    int Value;
    static int loopTheDllAgain=0;
    //static long saveTick, save_incase_bug0, save_incase_bug1;
    //static int FirstAfter, SecondAfter, ThirdAfter, FourthAfter, FifthAfter, 
    static int GotOne, ctr; 
    static int LeaveAlone = 0;  
    int binary [10]; //the binary number from last time the port was read 
    int remainder, temp; 
    int Value1, Value2, Value3;  
    static int comparValue1, comparValue2, comparValue3;      
    static int inport_prevValue;
    static int  encoder1_prev, encoder2_prev, encoder3_prev;  
    static int  encoder1_prevprev, encoder2_prevprev, encoder3_prevprev;
                                     
   if (rotary.x_onoff==1)
      {
       if (encoder1==0) //x_cnt_rotations counts the times 360 is hit   //when 360 is hit rezero the encoder   
	      {if ((encoder1_prev==1)&&(encoder1_prevprev== 2))    rotary.x_cnt_rotations--;  }   //cnt_rotations counts how many times we went past 360 and which way so it can be displayed if the user wants
       if (encoder1>=rotary.x_number_of_blips) {encoder1=0; rotary.x_cnt_rotations++; } 
	   if (encoder1 <= -1) encoder1=rotary.x_number_of_blips-1;	                            
	   encoder1_prevprev=encoder1_prev;
	   encoder1_prev=(int)encoder1;
	  }
   if (rotary.y_onoff==1)
      {
       if (encoder2==0) //x_cnt_rotations counts the times 360 is hit      
	      {if ((encoder2_prev==1)&&(encoder2_prevprev== 2))    rotary.y_cnt_rotations--;  }
       if (encoder2>=rotary.y_number_of_blips) {encoder2=0; rotary.y_cnt_rotations++; } 
	   if (encoder2 <= -1) encoder2=rotary.y_number_of_blips-1;	
	   encoder2_prevprev=encoder2_prev;
	   encoder2_prev=(int)encoder2;
	  }	    
   if (rotary.z_onoff==1)
      {
       if (encoder3==0) //x_cnt_rotations counts the times 360 is hit      
	      {if ((encoder3_prev==1)&&(encoder3_prevprev== 2))    rotary.z_cnt_rotations--;  }
       if (encoder3>=rotary.z_number_of_blips) {encoder3=0; rotary.z_cnt_rotations++; } 
	   if (encoder3 <= -1) encoder3=rotary.z_number_of_blips-1;	
	   encoder3_prevprev=encoder3_prev;
	   encoder3_prev=(int)encoder3;
	  }	     	    	    
 
   Value=_inp (dataport); //read the port
  
    
    if (Value==inport_prevValue) ctr++;
    if (Value!=inport_prevValue)
    	{ctr=0;
    	//valuechanged=1; //ctr=0; 
    	temp=Value;
    	for (t=0; t<=5; t++)  //should be 7 for figuring out all of the 0 and 1s but it is set to 5 because we only need the first 6 0 and 1s for 3 encoders
			{ remainder = temp % 2;   //find if there is anything left after trying to fit twos into the number
	     	temp = temp / 2;      //divide by 2 and save it to use here the next time around
		 	binary [t] = remainder;  //put the 1 or 0 into the binary array	
			} 
		binary[8] = '\0';   //terminate string with null character:	
	
	
	    if (coder1.cnt_updown==1)
		   {if ((binary[0]==0)&&(binary[1]==0)) Value1=0;
			if ((binary[0]==1)&&(binary[1]==0)) Value1=2;  //to make it change the direction it counts swap the 1 and 2 around
			if ((binary[0]==0)&&(binary[1]==1)) Value1=1;  //also you will need flip backlash.rightleft (1 and 2) around in the checkbacklash rezero  'if' right in the first part of checkbacklash
			if ((binary[0]==1)&&(binary[1]==1)) Value1=3;} 
		else 
		   {if ((binary[0]==0)&&(binary[1]==0)) Value1=0;
			if ((binary[0]==1)&&(binary[1]==0)) Value1=1;  //to make it change the direction it counts swap the 1 and 2 around
			if ((binary[0]==0)&&(binary[1]==1)) Value1=2;  //also you will need flip backlash.rightleft (1 and 2) around in the checkbacklash rezero  'if' right in the first part of checkbacklash
			if ((binary[0]==1)&&(binary[1]==1)) Value1=3;} 	
	    if (coder2.cnt_updown==1)
		   {if ((binary[2]==0)&&(binary[3]==0)) Value2=0;
			if ((binary[2]==1)&&(binary[3]==0)) Value2=2;
			if ((binary[2]==0)&&(binary[3]==1)) Value2=1;
			if ((binary[2]==1)&&(binary[3]==1)) Value2=3;}   //these figure out if we should work with a 0 1 2 or 3 for the encoders.  even 2 or 3 encoders can change and it should keep up
		else
	       {if ((binary[2]==0)&&(binary[3]==0)) Value2=0;
			if ((binary[2]==1)&&(binary[3]==0)) Value2=1;
			if ((binary[2]==0)&&(binary[3]==1)) Value2=2;
			if ((binary[2]==1)&&(binary[3]==1)) Value2=3;}                                                
		if (coder3.cnt_updown==1)
		   {if ((binary[4]==0)&&(binary[5]==0)) Value3=0;
			if ((binary[4]==1)&&(binary[5]==0)) Value3=2;
			if ((binary[4]==0)&&(binary[5]==1)) Value3=1;
			if ((binary[4]==1)&&(binary[5]==1)) Value3=3;}
	    else
	       {if ((binary[4]==0)&&(binary[5]==0)) Value3=0;
			if ((binary[4]==1)&&(binary[5]==0)) Value3=1;
			if ((binary[4]==0)&&(binary[5]==1)) Value3=2;
			if ((binary[4]==1)&&(binary[5]==1)) Value3=3;}
	 
	//	if (mywatch==1) //I can turn this on from the encoder setup screen by pressing ctr z
//			{_settextposition (1,1); 
//			cout <<binary<<"\n"<<flush;
	//		cout <<Value1<<Value2<<Value3<<"\n"<<flush;  }
	
		if (comparValue1!=Value1)  
			{coder1.changed=1;   //for the calling function to let it know which coder changed so it only updates that one on the screen.  It can update them all but if it does it has more to do and will be slower
			encoder1=checkit(Value1, x_unit, encoder1, 1); 
	    	}
		if (comparValue2!=Value2)  
			{coder2.changed=1; 
			encoder2=checkit(Value2, y_unit, encoder2, 2);		 
			}
		if (comparValue3!=Value3)  
			{coder3.changed=1; 
			encoder3=checkit(Value3, z_unit, encoder3, 3);;
			}   
	
		comparValue1=Value1; comparValue2=Value2; comparValue3=Value3;//save the values for the next time back for comparison	
	
	    
		//inport_prevValue=Value;             //save the new value into prevValue      //might need to do it here**********
		}  //end if Value!=inport_prevValue  
	
		
//	}//end while 
	
   	inport_prevValue=Value;	
	 strobe=_inp(dataport+2);      //these three lines are to check the strobe (pin 1) and change its status from 0 to 1 or from 1 to 0.  This is so my hardware will reset its self and be ready for another interupt
    if ((strobe&1)!=0) _outp (dataport+2,16); 
    else _outp (dataport+2,16+1); 
    just_returned_from_irq==1;	
    _outp(32,32);  //nonspecific end of interrupt//this interrupt is done go wait for another	  
	
}//end function	
			
    	

		
long double checkit (int Value, long double unit, long double encoder, int which_coder)	 
{	
	     
	      
	     int GotOne=0, LeaveAlone=0;
         int PrevValue, PrevPrevValue, PrevPrevPrevValue; 
   	 	 
   	 	 if (which_coder==1)
   	 	 	{PrevValue=coder1.PrevValue;
   	 	 	 PrevPrevValue=coder1.PrevPrevValue;  
   	 	 	 PrevPrevPrevValue=coder1.PrevPrevPrevValue;}  
   	 	 if (which_coder==2)
   	 	 	{PrevValue=coder2.PrevValue;
   	 	 	 PrevPrevValue=coder2.PrevPrevValue;  
   	 	 	 PrevPrevPrevValue=coder2.PrevPrevPrevValue;} 
   	 	 if (which_coder==3)
   	 	 	{PrevValue=coder3.PrevValue;
   	 	 	 PrevPrevValue=coder3.PrevPrevValue;  
   	 	 	 PrevPrevPrevValue=coder3.PrevPrevPrevValue;}  	  
   	 	 
   	 	//if (!ERRORout) {cout<<"\n"<<"Cannot write error.txt file"<<flush;_getch();}
    	    //	ERRORout<<Value<<PrevValue<<PrevPrevValue<<PrevPrevPrevValue<<"\n";	
   	 	 
   	 	 if (GotOne == 0)  
   	 	    {
   	 	        if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;}
   	 	    
   	 	        if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;} 
   	 	         
   	 	         
   	 	        if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;}   
     	 		
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;}    
     	 		
     	 		
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;}
     	 		
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;}  
     	 		                                                                                                                
     	 		                                                                                                                
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;} 
     	 		
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;}   
   	 	    }
   	 	 
   	 	 
     	 	
/*     	 	{
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;} 
     	 		
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;}
     	 	}
     	 if (GotOne ==0)        //in case of an extra # before 3 go back and look at the one before and count it as PrevValue
     	    {	
     	    	if ((Value==3)&&(PrevPrevValue==1)) {PrevValue=PrevPrevValue; encoder=encoder-unit;GotOne = 1;}  
     	    	if ((Value==3)&&(PrevPrevValue==2)) {PrevValue=PrevPrevValue; encoder=encoder+unit;GotOne = 1;}
     	    	 
     	    	if ((Value==3)&&(PrevPrevValue==1)) {PrevValue=PrevPrevValue; encoder=encoder-unit;GotOne = 1;}  
     	    	if ((Value==3)&&(PrevPrevValue==2)) {PrevValue=PrevPrevValue; encoder=encoder+unit;GotOne = 1;}  
     	    }
     	 if (GotOne==0)        
     	 	{	if ((Value==0)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){GotOne=1;LeaveAlone=1;}//skip the extra zero  error
     	 		if ((Value==0)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){GotOne=1;LeaveAlone=1;}//skip the extra zero  error  
     	 		if ((Value==1)&&(PrevValue==2)&&(PrevPrevValue==0)){GotOne=1;LeaveAlone=1;}//skip the extra one error
     	 		if ((Value==2)&&(PrevValue==1)&&(PrevPrevValue==0)){GotOne=1;LeaveAlone=1;}//skip the extra two error
     	 	}	   
    	 if (GotOne == 0) //changed directions 
     	 	{
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;} 
     	 		
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;}
     	 	} 
     	 if (GotOne == 0)  //for next time around after changed directions
     	 	{
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;} 
     	 		
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==3)&&(PrevPrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==1)&&(PrevPrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==0)&&(PrevPrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==2)&&(PrevPrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;}
     	 	}	    	
         if (GotOne == 0) //changed directions //same as other changed directions but this is a catch all in case it is just siting there flashing back and forth between two
     	 	{
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;} 
     	 		
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;}     	 
     	 
     	 	} 

     	 if (GotOne == 0)  //same as first one and the one above but without the last requirement
     	 	{
     	 		if ((Value == 0)&&(PrevValue==2)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==0)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)){encoder=encoder-unit;GotOne = 1;} 
     	 		
     	 		if ((Value == 0)&&(PrevValue==1)){udtRet.encoder=udtRet.encoder+unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==0)){udtRet.encoder=udtRet.encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)){udtRet.encoder=udtRet.encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)){udtRet.encoder=udtRet.encoder+unit;GotOne = 1;}
     	 	}   
     	 
        if (GotOne==0)
            {	
            //if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 0)&&(PrevValue==2)&&(PrevPrevValue==3)){encoder=encoder-unit;GotOne = 1;} 
     	 		if ((Value == 1)&&(PrevValue==0)&&(PrevPrevValue==2)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==1)&&(PrevPrevValue==0)){encoder=encoder-unit;GotOne = 1;}
     	 		if ((Value == 2)&&(PrevValue==3)&&(PrevPrevValue==1)){encoder=encoder-unit;GotOne = 1;} 
     	 		
     	 		if ((Value == 0)&&(PrevValue==1)&&(PrevPrevValue==3)){encoder=encoder+unit;GotOne = 1;} 
     	 		if ((Value == 2)&&(PrevValue==0)&&(PrevPrevValue==1)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 3)&&(PrevValue==2)&&(PrevPrevValue==0)){encoder=encoder+unit;GotOne = 1;}
     	 		if ((Value == 1)&&(PrevValue==3)&&(PrevPrevValue==2)){encoder=encoder+unit;GotOne = 1;}     
            
           } 
*/ 
 
 		if (GotOne==0)  //Hopfuly it will not make it to here.  If it does it counts the errors up
     	   {if (which_coder==1) {coder1.possible_error_cnt ++; coder1.error_changed=1;}	
    	    if (which_coder==2) {coder2.possible_error_cnt ++; coder2.error_changed=1;}
    	    if (which_coder==3) {coder3.possible_error_cnt ++; coder3.error_changed=1;}
    	    
//   	    if (mywatch==1)       //this will make a text file with the values it did not know what to do with
 //   	    	{
    	    	
 				
 	//			if (!ERRORout) {cout<<"\n"<<"Cannot write error.txt file"<<flush;_getch();}
    //	    	ERRORout<<Value<<PrevValue<<PrevPrevValue<<PrevPrevPrevValue<<"\n";
    	  
 //   	        }
    	    
    	    
   	    }
    	    
    	//_settextposition (4,1);
   	 	// cout<<Value<<PrevValue<<PrevPrevValue<<"\n"<<flush;	
     	 
     	 if (LeaveAlone == 0)	 //if it should not be left along then update the Prevs-Values
     	 	{if (which_coder==1)
   	 	 		{coder1.PrevValue=Value;
   	 	 	 	coder1.PrevPrevValue=PrevValue;  
   	 	 	 	coder1.PrevPrevPrevValue=PrevPrevValue;}  
   	 	 	 if (which_coder==2)
   	 	 		{coder2.PrevValue=Value;
   	 	 	 	coder2.PrevPrevValue=PrevValue;  
   	 	 	 	coder2.PrevPrevPrevValue=PrevPrevValue;} 
   	 	 	 if (which_coder==3)
   	 	 		{coder3.PrevValue=Value;
   	 	 	 	coder3.PrevPrevValue=PrevValue;  
   	 	 	 	coder3.PrevPrevPrevValue=PrevPrevValue;}  	  
     	    }
     	 
     	 return encoder;
     	 	 
}     	 	   	

   

 main ()
{ 
/////////This is the defalt mode setting if the encode.cfg file can not be found

rotary.x_onoff=0;
rotary.y_onoff=0;
rotary.z_onoff=0;  
rotary.x_number_of_blips=720; rotary.y_number_of_blips=720;  rotary.z_number_of_blips=720;   
x_unit=0.001, y_unit=0.001, z_unit=0.001;  
rotary.x_cnt_rotations=0, rotary.y_cnt_rotations=0, rotary.z_cnt_rotations=0;
backlash_X.dist=0.0100;backlash_X.onoff=0;backlash_Y.onoff=0;backlash_Y.dist=0.0100,backlash_Z.onoff=0;backlash_Z.dist=0.0100; 
backlash_X.rightleft=1, backlash_Y.rightleft=1, backlash_Z.rightleft=1; 
backlash_X.tension=0, backlash_Y.tension=0, backlash_Z.tension=0;  
use_irq=0;   //user selects rather to use IRQ or use no IRQ  (1 is yes, 0 is no)   
timedisplay=10;

 coder1.possible_error=1; coder2.possible_error=1;coder3.possible_error=1;
 coder1.cnt_updown=0; coder2.cnt_updown=0;coder3.cnt_updown=0; 
 coder1.decimal_places_shown=4; 
 coder2.decimal_places_shown=4;
 coder3.decimal_places_shown=4;  
 


/////////End of default mode settings 

 //int imask_bit;
 //int NumberOfEncoders = 1;
 int imask, imask_bit=128;  //irq stuff    //default imask_bit is 128 which is irq 7
 long double longdoub;
 long double shouldweprint;
 char printhex; //used to print some funny text characters to decorate the screen 
 int m;  //used just as a counter in some for loops  
 int c, len, temp;
 char letter;
 long double printfloat; 
 char string[20]; 
 char stringg[6];   
 char tempbuffer[10];                                              
 int oldtextcolor;  
 //char binary [10];
 //holdtime= time(NULL);  //get the time when the user first starts so you can rezero every ten minutes because this is a demo
 fstream   FILEin;       //for the encode.cfg that is read when the program starts and is rewritten when it ends
 fstream   FILEout;        
 
 
 gogetit:
 
 FILEin.open(machinecfg, ios::in | ios::nocreate); 
 
 /*AGE1*/ 
 
 /*FILEin.open("encode.cfg", ios::in | ios::nocreate);  */
 //		if (!FILEin) {cout<<"\n"<<"ENCODE.CFG file does not exist in current directory.  Loading default settings."<<"\n"<<"Press any key to continue..."<<flush; _getch();temp=67;goto gosaveit;}  
 
 
 		
 FILEin>>dataport>>timedisplay>>x_unit>>y_unit>>z_unit
 	   >>backlash_X.dist>>backlash_Y.dist>>backlash_Z.dist
 	   >>backlash_X.onoff>>backlash_Y.onoff>>backlash_Z.onoff
 	   >>backlash_X.tension>>backlash_Y.tension>>backlash_Z.tension
 	   >>coder1.possible_error>>coder2.possible_error>>coder3.possible_error;
 FILEin>>coder1.decimal_places_shown>>coder2.decimal_places_shown>>coder3.decimal_places_shown
 	   >>coder1.cnt_updown>>coder2.cnt_updown>>coder3.cnt_updown
 	   >>coder1.conversion>>coder2.conversion>>coder3.conversion
 	   >>dataport>>statusport>>controlport	
 	   >>interrupt_address>>imask_bit;
 FILEin>>rotary.x_onoff>>rotary.y_onoff>>rotary.z_onoff 
       >>rotary.x_display_min>>rotary.y_display_min>>rotary.z_display_min   
       >>rotary.x_number_of_blips>>rotary.y_number_of_blips>>rotary.z_number_of_blips 
       >>rotary.x_show_revolutions>>rotary.y_show_revolutions>>rotary.z_show_revolutions
       >>use_irq>>timedisplay;     
 /* added by AGE1 */
 FILEin>>machine;       
 FILEin>>versiontype; 
 FILEin>>versioncontrol;
 
 
 FILEin.close();    
 
 _outp (controlport, versioncontrol); /* AGE1 now we have to make it work! */
 /*
 ERRORout.open("error.txt", ios::out ); //opens the error.txt file so it is read for input
 ERRORout<<"This ERROR.TXT file is generated everytime ENCODE READ is ran"<<"\n"
 		 <<"and ends.  A quadature encoder puts out input similar to this:"<<"\n\n"
 		 <<"013201320132  counting one direction"<<"\n"
 		 <<"023102310231  counting the other"<<"\n\n"
 		 <<"From looking at the current number and the past numbers it is"<<"\n"
 		 <<"determined which way it should count."<<"\n\n"
 		 <<"Each line below shows the number that the error occurred on and"<<"\n"
 		 <<"the last three values that occurred before it"<<"\n\n";
 */
 oldtextcolor= _gettextcolor();  //save the users computer current text color
 _outp (dataport, 0);  //just send a 0 to the data ports to make sure none of them are turned on internally
again: 

 _settextcursor (0x2000);  //no cursor  //0x0707 is the regular cursor                            
 _setvideomode(_TEXTC80);   
 _setbkcolor (9);  //blue
 _clearscreen(_GCLEARSCREEN); 
//_setvideomode (_DEFAULTMODE);
 _settextposition (24,60); cout<<"Esc to Quit"<<flush; 

 //settextcolor(4);//red=4 yellow=14 brightwhite=15 normal white =7   black=0   light blue=9
 _setbkcolor (4); _settextposition(1,2);_outtext("                                                                              ");
 //_settextposition (1,28); 
 _settextposition (1,19);    
 _settextcolor(14);    //black
// _outtext("QUADRATURE ENCODE READER");       
 _outtext("DIGITAL READOUT  Quadrature encoder reader");
                                                        
                                                        
/* mod by AGE1 11-26-00  */    

_settextposition (2,30);    
 _settextcolor(14);    //black
_outtext( machine );                                                        

 _setbkcolor (9);  //light blue 
_settextcolor(4);//red=4 yellow=14 brightwhite=15 normal white =7   
 printhex=0xc8; 
 _settextposition (24,2); _outtext(" "); _settextposition (24,2); cout<<printhex<<flush; 
 printhex=0xbc; 
 _settextposition (24,79); _outtext(" "); _settextposition (24,79); cout<<printhex<<flush; 
 printhex=0xBA; 
 for (m=2;m<=23;m++)
 	{_settextposition (m,2); _outtext(" "); _settextposition (m,2); cout<<printhex<<flush; }    
 for (m=2;m<=23;m++)
 	{_settextposition (m,79); _outtext(" "); _settextposition (m,79); cout<<printhex<<flush; }    	 
 printhex=0xcd; 
 for (m=3;m<79;m++)
 	{if ((m<59)||(m>71))
 	    {_settextposition (24,m); _outtext(" "); _settextposition (24,m); cout<<printhex<<flush; }}	



// _settextposition(4,1);
// _setbkcolor (8); _outtext("                            ");    
 
 _setbkcolor (7);  
 for (m=5;m<=22;m++) 
 	{_settextposition(m,15);                                          // age changed 19 to 22
 	 _outtext("                                                  ");
 	} 
 _setbkcolor (0);
 
 for (m=15;m<=65;m++) 
    {_settextposition(23,m);
     _outtext(" ");                                                 // age changed 20 to 23
    } 
 for (m=5;m<=22;m++) 
    {_settextposition(m,65);                                       // age changed 19 to 22
     _outtext(" ");
    }  

 /*  show version number AGE1*/
 
 _settextposition (4,10);   
 
 if ( versiontype == 0 )     
       		cout<<version1<<flush ;     
 	   else   if ( versiontype == 1 ) 
 	        cout<<version2<<flush ;    
 	        
 /* AGE1 changed positions and added Program Version to screen */	        
 
 _setbkcolor (7);  //white
 _settextcolor(0);  //black
 _settextposition (6,28);_outtext("Read Encoders");
 _settextposition (8,28);_outtext("Zero All Encoders");  
 _settextposition (10,28);_outtext("LPT Port"); 
 _settextposition (12,28);_outtext("IRQ Settings");   
 _settextposition (14,28);_outtext("Time Display"); 
 _settextposition (16,28);_outtext("Program Version");   
 _settextposition (18,28);_outtext("Machine Name"); 
 _settextposition (20,28);_outtext("Help");                            //age changed 18 to 20
 _settextposition (22,28);_outtext("About");  
 if(use_irq==1) {_settextposition (22,55);_outtext("IRQ ON");}         //age changed 19 to 22
 if(use_irq==0) {_settextposition (22,55);_outtext("IRQ OFF");}
 

 
 _settextcolor(4);     //red=4 yellow=14 brightwhite=15 normal white =7
 _settextposition (6,28);    
 _outtext ("R");
 _settextposition (8,28);    
 _outtext ("Z");
 _settextposition (10,28);    
 _outtext ("L");
 _settextposition (12,28);    
 _outtext ("I");    
 _settextposition (14,28);    
 _outtext ("T");        
 _settextposition (16,28);    
 _outtext ("P");             
 _settextposition (18,28);    
 _outtext ("M");
 _settextposition (20,28);    
 _outtext ("H");    
 _settextposition (22,28);    
 _outtext ("A"); 
 _settextposition (22,59);
 _outtext ("O");
 _settextcolor(7);     //white   
 

 
 
 
 //cout<<c<<flush;  //for testing.  Will print out character decimal number.    
 _settextcolor(14);  //yello                                                                 
 //_settextposition (14,3);_outtext("                                                                            ");   //from now on this place will be yellow with couts.                           
 //_settextposition (15,3);_outtext("                                                                            ");   //from now on this place will be yellow with couts.   
 _settextcolor(7);  //white	        
 	        
 	        
 	        
 
 
 

 _settextcolor(7);  //white  
 _settextposition (4,62);  
 if (dataport==888) cout<<"LPT1 "<<flush;
 if (dataport==632) cout<<"LPT2 "<<flush;
 if (dataport==956) cout<<"LPT3 "<<flush;
 if ((dataport!=888)&&(dataport!=632)&&(dataport!=956))  cout<<"decimal "<<dataport<<" "<<flush;
 if (imask_bit==128) cout<<"IRQ7"<<flush; //irq7
 if (imask_bit==32)  cout<<"IRQ5"<<flush;  //irq5
 
 c = _getch();  
 //_settextposition (3,3); cout<<c<<flush;	     
 //c = _getch();  
 _settextcursor (0x0707);  //cursor on    
 if ((c==111)||(c==79))   //if the o or O key for irq off and irq on
 	{if (use_irq==0) use_irq=1;
 	 else {
 	 		use_irq=0;   
 	 		//*****************put the interrupts back like they were*****************************
    
			_disable();  //disable interrupts while we work on them
			_outp (dataport+2, 0);  //clear all the bits to 0 at the control lpt control register
    		imask=_inp(33);        //8259 chip it is always this address (decimal 33) on even the old computers
			if ((imask&imask_bit)==0) imask=imask+imask_bit; //imask_bit is either 32 or 128 //remask the 8259 irq bit    (add these to get to (irq7=128 and irq5==32)
			_outp (33,imask);
			_enable();   //re-enable the interrupts   
	
	
    		//*******************end or put the interrupts back like they were ***************************    
    	}	
    
 	} 
 if ((c==97)||(c==65))  //if A or a for About  
     {bigoverlay_box ();
	 _settextcolor(15);    //red=4 yellow=14 brightwhite=15 normal white =7, black =0   
 	 _settextcursor (0x0707);     //0x0707 is the regular cursor         
     _settextposition (1,3);  
     _outtext("           Version 4.1, copyright 1995 1996 2000");  //AGE1 
     _settextposition (2,3);  
     _outtext("               Lindsay Engraving Inc.");  
     _settextposition (3,3);  
     _outtext("             3714 W. Cedar Hills Drive");  
     _settextposition (4,3);  
     _outtext("                 Kearney, NE 68847");  
     _settextposition (5,3);  
     _outtext("            slindsay@lindsayengraving.com");   
     _settextcolor(4);    //red=4 yellow=14 brightwhite=15 normal white =7, black =0 
     _settextposition (7,3);  
     _outtext("This program will make your computer into a DRO");  
     _settextposition (8,3);  
     _outtext("indexing reader for three 5V encoders.  See dro.txt");  
     _settextposition (9,3);  
     _outtext("and no_irq.gif for attaching encoders to your LPT port.");  
     _settextcolor(14);    //red=4 yellow=14 brightwhite=15 normal white =7, black =0   
     _settextposition (12,4);  
     _outtext("     This program previously sold for ");  
     _settextposition (13,4);  
     _outtext("     $79.  Now it is available to everyone.");  
     _settextposition (14,4);  
     _outtext("     If  you use it and think it is worth");  
     _settextposition (15,4);  
     _outtext("     something please make a $ contribution.");  
     _settextposition (16,4);  
     _outtext("               ");  
     _settextposition (17,4);  
     _outtext("     Steve Lindsay");      
     _outtext("               ");  
     _settextposition (19,9);  
     _outtext("As Modified by Art Eckstein");      //AGE1
     
     
     _settextcolor(4);    //red=4 yellow=14 brightwhite=15 normal white =7, black =0   
     _settextposition (21,17);      
     _settextcursor (0x2000 );    //0x0707 is the regular cursor    //no cursor  0x2000
     _outtext("Press any key to continue...");
     _getch();
     goto again;
    } 
 
 if ((c==27)||(c==113))           //if Q or q or ESC then quit 
    {_settextcursor (0x2000 );    //0x0707 is the regular cursor    //no cursor  0x2000
    _setbkcolor (4);
	_settextcolor(14);  //yello   
    _settextposition (11,18);_outtext("                                            "); 
    _settextposition (12,18);_outtext("     Are you sure you want to exit? y/n     ");
    _settextposition (13,18);_outtext("                                            "); 
    _setbkcolor (0);   //black
    _settextposition (14,18);_outtext("                                            ");     
    _settextposition (11,61);_outtext(" "); 
    _settextposition (12,61);_outtext(" ");
    _settextposition (13,61);_outtext(" "); 
    c=_getch(); 
    if (c!=121)   goto again;//the y for "yes lets exit"
    //_setvideomode(_TEXTC80); _settextcolor (oldtextcolor); _settextcursor (0x0707);  
    _setvideomode(_DEFAULTMODE);    
    
    /* age2 attempt to file out by variable! machinecfg*/    
    gosaveit:
     FILEout.open(machinecfg, ios::out ); 
     
     /* age1 FILEout.open("ENCODE.GFG", ios::out );  */
 	if (!FILEout) {cout<<"\n"<<"Cannot write ENCODE.CFG"<<flush;_getch();}    
 	/* changed setprecision to  setprecision */ 
 	FILEout<<setprecision (14);    //this will tell it to write 14 places to the right of the decimal point.  Need it to store a long double and get it all
 	FILEout<<dataport<<"\n"<<timedisplay<<"\n"<<x_unit<<"\n"<<y_unit<<"\n"<<z_unit<<"\n"
       	   <<backlash_X.dist<<"\n"<<backlash_Y.dist<<"\n"<<backlash_Z.dist<<"\n"
 		   <<backlash_X.onoff<<"\n"<<backlash_Y.onoff<<"\n"<<backlash_Z.onoff<<"\n";
    FILEout<<backlash_X.tension<<"\n"<<backlash_Y.tension<<"\n"<<backlash_Z.tension<<"\n"
       	   <<coder1.possible_error<<"\n"<<coder2.possible_error<<"\n"<<coder3.possible_error<<"\n";
    FILEout<<coder1.decimal_places_shown<<"\n"<<coder2.decimal_places_shown<<"\n"<<coder3.decimal_places_shown<<"\n"  
       	   <<coder1.cnt_updown<<"\n"<<coder2.cnt_updown<<"\n"<<coder3.cnt_updown<<"\n"
       	   <<coder1.conversion<<"\n"<<coder2.conversion<<"\n"<<coder3.conversion<<"\n"
       	   <<dataport<<"\n"<<statusport<<"\n"<<controlport<<"\n";
    FILEout<<interrupt_address<<"\n"<<imask_bit<<"\n"	
           <<rotary.x_onoff<<"\n"<<rotary.y_onoff<<"\n"<<rotary.z_onoff<<"\n" 
       	   <<rotary.x_display_min<<"\n"<<rotary.y_display_min<<"\n"<<rotary.z_display_min<<"\n";
    FILEout<<rotary.x_number_of_blips<<"\n"<<rotary.y_number_of_blips<<"\n"<<rotary.z_number_of_blips<<"\n"
       	   <<rotary.x_show_revolutions<<"\n"<<rotary.y_show_revolutions<<"\n"<<rotary.z_show_revolutions<<"\n"
       	   <<use_irq<<"\n"<<timedisplay; 
 	/* Added by AGE1   3lines       */
 		FILEout<<machine<<"\n";  
 		FILEout<<versiontype<<"\n"; 
 		FILEout<<versioncontrol<<"\n";  
 	
 	FILEout.close();	
    //ERRORout.close();//if the error file was open it will close it too   
    //*****************put the interrupts back like they were*****************************
    
	_disable();  //disable interrupts while we work on them
	_outp (dataport+2, 0);  //clear all the bits to 0 at the control lpt control register
    imask=_inp(33);        //8259 chip it is always this address (decimal 33) on even the old computers
	if ((imask&imask_bit)==0) imask=imask+imask_bit; //imask_bit is either 32 or 128 //remask the 8259 irq bit    (add these to get to (irq7=128 and irq5==32)
	_outp (33,imask);
	_enable();   //re-enable the interrupts   
	
	
    //*******************end or put the interrupts back like they were ***************************
     if (temp == 67)
    {
    temp=0;      /* AGE1 somehow, have to clear temp so we can quit program*/
    goto again;}  /* This means that we just created a file and want to continue the program */                                        
                                             
    return 0;}     //return back to DOS
/*   


 if (c==98)   //check if it is a biodirectional port
 	{//getnum=_inp (0x0c);
 	_settextposition (14,1); 
	_settextcolor(14);  //yello  
    _settextcursor (0x2000);  //no cursor  //0x0707 is the regular cursor     
 	if (255==(_inp (statusport)) &&(255==(_inp (controlport)) ))   //check and see if there is even a lpt port here(if they both read 255 it is not here)        
 	   {//_outtext("              The LPT port decimal address of ");
 		//_outtext(dataport);
 		//_outtext(" is not a valid port or hardware is not installed. \n              Press any key to continue...");                   
 	   //_settextcolor(7);  //white		  
	   //_getch();
	   //goto again; 
	   cout<<"              The LPT port decimal address of "<<dataport<<"\n"
	       <<"              is not a valid port or hardware is not installed"<<flush;
 	   _settextcolor(14);  //yello  
 	   _settextposition (18,28); 
 	   _outtext("   Press any key to continue...");                   
 	   _settextcolor(7);  //white		  
	   _getch();
	   goto again; 
 	   }	
 			
 	getnum = _inp (controlport);    
    temp=getnum;
 	for (t=0; t<=7; t++)  //should be 7 for figuring out all of the 0 and 1s but it is set to 5 because we only need the first 6 0 and 1s for 3 encoders
		{ remainder = temp % 2;   //find if there is anything left after trying to fit twos into the number
	     temp = temp / 2;      //divide by 2 and save it to use here the next time around
		 binary [t] = remainder;  //put the 1 or 0 into the binary array	
		 //cout<<remainder<<flush;
		}
	
	if (binary [5]==1)   //bit 5 tells if it is biodirectaional or not
	 {_outtext("             This is a biodirectional port.\n\n                 Press any key to continue...");} 
			               
	else{
		 cout<<"             This is not a biodirectional port. "<<"\n\n"
		     <<"             Some I/O cards and motherboards with I/O on"<<"\n"
			 <<"             board allow you to set the parallel port as "<<"\n"
			 <<"             biodirectional with jumper settings or "<<"\n"
			 <<"             with software.  Check your board manufactures"<<"\n"
			 <<"             manual for more information.  Not all parallel"<<"\n"
			 <<"             ports are biodirectaional.  If yours is not you"<<"\n"
			 <<"             will have to purchase a new I/O card that supports"<<"\n"
			 <<"             a biodirectional port to allow you to read from"<<"\n"
			 <<"             encoders."<<flush;  
	     _settextcolor(14);  //yello  
	     _settextposition (24,28);  _outtext("  Press any key to continue...");
		}  
	_settextcolor(7);  //white		  
	_getch();
	goto again;} 
*/ 
 
 if (c==104)  {open_scroll_help(); goto again;}   //help screen  
 if (c==116)//the t key
 	{
	 overlay_box (); 
	 _settextcolor(14); 
 	 _settextcursor (0x0707);     //0x0707 is the regular cursor         
     _settextposition (1,3);  
     _outtext("Time display value is used when IRQ is off.");   
     _settextposition (2,3);  
     _outtext("Dro tries to watch the port and display the");   
     _settextposition (3,3);  
     _outtext("incoming value changes to the screen.  When");  
     _settextposition (4,3);  
     _outtext("info begins to come in too fast the program");  
     _settextposition (5,3);  
     _outtext("will stop updating to the screen and just");  
     _settextposition (6,3);  
     _outtext("concentrate watching the port and counting.");  
     _settextposition (7,3);  
     _outtext("The time display value determines when the");  
     _settextposition (8,3);  
     _outtext("program should quit displaying to the screen");  
     _settextposition (9,3);  
     _outtext("and only concentrate watching the port.  Try");  
     _settextposition (10,3);  
     _outtext("to set this value to the smallest number");  
     _settextposition (11,3);  
     _outtext("without errors showing up.  Between 1 & 300");
     _settextcolor(0);     //red=4 yellow=14 brightwhite=15 normal white =7, black =0      
     _settextposition (13,7); 
     _outtext("Current time display is: ");
     sprintf(tempbuffer, "%d",timedisplay);
     _settextposition (13,33);  
     _settextcolor(4);     //red=4 yellow=14 brightwhite=15 normal white =7, black =0      
     _outtext(tempbuffer);  
     _settextcolor(0);     //red=4 yellow=14 brightwhite=15 normal white =7, black =0   
     _settextposition (15,7);
     _outtext("Enter new time display value:");  
     _settextcolor(4);     //red=4 yellow=14 brightwhite=15 normal white =7, black =0    
     _settextposition (15,37);
     _outtext("     ");   
      temp=intnumbers_only (15,37,"");  //if 9999 is returned it means the user hit esc   
      if (temp!=9999) timedisplay=temp;
      goto again;
      
    } 	
 if (c==105)  //the i key (irq)
 	{ 
 	 //*****************put the irq back the way it was incase the user now changes the irq 
 	 _disable();  //disable interrupts while we work on them
	 _outp (dataport+2, 0);  //clear all the bits to 0 at the control lpt control register
     imask=_inp(33);        //8259 chip it is always this address (decimal 33) on even the old computers
	 if ((imask&imask_bit)==0) imask=imask+imask_bit; //imask_bit is either 32 or 128 //remask the 8259 irq bit    (add these to get to (irq7=128 and irq5==32)
	 _outp (33,imask);
	 _enable(); 
	 //*****************end of put the irq back the way it was
 	 overlay_box ();
 	 _settextcolor(0); 
 	 _settextcursor (0x0707);  //no cursor  //0x0707 is the regular cursor         
     _settextposition (5,9);  
     _outtext("Interrupt is now set at:           "); 
     _settextposition (5,34);
     if (imask_bit == 128) cout<<"IRQ 7"<<"\n\n"<<flush;
     if (imask_bit == 32) cout<<"IRQ 5"<<"\n\n"<<flush;
     _settextposition (7,9); 
 	 _outtext("Two IRQs are available 5 and 7");   
 	 _settextcolor(14); 
 	 _settextposition (20,36);_outtext(" Esc to Cancel");  
 	 _settextcolor(14); 
 	 _settextposition (10,9); 
 	 _outtext("Choose which IRQ to use: ");  
 	 temp=getche();
 	 if (temp==55) 
 	 {imask_bit=128; //irq7 
 	 interrupt_address=15;//interrupt decimal 15 (hex 0x0f)(irq 7) the printer 1 ack pin   // (0x0D is irq 5 (decimal 13)) address to the ack pin
     }
 	 if (temp==53) 
 	 {imask_bit=32; //irq5
 	 interrupt_address=13;//interrupt decimal 15 (hex 0x0f)(irq 7) the printer 1 ack pin   // (0x0D is irq 5 (decimal 13)) address to the ack pin
     }
 	 if ((temp!=53)&&(temp!=55)&&(temp!=ESC))
 	 	{_settextcursor (0x2000);  
 	 	 _settextposition (11,9); _outtext("                           "); 
     	_settextposition (13,3);   
     	_outtext("Nothing done!    Press any key to continue...");
     	 getch();}	
 	
 	
 	}

 if (c==122) //the z key   
     { 
     //_settextcursor (0x0707);  //no cursor  //0x0707 is the regular cursor    //no cursor  0x2000
     overlay_box ();
     _settextposition (6,4); 
	 _settextcolor(0); 
     _outtext("Are you sure you want to rezero all encoders,"); 
     _settextposition (8,4);   
     _outtext("  including Master and Incremental modes?"); 
     
     _settextcolor(14); 
     _settextposition (10,24); _outtext("y/n");
     c=_getch();
     
     
     if ((c!=121)&&(c!=ESC))
 	 	{
 	 	 _settextposition (11,9); _outtext("                           "); 
     	_settextposition (13,3);   
     	_outtext("Nothing done!    Press any key to continue...");
     	 getch();
     	 goto again;}	
    
     if (c==ESC) //not yes, lets escape  
     	{
     	//_settextposition (13,3);   
     	//_outtext("Nothing done!    Press any key to continue...");
     	//getch();
     	goto again; 
     	}  
     	
     encoder1=0; encoder2=0; encoder3=0;   
     mode1.xoffset=0,mode2.xoffset=0,mode3.xoffset=0,mode4.xoffset=0;
     mode1.yoffset=0,mode2.yoffset=0,mode3.yoffset=0,mode4.yoffset=0; 
     mode1.zoffset=0,mode2.zoffset=0,mode3.zoffset=0,mode4.zoffset=0;
     coder1.possible_error_cnt=0; coder2.possible_error_cnt=0; coder3.possible_error_cnt=0;  
     zero_all_X=1,zero_all_Z=1,zero_all_Z=1;  //this turns on the rezero stuff in the backlash function
     //backlash_X.currently_displayed=0;
     goto again;} 
 
 if (c==108)//the  L key    LPT port
     {_settextcursor (0x0707 );    //0x0707 is the regular cursor    //no cursor  0x2000
     overlay_box ();
     _settextcolor(0);  
     _settextposition (5,9);    
 	 _outtext("1: LPT1 decimal 888 (hex 378)");  
 	 _settextposition (6,9);
 	 _outtext("2: LPT2 decimal 632 (hex 278)"); 
 	 _settextposition (7,9);
 	 _outtext("3: LPT3 decimal 956 (hex 3BC)");
 	 _settextposition (8,9);  
 	 _outtext("4: Custom entry of decimal address");    
 	
 	 _settextposition (9,12); _outtext("(for advanced users)");   
 	 _settextcolor(14); 
 	 
 	  _settextposition (20,36);_outtext(" Esc to Cancel"); 
     _settextposition (11,9); _outtext("Choose a Port: ");   
 	 
 	
 	 temp=getch();
 	 if (temp==52)  //number 4
 	 	{_settextcolor(14);  //yello  
 	 	 _settextposition (11,1);
 	 	 _outtext("                                                   ");
 	 	 longdoub=numbers_only (11,40,"Enter Port Decimal Address to use ");     
 	 	 if (longdoub==-99999) goto again;         //-99999 is returned from numbers_only if the user hit the escape key 
 	 	 sprintf(stringg, "%.0f",(double) longdoub); //convert it to a string
     	 cout<<stringg<<flush;
     	 dataport=atoi(stringg); //convert it to an int  and put it in dataport
 	 	}  
 	 if (temp==49)  //number 1
 	 	{dataport=888;}
 	 if (temp==50)  //number 2
 	 	{dataport=632;}	
 	 if (temp==51)  //number 3
 	 	{dataport=956;} 
 	 if ((temp!=52)&&(temp!=49)&&(temp!=50)&&(temp!=51)&&(temp!=ESC))
 	 	{_settextcursor (0x2000);  
 	 	 _settextposition (11,9); _outtext("                           "); 
     	_settextposition (13,3);   
     	_outtext("Nothing done!    Press any key to continue...");
     	 getch();}	
 	 
 	 _outp (dataport, 0);     //send a zero to the data port
 
 	 }          
 	 
 	 
 	 
/*AGE1    */

	  /* AGE1 Config Name   Now lets try to setup the cfg file and filename to write out */	  
 	if (c==77||(c==109))   //the  M or m key    Machine Name 
 	      {
 	
 	        _settextcursor (0x0707 );    //0x0707 is the regular cursor    //no cursor  0x2000
     		overlay_box ();
    		 _settextcolor(0);  
     		_settextposition (5,3);    
 	 		_outtext("Do you want to:");   
 	 		_settextposition (7,3);
 	 		_outtext("Create a new Configuration File.");
 		 	_settextposition (9,3);   
 	  		_outtext("Load a different Configuration File");  
 	  		 	 	 	_settextcolor(4);  
     		_settextposition (7,3);    
 	 		_outtext("C");  
 	 		_settextposition (9,3);    
 	 		_outtext("L"); 
 	 		_settextcursor (0x2000 );    //0x0707 is the regular cursor    //no cursor  0x2000
 	 	 	_settextposition (5,32);  
 	
 	    temp=getch();   
/*AGE1  print out a listing of *.cfg files   */   

_settextwindow(3,10,10,65);    //set screen size to bigh background
_setbkcolor (9);               //red=4 yellow=14 brightwhite=15 normal white =7   black=0   light blue=9
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN 


{
   struct _find_t  c_file;
   int r=4   ;
   int c=0   ;
   
   /* Find first .cfg file in current directory. */
   _dos_findfirst( "*.cfg", _A_NORMAL, &c_file );
   _settextcolor(15);  
   _settextposition (3,13);
   _outtext("List of Current cfg files"); 
   _settextposition (r,c); 
   
    
   _outtext(c_file.name);  
    c+=15 ;
      
    /* Find the rest of the .cfg files. */
   while( _dos_findnext( &c_file ) == 0 ) 
    {         
  //  c+=15 ; 
        
    _settextposition(r,c);     
    _outtext(c_file.name );  
    if (c>=45)
      {c=0;
       r++ ;}   
     else 
     {c+=15 ;}    
        
    
         }
     
} 	    
 	    
 	    
 	    
 	    
 	    
 	    
 	    
 	    if (temp==67||(temp==99))        //c or C for create
     		{     
    /* AGE1  */ 		
     		                  
	 		_settextcursor (0x0707 );    //0x0707 is the regular cursor    //no cursor  0x2000
     		smalloverlay_box ();
    		 _settextcolor(0);  
     		_settextposition (5,3);    
 	 		_outtext("Enter the new Machine Name   ");  
 	 		_outtext(machine);
 		 	_settextposition (8,12);   
 	  		_outtext("Maximum of 8 Characters");  
 	 	 	_settextposition (5,32);
 	 		  
 	 		temp=getch();  
			if (temp==27)goto again; 
			if (temp==13)goto smallend;
			*machinetemp = temp; 
			_outtext(machinetemp);
			_outtext("            ");
			_settextposition (5,33);  
			strcpy(machine , machinetemp);
 	  		small:
 	 	    temp=getch();  
			if (temp==27)goto again; 
			if (temp==13)goto smallend;
			else {
			*machinetemp = temp;   
			_outtext(machinetemp);
			strcat (machine , machinetemp); 
			goto small;	 }	    
 	 	    smallend:
 	        strcpy ( machinecfg , machine  ) ;  
 	 		strcat( machinecfg, ".cfg") ; 
 	 		
 	 		temp = 67 ;  /* force temp to 67 so the program will write the file and then go to again! */
 	 		goto gosaveit  ;     //age1                              
 	  		
 	  		}
 	                               	 
 	    if (temp==76||(temp==108))      // l or L  for load
 	        {  // int result          ;           
	 		_settextcursor (0x0707 );    //0x0707 is the regular cursor    //no cursor  0x2000
             smalloverlay_box ();  
 	 		 _settextcolor(0);      
 	 		 _settextposition (2,2);    
 	 		_outtext("Load an Existing Cfg. File from the list above"); 
 	 		_settextposition (5,3);    
 	 		_outtext("Enter the new Machine Name   ");  
 	 		//_outtext(machine);
 		 	_settextposition (8,12);   
 	  		_outtext("Maximum of 8 Characters");  
 	 		 _settextposition (5,32);
 /*AGE1*/
 	 		 
 	 		temp=getch();  
			if (temp==27)goto again; 
			if (temp==13)goto smallend1;
			*machinetemp = temp;   
			_outtext(machinetemp); 
			_outtext("          ");
			_settextposition (5,33);
			strcpy(machine , machinetemp);
 	  		small1:
 	 	    temp=getch();  
			if (temp==27)goto again; 
			if (temp==13)goto smallend1;
				else {
				*machinetemp = temp;   
				_outtext(machinetemp);
				strcat (machine , machinetemp); 
				goto small1;	 }	    
 	 	    smallend1:
 	        strcpy ( machinecfg , machine  ) ;  
 	 		strcat( machinecfg, ".cfg") ; 		 
   
 	 	
 	 		goto gogetit  ;}   
 
 	     }
 	 
 /*  AGE1  PROGRAM VERSION An Attempt to try to set the 5th bit in the Control port to either a 0 or 1 as necessary and if it sets the bit to add an "A" to the main screen*/
   
     if (c==80||(c==112))//the  P or p key    Version Number
     {
     _settextcursor (0x0707 );    //0x0707 is the regular cursor    //no cursor  0x2000
     overlay_box ();
     _settextcolor(0);  
     _settextposition (5,9);    
 	 _outtext("1: Origional DRO40 execution");  
 	 _settextposition (8,9);
 	 _outtext("2: Version DRO40A execution");   
 	 _settextposition (5,9); 
 	 _settextcolor(4); 
 	 _outtext("1");  
 	 _settextposition (8,9); 
 	 _settextcolor(4); 
 	 _outtext("2");                                                         
 	                                              
	 _settextposition (20,36);_outtext(" Esc to Cancel"); 
     _settextposition (11,9); _outtext("Choose a Version: ");   
     temp=getch();	                                              
 	 if (temp==49)  //number 1
 	 	{versiontype=0;
 	 	versioncontrol=192;}
 	 if (temp==50)  //number 2
 	 	{versiontype=1;
 	 	versioncontrol=32;
 	 	            }	   
 	 	
 	 	

 	                     	 	
 	 	
 	 	
 	 	    
 	 	
 	 if (temp!=49&&(temp!=50))    // what happens if wrong key is struck
 	 	{
 	     _settextcursor (0x0707 );    //0x0707 is the regular cursor    //no cursor  0x2000
     	overlay_box ();
     	_settextcolor(4);  
     	_settextposition (5,9);    
 	 	_outtext("Nothing Done!!") ;
 	 	_settextposition (10,9);  
 	 	  
 	 	_outtext("Press any key to continue");  
 	     
 	    temp=getch(); 
 	     
 	       
 	                                                         
 /*	     goto again;                                         */
	 	                              
 	       
 	  
 	      }
 	  
 	   _outp (controlport, versioncontrol);     //send versiontype to controlport     
 	  
 	  
 	                      
 	                              
 	  /* AGE1 Config Name   Now lets try to setup the cfg file and filename to write out */	  
 	if (c==77||(c==109))   //the  M or m key    Machine Name
     {                        
	 _settextcursor (0x0707 );    //0x0707 is the regular cursor    //no cursor  0x2000
     overlay_box ();
     _settextcolor(0);  
     _settextposition (5,3);    
 	 _outtext("Enter the new Machine Name   ");  
 	 _outtext(machine);
 	 _settextposition (8,12);   
 	  _outtext("Maximum of 8 Characters");  
 	  _settextposition (5,32);
 	 gets(machine);
 	                              
 	  goto again;
 	 }                             
 	                              
 	  goto again;
 	 } 
 	
    
    
    
    
    
   	 
 //if ((c!=27)&&(c!=114)&&(c!=122)&&(c!=116)&&(c!=117)&&(c!=99))   {cout<<"\a";}  //beep
 if (c!=114) goto again;    //if the r key let it go on through to the rest of the program 

 //**************install interrupt here************************************
 if (use_irq==1)  //if the user wants to use the irq then enable it
    {_disable();    //disable all interrupts while we work on them.
 	oldhandler=_dos_getvect (interrupt_address);//save the old interrrupt vector  *******interrupt_address=15;//interrupt decimal 15 (hex 0x0f)(irq 7) the printer 1 ack pin   // (0x0D is irq 5 (decimal 13)) address to the ack pin
 	_dos_setvect (interrupt_address, handler); //install the new interrupt handler   
 	_outp (dataport+2,16);  //enable interrupt  with a 16 everything else is sent a zero (00010000)
 	//imask=_inp(33)&127; _outp(33,imask);//enable the imask bit on the 8259A irq //just sending a 1 like this works too:  _outp(33,1);  //turn on the imask bit
 	imask=_inp(33);
 	if ((imask&imask_bit)!=0) imask=imask-imask_bit; // imask_bit     //turn the 7th mask bit from a 1 to a zero so this interrupt can be used  (irq 7) (subtract 32 for the 5th bit if using irq 5
 	_outp (33,imask);
 	_enable();    //re-enable the interrupts.  We are done working on them
   }
 //****************end of install interrupt*****************************************


 
 _settextcursor (0x2000);  //cursor off
 draw_encode_screen();
 
while(1==1)  //loop forever
 { 	 
  if (coder1.changed==1)   //this will turn on from the InPort function  //also from encode screen redraw
	 {coder1.changed=0;
	 if (backlash_X.onoff==1)
	 	 {shouldweprint=checkbacklash_X(backlash_X.dist, encoder1); //shouldweprint will get the longfloat that should be sent to the screen.  Unless it is a 999 (this means don't print)
	     if (shouldweprint==999) 
	     	{goto skipprint;}
         printfloat=shouldweprint;} 
	 else printfloat=encoder1;   //put encoder into printfloat to be ready to print it
	 backlash_X.currently_displayed=printfloat; //backlash_X.currently_displayed will always be the number that is displayed no matter if backlash is on or off
	                                            //since the modes have been added backlash_X.currently_displayed is only what is displayed on the master mode When other modes are running it still holds what would have been printed on the master mode
	 if (rotary.x_onoff==0)     //don't let it do any of this stuff with in this if   if it if rotary is on
	 	{
	 	switch (mode)
			{case (0):break;  //do nothing   //this is the master mode
		 	case (1):{printfloat=printfloat-mode1.xoffset; break;}  //subtract whatever is about to go to the screen from the saved (xoffset) position the encoder was in when the user reset this mode to zero
		 	case (2):{printfloat=printfloat-mode2.xoffset; break;}
		 	case (3):{printfloat=printfloat-mode3.xoffset; break;}
		 	case (4):{printfloat=printfloat-mode4.xoffset; break;}  
			} 
	 
	 	switch (coder1.conversion) //0 is off 1 is (inch to mm) and 2 is (mm to inch) 
	 	 	{case (0):break;  //do nothing to printfloat 
	 	  	case (1):printfloat=printfloat*25.4; break; //1 inch = 25.4
	 	  	case (2):printfloat=printfloat*.03937; break;//1 mm = .03937              
	     	}  
	    	          
	 	switch (coder1.decimal_places_shown) //how many spaces to the right of the decimal to show (selectable by the user)
	 		{case (0): {sprintf(string, "%.0f",(double) printfloat); break;}//convert printfloat into a string  
	     	case (1): {sprintf(string, "%.1f",(double) printfloat); break;} 
	     	case (2): {sprintf(string, "%.2f",(double) printfloat); break;}
	    	case (3): {sprintf(string, "%.3f",(double) printfloat); break;}
	     	case (4): {sprintf(string, "%.4f",(double) printfloat); break;}
	     	case (5): {sprintf(string, "%.5f",(double) printfloat); break;}  
	     	case (6): {sprintf(string, "%.6f",(double) printfloat); break;} 
	    	} 
	    }  
	 //backlash_X.currently_displayed=printfloat;     
	 if (rotary.x_onoff==1)
	 	{if (rotary.x_show_revolutions==1) {_settextposition(10,30);cout<<"   "<<flush;_settextposition(10,30);cout<<rotary.x_cnt_rotations<<flush;}  
	 	 printfloat=(encoder1*360)/rotary.x_number_of_blips;  //printfloat now holds the decimal degrees  //rotary.x_number_of_blips is number of blips that make up 360 degrees (set by user)
	     if (rotary.x_display_min==0) sprintf(string, "%.4f",(double) printfloat);   //these two decide if it should be displayed in decimal degrees or minute and secs.
	     if (rotary.x_display_min==1) strcpy(string,decimal_to_degrees(printfloat));	 
	    }
	 len=strlen(string);                  //see how many charchacters make up the string
	 //string[len]=' ';                     //copy a nothing char over the last place in the string
	 string[12]='\0';//copy a null 0 to make the 8th place the end of the string //this is all done because when you just send a float to the screen and it changes from neg to positive not all of the text get overwritten with the updated text
	 strncat(string,"  ",6);//will append spaces to the end of the string. the 6 means it will only add in 6 more characters.  If there are more it just drops them. 
	 _settextposition (10,16);
	 cout<<string<<flush;
	 //_outtext (string);
	 //printf("%10s", string); 
	 //_outmem (string,10);
	 if ((coder1.possible_error==1)&&(coder1.error_changed==1))  //if the user has the possible error thingy turned on in the setup screen
	 	{coder1.error_changed=0;
	 	 _settextposition (10,35);
	     cout<<"E"<<coder1.possible_error_cnt<<flush;} 
    }
//===================================================================================================================
//this is marked out to make into the demo.  Don't let them use the y and z axis.  
  if (coder2.changed==1)	 
	 {coder2.changed=0;
	 if (backlash_Y.onoff==1)
	 	 {shouldweprint=checkbacklash_Y(backlash_Y.dist, encoder2); //shouldweprint will get the longfloat that should be sent to the screen.  Unless it is a 999 (this means don't print)
	     if (shouldweprint==999) 
	     	{goto skipprint;}
         printfloat=shouldweprint;} 
	 else printfloat=encoder2;   //put encoder into printfloat to be ready to print it
	 backlash_Y.currently_displayed=printfloat; 
	 
  	 if (rotary.y_onoff==0)   
	 	{
	 	switch (mode)
			{case (0):break;  //do nothing   //this is the master mode
		 	case (1):{printfloat=printfloat-mode1.yoffset; break;}  //subtract whatever is about to go to the screen from the saved (xoffset) position the encoder was in when the user reset this mode to zero
		 	case (2):{printfloat=printfloat-mode2.yoffset; break;}
		 	case (3):{printfloat=printfloat-mode3.yoffset; break;}
		 	case (4):{printfloat=printfloat-mode4.yoffset; break;}  
			}  
	 	switch (coder2.conversion) //0 is off 1 is (inch to mm) and 2 is (mm to inch) 
	 	 	{case (0):break;  //do nothing to printfloat 
	 	  	case (1):printfloat=printfloat*25.4;  break;//1 inch = 25.4
	 	  	case (2):printfloat=printfloat*.03937; break;//1 mm = .03937              
	     	}            
	 	switch (coder2.decimal_places_shown)
	 		{case (0): {sprintf(string, "%.0f",(double) printfloat); break;}//convert printfloat into a string  
	     	case (1): {sprintf(string, "%.1f",(double) printfloat); break;} 
	     	case (2): {sprintf(string, "%.2f",(double) printfloat); break;}
	     	case (3): {sprintf(string, "%.3f",(double) printfloat); break;}
	     	case (4): {sprintf(string, "%.4f",(double) printfloat); break;}
	     	case (5): {sprintf(string, "%.5f",(double) printfloat); break;}  
	     	case (6): {sprintf(string, "%.6f",(double) printfloat); break;} 
	    	}
	 	}   
	 if (rotary.y_onoff==1)
	 	{if (rotary.y_show_revolutions==1) {_settextposition(12,30);cout<<"   "<<flush;_settextposition(12,30);cout<<rotary.y_cnt_rotations<<flush;}  
	 	 printfloat=(encoder2*360)/rotary.y_number_of_blips;  //printfloat now holds the decimal degrees  //rotary.x_number_of_blips is number of blips that make up 360 degrees (set by user)
	     if (rotary.y_display_min==0) sprintf(string, "%.4f",(double) printfloat);   //these two decide if it should be displayed in decimal degrees or minute and secs.
	     if (rotary.y_display_min==1) strcpy(string,decimal_to_degrees(printfloat));	 
	    }	 
	 len=strlen(string);                  //see how many charchacters make up the string
	 //string[len]=' ';                     //copy a nothing char over the last place in the string
	 string[10]='\0';//copy a null 0 to make the 8th place the end of the string //this is all done because when you just send a float to the screen and it changes from neg to positive not all of the text get overwritten with the updated text
	 strncat(string,"  ",6);//will append spaces to the end of the string. the 6 means it will only add in 6 more characters.  If there are more it just drops them. 
	 _settextposition (12,16);
	 cout<<string<<flush;  
	 if ((coder2.possible_error==1)&&(coder2.error_changed==1)) 
	 	{_settextposition (12,35);
	     cout<<"E"<<coder2.possible_error_cnt<<flush;}   
	}     
//=======================================================================================================================	 
  if (coder3.changed==1)	 
	 {coder3.changed=0;
	 if (backlash_Z.onoff==1)
	 	 {shouldweprint=checkbacklash_Z(backlash_Z.dist, encoder3); //shouldweprint will get the longfloat that should be sent to the screen.  Unless it is a 999 (this means don't print)
	     if (shouldweprint==999) 
	     	{goto skipprint;}
         printfloat=shouldweprint;} 
	 else printfloat=encoder3;   //put encoder into printfloat to be ready to print it
	 backlash_Z.currently_displayed=printfloat;    
	 
	 if (rotary.z_onoff==0)  
	 	{
	 	switch (mode)
			{case (0):break;  //do nothing   //this is the master mode
			 case (1):{printfloat=printfloat-mode1.zoffset; break;}  //subtract whatever is about to go to the screen from the saved (xoffset) position the encoder was in when the user reset this mode to zero
		 	 case (2):{printfloat=printfloat-mode2.zoffset; break;}
			 case (3):{printfloat=printfloat-mode3.zoffset; break;}
			 case (4):{printfloat=printfloat-mode4.zoffset; break;}  
			} 
		 switch (coder3.conversion) //0 is off 1 is (inch to mm) and 2 is (mm to inch) 
	 		 {case (0):break;  //do nothing to printfloat 
	 	 	 case (1):printfloat=printfloat*25.4;  break;//1 inch = 25.4
	 	 	 case (2):printfloat=printfloat*.03937; break;//1 mm = .03937              
	    	 }            
	 	switch (coder3.decimal_places_shown)
	 		{case (0): {sprintf(string, "%.0f",(double) printfloat); break;}//convert printfloat into a string  
	     	case (1): {sprintf(string, "%.1f",(double) printfloat); break;} 
	    	 case (2): {sprintf(string, "%.2f",(double) printfloat); break;}
	    	 case (3): {sprintf(string, "%.3f",(double) printfloat); break;}
	    	 case (4): {sprintf(string, "%.4f",(double) printfloat); break;}
	    	 case (5): {sprintf(string, "%.5f",(double) printfloat); break;}  
	    	 case (6): {sprintf(string, "%.6f",(double) printfloat); break;} 
	    	}
	    }	
	 if (rotary.z_onoff==1)
	 	{if (rotary.z_show_revolutions==1) {_settextposition(14,30);cout<<"   "<<flush;_settextposition(14,30);cout<<rotary.z_cnt_rotations<<flush;}  
	 	 printfloat=(encoder3*360)/rotary.z_number_of_blips;  //printfloat now holds the decimal degrees  //rotary.x_number_of_blips is number of blips that make up 360 degrees (set by user)
	     if (rotary.z_display_min==0) sprintf(string, "%.4f",(double) printfloat);   //these two decide if it should be displayed in decimal degrees or minute and secs.
	     if (rotary.z_display_min==1) strcpy(string,decimal_to_degrees(printfloat));	 
	    }
    
	 len=strlen(string);                  //see how many charchacters make up the string
	 //string[len]=' ';                     //copy a nothing char over the last place in the string
	 string[10]='\0';//copy a null 0 to make the 8th place the end of the string //this is all done because when you just send a float to the screen and it changes from neg to positive not all of the text get overwritten with the updated text
	 strncat(string,"  ",6);//will append spaces to the end of the string. the 6 means it will only add in 6 more characters.  If there are more it just drops them. 
	 _settextposition (14,16);
	 cout<<string<<flush;
	 if ((coder3.possible_error==1)&&(coder3.error_changed==1)) 
	 	{_settextposition (14,35);
	     cout<<"E"<<coder3.possible_error_cnt<<flush;}      
	}   
 //end of stuffed marked out to make into demo
//_settextposition (12,16);     //these two show disabled at y and z
//	 cout<<"Disabled"<<flush; 	
//_settextposition (14,16);
//	 cout<<"Disabled"<<flush;  
 	 

zero_all_X_mode=0;zero_all_Y_mode=0;zero_all_Z_mode=0;  //if the user enters a new value in master mode 0 this backlash thing will get turned on (it will not get turned off unless backlash is turned on so I shut them off here in case they are on
zero_all_X=0;zero_all_Y=0;zero_all_Z=0;    
//=======================================================================================================================
skipprint: _settextposition (14,16);  //in case you decide not to print go to here.  The settext statement is to get the text to where it would have been if you did print
     if (use_irq==0) InPort (timedisplay);   //if we are not useing an irq
 /*  ///////////////////////////////////////////////////////
   //this time stuff is to rezero without asking every 15 minutes becasue this is a demo  
     currenttime=time(NULL);
     if (currenttime > holdtime+900)    //rezero every 15 minutes       900 seconds is 15 minutes
         {
         _settextposition (17,8); 
         cout<<"   Unregistered version"<<flush; 
         _settextposition (18,8);  
	     cout<<"    X has been zeroed"<<flush;     
	     _settextposition (19,8); 
	     cout<<"Press any key to continue..."<<flush;   
	     _getch();    
	     currenttime=time(NULL);  
	     holdtime=currenttime;   //this will give them 15 minutes from when the press a key to continue.  Otherwise if they just let it run for hours it will store up a bunch of 10minutes
	     quick_rezero ('x'); 
         draw_encode_screen();
         } 
     //_settextposition (15,16);
     //cout<<checktime<<flush;      
 */  //////////////////////////////////////////time stuff for demo ends here  
     if (_bios_keybrd (_KEYBRD_READY))
        {c=_getch(); if (c==0) c=_getch();  if(c==27) goto again; //27 is the esc key //have to getch(); twice in order to get function keys like F1 or F2.  First one getch(); is a 0 and the next is the decimal of the function key. 
    // cout<<c<<flush;
     	if ((c==120)||(c==121)||(c==122))//the x y or z key
     		{letter=c;  //turn the key pushed into the character  pushed
     	    if (((rotary.x_onoff==1)&&(letter=='x'))||((rotary.y_onoff==1)&&(letter=='y'))||((rotary.z_onoff==1)&&(letter=='z')))  
     	     		{switch (letter)
	       				{case ('x'): {if (rotary.x_onoff==1) {rotary_setup(letter);draw_encode_screen();} 
	       				 			
	       				 			  break;}
	             		 case ('y'): {if (rotary.y_onoff==1) {rotary_setup(letter);draw_encode_screen();} 
	             		 
	             		 			  break;}
	             		 case ('z'): {if (rotary.z_onoff==1) {rotary_setup(letter);draw_encode_screen();}
	             		 
	             		 			  break;}
	            		}
     	            }
     	     
     	    else {if (mode==0)
     	      		  {encoder_setup(letter); //master mode setup screen
     			       draw_encode_screen();}
     			   if (mode!=0)
     		 	      {mode_setup(letter);   //mode a b c and d setup screen
     		 		   draw_encode_screen();} 
     		 	  } 	    
     		}
        if (c==104)  {open_scroll_help(); draw_encode_screen();}   //help screen
        
        if (c==59)   //the F1    quick rezero
        	{quick_rezero ('x');
        	 draw_encode_screen();
        	} 
        if (c==60)   //the F2    quick rezero
        	{quick_rezero ('y');
        	 draw_encode_screen();
        	} 
        if (c==61)   //the F3 key combination    quick rezero
        	{quick_rezero ('z');
        	 draw_encode_screen();
        	} 		
        	 
        if ((c==32)||(c==109))   //the space bar key  or the m key (mode)
	       {switch (mode)
	       		{case (0): {mode=1; break;}
	             case (1): {mode=2; break;}
	             case (2): {mode=3; break;}
	             case (3): {mode=4; break;}
	             case (4): {mode=0; break;}
	            }
			draw_encode_screen(); 
	       }
        if (c==114) //the r key (rotary)
        	{ 
        	 _settextcolor(14); //yellow   
        	 _settextposition(18,1);
        	 _outtext("       For which axis? X, Y or Z"); 
        	 _settextposition(18,1);
        	 c=_getch();
        	 letter=c;  //turn the key pressed into the character pressed
        	 if (c==120) //the x key
        	 	{if (rotary.x_onoff==1) 
        	 		{_outtext("  Rotary for X encoder is already on.\n"); 
        	 		 _outtext("  Use the 'x' key to enter its setup.\n\n\n"); 
        	 		 _settextcolor(15);//red=4 yellow=14 brightwhite=15 normal white =7
        	 		 _outtext("     Press any key to continue..."); 
        	 		 _getch();
        	 		}
        	     if (rotary.x_onoff==0) 
        	     	{rotary.x_onoff=1;
        	     	 rotary_setup(letter);
        	     	}
        	    } 
        	 if (c==121) //the y key
        	 	{if (rotary.y_onoff==1) 
        	 		{_outtext("  Rotary for Y encoder is already on.\n"); 
        	 		 _outtext("  Use the 'y' key to enter its setup.\n\n\n"); 
        	 		 _settextcolor(15);//red=4 yellow=14 brightwhite=15 normal white =7
        	 		 _outtext("     Press any key to continue..."); 
        	 		 _getch();
        	 		}
        	     if (rotary.y_onoff==0) 
        	     	{rotary.y_onoff=1;
        	     	 rotary_setup(letter);
        	     	}
        	    } 
        	  if (c==122) //the z key
        	 	{if (rotary.z_onoff==1) 
        	 		{_outtext("  Rotary for Z encoder is already on.\n"); 
        	 		 _outtext("  Use the 'z' key to enter its setup.\n\n\n"); 
        	 		 _settextcolor(15);//red=4 yellow=14 brightwhite=15 normal white =7
        	 		 _outtext("     Press any key to continue..."); 
        	 		 _getch();
        	 		}
        	     if (rotary.z_onoff==0) 
        	     	{rotary.z_onoff=1;
        	     	 rotary_setup(letter);
        	     	}
        	    }      	
            draw_encode_screen();
        	} 
     
     
     	}                                      
     	
  } //end of loop forever
goto again;			
return 0;
}  //end of main

 /*          X Axis Rotary Settings   
          
     C     Rotary Counting on/off  
     R     Reset to zero
     V     Current value in decimal
     P     Display minutes, secs (Display decimal degrees)
     N     Num of counts in 360 degrees  
     Z     Zero when at 360 degress on/off  
     D     Count direction +-
  
  
      .5 degreess = 30 sec and 0 sec


                                    */


void rotary_setup(char xyzletter)
{
    long double decimal, longdouble; 
    char letter[2]; 
    char *capital;
    char string[25];
    int a=0,cc,save_rotation_cnt;  
    long double save_1;
    long int save_error;
   
    
    
    upper: //line name I use it to return back to sometimes to redraw everthing
    _clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   
    _settextwindow(1,1,25,40);    //largest is 1,1,25,40//this would be full screen
    _setbkcolor (7);    
    _clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN    
    letter[0]= xyzletter;
    letter[1]='\0';  
    capital = _strupr(letter);
    _settextcolor(15);    
    _setbkcolor (1);
    _outtext("                                        ");     
    _settextposition (1,10);  
    _outtext (capital);
    _outtext (" Axis Rotary Settings ");
    _outtext("\n\n\n\n");
    _setbkcolor (7);
    _settextcolor(0);    //black
    _outtext("    Rotary Counting\n\n");
    _outtext("    Reset to Zero\n\n");
    _outtext("    Current Value\n\n");
    _outtext("    Display   \n\n");
    _outtext("    Num. in 360 degrees\n\n");
    _outtext("    Show Revolutions\n\n");    
    _outtext("    Count Direction\n\n"); 
    
    _settextcolor(4);  //red     
    _settextposition (5,12);   
    _outtext("C");   
    _settextposition (7,5); 
    _outtext("R"); 
    _settextposition (9,13); 
    _outtext("V");  
    _settextposition (11,8);   
    _outtext("p");   
    _settextposition (13,5); 
    _outtext("N"); 
    _settextposition (15,5); 
    _outtext("S");          
    _settextposition (17,11); 
    _outtext("D");          
    _settextposition (25,1); 
    _settextcolor(0);    //black
    _outtext("                        Esc to return"); 
    _settextcolor(14); //yellow  
 
    _settextposition (9,25); 
    _outtext("              ");  
    
     
    //when user turns the coder to neg numbers (do this)
    //as soon as you see that the encoder1 value is a neg number(-1 would be the first count)
    //subract 1 from the total of rotary.x_number_of_blips and reset encoder one to that value
    //So if the number_of_blips is set to 700 and the user turns the coder from 0 to -1 then set encoder1 to 699
    //So as the user continues to turn it that way it will be counting down (698,697...) this will be displaying
    //the degrees counting backwards.  
    
    //blips	  encoder                //cross multipy to figure decimal degrees
    //-----   ------
    // 360 	  decimal degrees
    
    _settextposition (5,25); 
   	switch (xyzletter)
    			{case ('x'): {if (rotary.x_onoff==1) _outtext("ON ");  
    	 		 			  else _outtext("OFF");  break;}		
    	 		 case ('y'): {if (rotary.y_onoff==1) _outtext("ON ");  
    	 		 			  else _outtext("OFF");  break;}		
         		 case ('z'): {if (rotary.z_onoff==1) _outtext("ON ");  
    	 		 			  else _outtext("OFF");  break;}	
        		} 
   	
   	_settextposition (9,25);
   	switch (xyzletter)
   		{case ('x'): {if (rotary.x_display_min==1)      //1 is display minutes and secs. and 0 is display decimal  
    				 	{strcpy(string,decimal_to_degrees((encoder1*360)/rotary.x_number_of_blips));
    					 cout<<string<<flush;
    					} 
     				  else cout<<(encoder1*360)/rotary.x_number_of_blips<<flush;    //decimal degrees 
     				  break;}
	     case ('y'): {if (rotary.y_display_min==1)      //1 is display minutes and secs. and 0 is display decimal  
    				 	{strcpy(string,decimal_to_degrees((encoder2*360)/rotary.y_number_of_blips));
    					 cout<<string<<flush;
    					} 
     				  else cout<<(encoder2*360)/rotary.z_number_of_blips<<flush;    //decimal degrees 
     				  break;}
	     case ('z'): {if (rotary.z_display_min==1)      //1 is display minutes and secs. and 0 is display decimal  
    				 	{strcpy(string,decimal_to_degrees((encoder3*360)/rotary.z_number_of_blips));
    					 cout<<string<<flush;
    					} 
     				  else cout<<(encoder3*360)/rotary.z_number_of_blips<<flush;    //decimal degrees 
     				  break;}
	    } 
    _settextposition (11,25); 
  //  if (rotary.x_display_min==1) _outtext("minutes, secs   ");
  //  if (rotary.x_display_min==0) _outtext("decimal degrees ");
    switch (xyzletter)
    			{case ('x'): {if (rotary.x_display_min==1) _outtext("minutes, secs   "); 
    	 		 			  if (rotary.x_display_min==0) _outtext("decimal degrees ");break;}		
    	 		 case ('y'): {if (rotary.y_display_min==1) _outtext("minutes, secs   "); 
    	 		 			  if (rotary.y_display_min==0) _outtext("decimal degrees ");break;}	
         		 case ('z'): {if (rotary.z_display_min==1) _outtext("minutes, secs   "); 
    	 		 			  if (rotary.z_display_min==0) _outtext("decimal degrees ");break;}		
        		} 
   
   
    _settextposition (13,25);  
    _outtext("           "); 
    _settextposition (13,25);
    switch (xyzletter)
    	{case ('x'): {cout<<rotary.x_number_of_blips<<flush; break;}
    	 case ('y'): {cout<<rotary.y_number_of_blips<<flush; break;}
         case ('z'): {cout<<rotary.z_number_of_blips<<flush; break;}
        } 
    _settextposition (15,25); 
    _outtext("           "); 
    _settextposition (15,25);   
    switch (xyzletter)
    	{case ('x'): {if (rotary.x_show_revolutions==1) {cout<<"ON"<<flush; break;} 
    	              if (rotary.x_show_revolutions==0) {cout<<"OFF"<<flush; break;}} 
    	 case ('y'): {if (rotary.y_show_revolutions==1) {cout<<"ON"<<flush; break;} 
    	              if (rotary.y_show_revolutions==0) {cout<<"OFF"<<flush; break;}} 
         case ('z'): {if (rotary.z_show_revolutions==1) {cout<<"ON"<<flush; break;} 
    	              if (rotary.z_show_revolutions==0) {cout<<"OFF"<<flush; break;}} 
        } 
    _settextposition (17,25); 
    _outtext("           "); 
    _settextposition (17,25);   
    
    if  (xyzletter=='x')
    	{if (coder1.cnt_updown==0) cout<<"-"<<flush; 
    	 if (coder1.cnt_updown==1) cout<<"+"<<flush;}   
    if  (xyzletter=='y') 
    	{if (coder2.cnt_updown==0) cout<<"-"<<flush; 
    	 if (coder2.cnt_updown==1) cout<<"+"<<flush;}  
    if  (xyzletter=='z')  
    	{if (coder3.cnt_updown==0) cout<<"-"<<flush; 
    	 if (coder3.cnt_updown==1) cout<<"+"<<flush;} 
    do {
    	cc=_getch();
    	_settextposition (19,1); 
    	
       
       
       
        if (cc==99)  //the c key  rotary counting on/off
         	{ 
            switch (xyzletter)
    			{case ('x'): {if (rotary.x_onoff==1) rotary.x_onoff=0;  
    	 		 			  else rotary.x_onoff=1;  break;}		
    	 		 case ('y'): {if (rotary.y_onoff==1) rotary.y_onoff=0;  
    	 		 			  else rotary.y_onoff=1;  break;}		
         		 case ('z'): {if (rotary.z_onoff==1) rotary.z_onoff=0;  
    	 		 			  else rotary.z_onoff=1;  break;}		
        		} 
        	goto upper;	
            }		 
        
        if (cc==114)  //the r key  reset to zero
    		{
    		 if (a==0) a=1;  //the a just toggles back and forth
    		 else a=0;  
    	//	 _settextposition (8,24); 
    	//	 cout<<"                "<<flush;  
    		 _settextposition (9,25); 
    		 if (a==1) cout<<"0             "<<flush;    
    		 
    		 if (xyzletter=='x')  
    		 	{if (a==1)  //0 is undo and 1 is rezero
    		 		{
     		 		 save_rotation_cnt=rotary.x_cnt_rotations;
    		 		 rotary.x_cnt_rotations=0;
     		 		 save_1=encoder1;
    		 		 encoder1=0; _settextposition (7,25); _outtext("Now Zero    ");
    		 		 save_error=coder1.possible_error_cnt;//save the error count
    		 		 coder1.possible_error_cnt=0;} //set the error counter back to zero too
    		 	 else {encoder1=save_1; _settextposition (7,25); _outtext("Zero Undone ");
    		 	 	  if (rotary.x_display_min==1)      //1 is display minutes and secs. and 0 is display decimal  
    				 	{strcpy(string,decimal_to_degrees((encoder1*360)/rotary.x_number_of_blips));
    					 _settextposition (9,25);
    					 cout<<string<<flush;
    					} 
     				  else {_settextposition (9,25);cout<<(encoder1*360)/rotary.x_number_of_blips<<flush; }   //decimal degrees 
    		 	 	  rotary.x_cnt_rotations=save_rotation_cnt; 
    		 	 	  coder1.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	  }
    		 	 }
      	    if (xyzletter=='y')  
    		 	{if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		 		 save_rotation_cnt=rotary.y_cnt_rotations;
    		 		 rotary.y_cnt_rotations=0;
    		 		 save_1=encoder2;
    		 		 encoder2=0; _settextposition (7,25); _outtext("Now Zero    ");
    		 		 save_error=coder2.possible_error_cnt;//save the error count
    		 		 coder2.possible_error_cnt=0;} //set the error counter back to zero too
    		 	 else {encoder2=save_1; _settextposition (7,25); _outtext("Zero Undone ");
    		 	 	  if (rotary.y_display_min==1)      //1 is display minutes and secs. and 0 is display decimal  
    				 	{strcpy(string,decimal_to_degrees((encoder2*360)/rotary.y_number_of_blips));
    					 _settextposition (9,25);
    					 cout<<string<<flush;
    					} 
     				  else {_settextposition (9,25);cout<<(encoder2*360)/rotary.y_number_of_blips<<flush; }   //decimal degrees 
    		 	 	  rotary.y_cnt_rotations=save_rotation_cnt; 
    		 	 	  coder2.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	  }
    		 	 } 
    		if (xyzletter=='z')  
    		 	{if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		         save_rotation_cnt=rotary.z_cnt_rotations;
    		 		 rotary.z_cnt_rotations=0;
    		 		 save_1=encoder3;
    		 		 encoder3=0; _settextposition (7,25); _outtext("Now Zero    ");
    		 		 save_error=coder3.possible_error_cnt;//save the error count
    		 		 coder3.possible_error_cnt=0;} //set the error counter back to zero too
    		 	 else {encoder3=save_1; _settextposition (7,25); _outtext("Zero Undone ");
    		 	 	  if (rotary.z_display_min==1)      //1 is display minutes and secs. and 0 is display decimal  
    				 	{strcpy(string,decimal_to_degrees((encoder3*360)/rotary.z_number_of_blips));
    					 _settextposition (9,25);
    					 cout<<string<<flush;
    					} 
     				  else {_settextposition (9,25);cout<<(encoder3*360)/rotary.z_number_of_blips<<flush; }   //decimal degrees 
    		 	 	  rotary.z_cnt_rotations=save_rotation_cnt; 
    		 	 	  coder3.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	  }
    		 	 }
    		 }      
      
      
      
      
        if (cc==100)//the d key for count other way (direction) -+    
    	   { 
    	     if (xyzletter=='x')   //the x key        
    		     {
    		      if (coder1.cnt_updown==0) {coder1.cnt_updown=1; }
    	          else {coder1.cnt_updown=0; }
    	        //  encoder1 = 0-encoder1;//subtract by 0 to swap the sign of the number -/+
    	          if (coder1.PrevValue==0) coder1.PrevValue=3;
    	          if (coder1.PrevValue==1) coder1.PrevValue=2;
    	          if (coder1.PrevValue==2) coder1.PrevValue=1;
    	          if (coder1.PrevValue==3) coder1.PrevValue=0;
    	          if (coder1.PrevPrevValue==0) coder1.PrevPrevValue=3;
    	          if (coder1.PrevPrevValue==1) coder1.PrevPrevValue=2;
    	          if (coder1.PrevPrevValue==2) coder1.PrevPrevValue=1;
    	          if (coder1.PrevPrevValue==3) coder1.PrevPrevValue=0; 
    	          if (coder1.PrevPrevPrevValue==0) coder1.PrevPrevPrevValue=3;
    	          if (coder1.PrevPrevPrevValue==1) coder1.PrevPrevPrevValue=2;
    	          if (coder1.PrevPrevPrevValue==2) coder1.PrevPrevPrevValue=1;
    	          if (coder1.PrevPrevPrevValue==3) coder1.PrevPrevPrevValue=0;     
    	         }
    	     if (xyzletter=='y')   //the x key        
    		     {
    		      if (coder2.cnt_updown==0) {coder2.cnt_updown=1; }
    	          else {coder2.cnt_updown=0; }
    	       //   encoder2 = 0-encoder2;//subtract by 0 to swap the sign of the number -/+    	          
    	          if (coder2.PrevValue==0) coder2.PrevValue=3;
    	          if (coder2.PrevValue==1) coder2.PrevValue=2;
    	          if (coder2.PrevValue==2) coder2.PrevValue=1;
    	          if (coder2.PrevValue==3) coder2.PrevValue=0;
    	          if (coder2.PrevPrevValue==0) coder2.PrevPrevValue=3;
    	          if (coder2.PrevPrevValue==1) coder2.PrevPrevValue=2;
    	          if (coder2.PrevPrevValue==2) coder2.PrevPrevValue=1;
    	          if (coder2.PrevPrevValue==3) coder2.PrevPrevValue=0; 
    	          if (coder2.PrevPrevPrevValue==0) coder2.PrevPrevPrevValue=3;
    	          if (coder2.PrevPrevPrevValue==1) coder2.PrevPrevPrevValue=2;
    	          if (coder2.PrevPrevPrevValue==2) coder2.PrevPrevPrevValue=1;
    	          if (coder2.PrevPrevPrevValue==3) coder2.PrevPrevPrevValue=0;     
    	         }  
    	     if (xyzletter=='z')   //the x key        
    		     {
    		      if (coder3.cnt_updown==0) {coder3.cnt_updown=1; }
    	          else {coder3.cnt_updown=0; }
    	      //    encoder3 = 0-encoder3;//subtract by 0 to swap the sign of the number -/+
       	          if (coder3.PrevValue==0) coder3.PrevValue=3;
    	          if (coder3.PrevValue==1) coder3.PrevValue=2;
    	          if (coder3.PrevValue==2) coder3.PrevValue=1;
    	          if (coder3.PrevValue==3) coder3.PrevValue=0;
    	          if (coder3.PrevPrevValue==0) coder3.PrevPrevValue=3;
    	          if (coder3.PrevPrevValue==1) coder3.PrevPrevValue=2;
    	          if (coder3.PrevPrevValue==2) coder3.PrevPrevValue=1;
    	          if (coder3.PrevPrevValue==3) coder3.PrevPrevValue=0; 
    	          if (coder3.PrevPrevPrevValue==0) coder3.PrevPrevPrevValue=3;
    	          if (coder3.PrevPrevPrevValue==1) coder3.PrevPrevPrevValue=2;
    	          if (coder3.PrevPrevPrevValue==2) coder3.PrevPrevPrevValue=1;
    	          if (coder3.PrevPrevPrevValue==3) coder3.PrevPrevPrevValue=0;     
    	         }              
    	   
    	  goto upper;
    	  }      
    
    
    
    
    	if (cc==112) //the p key //display minutes and seconds or display decimal degrees        rotary.x_display_min=0; 
    	    {switch (xyzletter)
    		 		 {case ('x'): {if (rotary.x_display_min==1) rotary.x_display_min=0; 
    		 					  else rotary.x_display_min=1;
    		 					  break;} 
    				 case ('y'): {if (rotary.y_display_min==1) rotary.y_display_min=0; 
    		 					  else rotary.y_display_min=1;
    		 					  break;} 
    				 case ('z'): {if (rotary.z_display_min==1) rotary.z_display_min=0; 
    		 					  else rotary.z_display_min=1;
    		 					  break;} 
    	 	        }
       	    goto upper;  //redraw screen
       		} 
    	 
    		
    	if (cc==115)  //s show revolutions  
    	    {switch (xyzletter)
    		 		 {case ('x'): {if (rotary.x_show_revolutions==1) rotary.x_show_revolutions=0; 
    		 					  else rotary.x_show_revolutions=1;
    		 					  break;} 
    				 case ('y'): {if (rotary.y_show_revolutions==1) rotary.y_show_revolutions=0; 
    		 					  else rotary.y_show_revolutions=1;
    		 					  break;} 
    				 case ('z'): {if (rotary.z_show_revolutions==1) rotary.z_show_revolutions=0; 
    		 					  else rotary.z_show_revolutions=1;
    		 					  break;} 
    	 	        }
       	    goto upper;  //redraw screen
       		} 
    	
    	
    	
    	
    	
    	
    	if (cc==118)  //v current value   
    	    {_settextcursor (0x0707);  //cursor on
    	    _settextposition (19,1);   
    	    switch(xyzletter)
    	    	{case ('x'):
    				{
    				if (rotary.x_display_min==1)     //in display min seconds
    		    		{//_outtext("   Enter degrees, minutes, seconds\n");
				 		//_outtext("   Seperate by comas and no spaces\n"); 
				 		_settextposition (22,15); 
				 		_outtext("            ");
				 		//_settextposition (22,15); 
				 		decimal=auto_degrees_decimal("   Enter degrees, minutes, seconds\n   Seperate by comas and no spaces\n",19,1,22,15);
				 		//cin>>string; 
				 		//decimal=degrees_to_decimal(string);  //decimal holds decimal degrees 
    					 if (decimal==-99999) break;    //user hit escape  
    					 //cout<<decimal<<flush;
    					 encoder1=(decimal*rotary.x_number_of_blips)/360; 
						} 
            		if (rotary.x_display_min==0)     //in display decimal
    		    		{
						 _settextposition (21,1); 
				 		_outtext("                                            ");
						 decimal=numbers_only (21, 25,"Enter decimal degrees ");   
    					 if (decimal==-99999) break;    //user hit escape
    					 encoder1=(decimal*rotary.x_number_of_blips)/360;
       					}
       				break;	 
       				}	 
       			case ('y'):
    				{
    				if (rotary.y_display_min==1)     //in display min seconds
    		    		{//_outtext("   Enter degrees, minutes, seconds\n");
				 		//_outtext("   Seperate by comas and no spaces\n"); 
				 		_settextposition (22,15); 
				 		_outtext("            "); 
				 		decimal=auto_degrees_decimal("   Enter degrees, minutes, seconds\n   Seperate by comas and no spaces\n",19,1,22,15);
				 	//	_settextposition (22,15);
				 	//	cin>>string; 
				 	//	decimal=degrees_to_decimal(string);  //decimal holds decimal degrees 
    					 if (decimal==-99999) break;    //user hit escape
    					 encoder2=(decimal*rotary.y_number_of_blips)/360; 
						} 
            		if (rotary.y_display_min==0)     //in display decimal
    		    		{
    		    		 _settextposition (21,1); 
				 		 _outtext("                                            ");
						 decimal=numbers_only (21, 25,"Enter decimal degrees ");   
    					 if (decimal==-99999) break;    //user hit escape
    					 encoder2=(decimal*rotary.y_number_of_blips)/360;
       					}
       				break;	  
       				}	
       			case ('z'):
    				{
    				if (rotary.z_display_min==1)     //in display min seconds
    		    		{//_outtext("   Enter degrees, minutes, seconds\n");
				 		//_outtext("   Seperate by comas and no spaces\n");
				 		_settextposition (22,15); 
				 		_outtext("            "); 
				 		decimal=auto_degrees_decimal("   Enter degrees, minutes, seconds\n   Seperate by comas and no spaces\n",19,1,22,15);
				 		//_settextposition (22,15); 
				 		
				 		//cin>>string; 
				 		//decimal=degrees_to_decimal(string);  //decimal holds decimal degrees 
    					if (decimal==-99999) break;    //user hit escape 
    					 encoder3=(decimal*rotary.z_number_of_blips)/360; 
						} 
            		if (rotary.z_display_min==0)     //in display decimal
    		    		{
						 _settextposition (21,1); 
				 		 _outtext("                                            ");
						 decimal=numbers_only (21, 25,"Enter decimal degrees ");   
    					 if (decimal==-99999) break;    //user hit escape
    					 encoder3=(decimal*rotary.z_number_of_blips)/360;
       					} 
       				break;			
        			} 
        		}	
            _settextcursor (0x2000);  //cursor off   
           goto upper;
            } 
        if(cc==110)  //n key number of blips in 360
        	{_settextcursor (0x0707);  //cursor on  
            //_settextposition (19,1);
            _settextposition (21,1); 
			_outtext("                                            ");
            switch(xyzletter)
    	    	{case ('x'): {longdouble=numbers_only (21, 29,"# of counts in 360 degrees "); 
    	    				  if (longdouble==-99999) break;
    	    				  rotary.x_number_of_blips=(int)longdouble;break;}
    	    	 case ('y'): {longdouble=numbers_only (21, 34,"# of counts in 360 degrees "); 
    	    	 			  if (longdouble==-99999) break;
    	    				  rotary.y_number_of_blips=(int)longdouble;break;}
    	    	 case ('z'): {longdouble=numbers_only (21, 34,"# of counts in 360 degrees "); 
    	    				  if (longdouble==-99999) break;
    	    				  rotary.z_number_of_blips=(int)longdouble;break;}
    	    	}
    	    _settextcursor (0x2000);  //cursor off   
            goto upper;
    	    }    	    				    
          
       }while (cc!=27);// only go back after the esc key	
}

    //blips	  encoder                //cross multipy to figure decimal degrees
    //-----   ------
    // 360 	  decimal degrees

void mode_setup(char xyzletter) 
{
    char letter[2]; 
    char *capital;
    char string[25];
    int cc, a=0;
    long double save_offset; 
    //long double save_1, save_2, save_3; 
    //long int save_error; 
    long double  dif, longdouble;
    int x_save_tension=backlash_X.tension;
	int y_save_tension=backlash_Y.tension;
	int z_save_tension=backlash_Z.tension;	
	   
    	   
//	_settextwindow(5,5,21,36);    //largest is 1,1,25,40//this would be full screen
//   _setbkcolor (1);   //red=4 yellow=14 brightwhite=15 normal white =7    
  dede:  //use this place to come back to and start over this function within the function  
    _settextposition(25,1);
    _outtext("                                      ");
    _clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   
    _settextwindow(5,5,20,35);    //largest is 1,1,25,40//this would be full screen
    _setbkcolor (7);    
    _clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN    
    letter[0]= xyzletter;
    letter[1]='\0';  
    capital = _strupr(letter);
    _settextcolor(15);    
    _setbkcolor (1);
    _outtext("                               ");     
    _settextposition (1,8);  
    _outtext ("Encoder ");
    _outtext (capital); 
    _outtext("  Mode ");
    if (mode==1) _outtext("A");  
    if (mode==2) _outtext("B");
    if (mode==3) _outtext("C");
    if (mode==4) _outtext("D"); 
    _outtext("\n\n\n\n");
    _setbkcolor (7);
    _settextcolor(0);    //black
    _outtext("   Reset to Zero\n\n");
    _outtext("   Current Value\n\n");
    _outtext("   Count up/down");  
    _settextcolor(4);  //red     
    _settextposition (5,4);   
    _outtext("R");   
    _settextposition (7,12); 
    _outtext("V"); 
    _settextposition (9,4); 
    _outtext("C");      
    _settextposition (25,1); 
    _settextcolor(0);    //black
    _outtext("                          Esc"); 
    _settextcolor(14); //yellow 
    _settextposition (5,18); _outtext("             "); //reset spot
    _settextposition (7,18); _outtext("             "); //current value spot
    _settextposition (9,18); _outtext("             "); //+/-
    
    
    
    _settextposition (9,19); 
    if  (xyzletter=='x')
    	{if (coder1.cnt_updown==0) cout<<"-"<<flush; 
    	 if (coder1.cnt_updown==1) cout<<"+"<<flush;}   
    if  (xyzletter=='y') 
    	{if (coder2.cnt_updown==0) cout<<"-"<<flush; 
    	 if (coder2.cnt_updown==1) cout<<"+"<<flush;}  
    if  (xyzletter=='z')  
    	{if (coder3.cnt_updown==0) cout<<"-"<<flush; 
    	 if (coder3.cnt_updown==1) cout<<"+"<<flush;} 
    	
    
    if  (xyzletter=='x')
        {switch (mode)
        	{case (1): {sprintf(string, "%.6f",(double) (backlash_X.currently_displayed-mode1.xoffset));break;}
             case (2): {sprintf(string, "%.6f",(double) (backlash_X.currently_displayed-mode2.xoffset));break;}
             case (3): {sprintf(string, "%.6f",(double) (backlash_X.currently_displayed-mode3.xoffset));break;}
             case (4): {sprintf(string, "%.6f",(double) (backlash_X.currently_displayed-mode4.xoffset));break;}
            }             
        }
    if  (xyzletter=='y')
        {switch (mode)
        	{case (1): {sprintf(string, "%.6f",(double) (backlash_Y.currently_displayed-mode1.yoffset));break;}
             case (2): {sprintf(string, "%.6f",(double) (backlash_Y.currently_displayed-mode2.yoffset));break;}
             case (3): {sprintf(string, "%.6f",(double) (backlash_Y.currently_displayed-mode3.yoffset));break;}
             case (4): {sprintf(string, "%.6f",(double) (backlash_Y.currently_displayed-mode4.yoffset));break;}
            }             
        }    
    if  (xyzletter=='z')
        {switch (mode)
        	{case (1): {sprintf(string, "%.6f",(double) (backlash_Z.currently_displayed-mode1.zoffset));break;}
             case (2): {sprintf(string, "%.6f",(double) (backlash_Z.currently_displayed-mode2.zoffset));break;}
             case (3): {sprintf(string, "%.6f",(double) (backlash_Z.currently_displayed-mode3.zoffset));break;}
             case (4): {sprintf(string, "%.6f",(double) (backlash_Z.currently_displayed-mode4.zoffset));break;}
            }             
        }               
    _settextposition (7,19); 
    cout<<string<<flush;
       
    do {
    	cc=_getch();   
    	
    
    	
    	if (cc==118)//the v key    Enter current value
    		{
    		_settextposition(13,1); _outtext("                                ");
   		    longdouble=numbers_only (13, 22,"Enter Current Value ");   
   		    
   		    if (longdouble != -99999.0)
   		       {
    			
    			if (xyzletter=='x')
    			     {switch (mode) //see what mode we are in
        			   {case (1): { mode1.xoffset=encoder1-longdouble;break;}
             		 	case (2): { mode2.xoffset=encoder1-longdouble;break;}
            			case (3): { mode3.xoffset=encoder1-longdouble;break;}
            		 	case (4): { mode4.xoffset=encoder1-longdouble;break;}  
            		   }
    			    // zero_all_X_mode==1;
    			     }
    		    if (xyzletter=='y')
    			     {switch (mode) //see what mode we are in
        			   {case (1): { mode1.yoffset=encoder2-longdouble;break;}
             		 	case (2): { mode2.yoffset=encoder2-longdouble;break;}
            			case (3): { mode3.yoffset=encoder2-longdouble;break;}
            		 	case (4): { mode4.yoffset=encoder2-longdouble;break;}  
            		   } 
            		 //zero_all_Y_mode==1;  
    			     }	
    		   if (xyzletter=='z')
    			     {switch (mode) //see what mode we are in
        			   {case (1): { mode1.zoffset=encoder3-longdouble;break;}
             		 	case (2): { mode2.zoffset=encoder3-longdouble;break;}
            			case (3): { mode3.zoffset=encoder3-longdouble;break;}
            		 	case (4): { mode4.zoffset=encoder3-longdouble;break;}  
            		   }
    			     //zero_all_Z_mode==1;
    			     }   
    		      
    		      
    		   }
             goto dede;
            }
    	
    	
    	
    	if (cc==99)//the c key for count other way (direction) -+    
    	   {_settextposition (9,19);
    	     if (xyzletter=='x')   //the x key        
    		     {
    		      if (coder1.cnt_updown==0) {coder1.cnt_updown=1;cout<<"+"<<flush; }
    	          else {coder1.cnt_updown=0; cout<<"-"<<flush;}
    	          encoder1 = 0-encoder1;//subtract by 0 to swap the sign of the number -/+
    	          backlash_X.currently_displayed = 0-backlash_X.currently_displayed; 
    	          
    	          backlash_X.swapsign=1;  //this being on will be found in the backlash function and will change the sign of the numbers in there too
    	          if (coder1.PrevValue==0) coder1.PrevValue=3;
    	          if (coder1.PrevValue==1) coder1.PrevValue=2;
    	          if (coder1.PrevValue==2) coder1.PrevValue=1;
    	          if (coder1.PrevValue==3) coder1.PrevValue=0;
    	          if (coder1.PrevPrevValue==0) coder1.PrevPrevValue=3;
    	          if (coder1.PrevPrevValue==1) coder1.PrevPrevValue=2;
    	          if (coder1.PrevPrevValue==2) coder1.PrevPrevValue=1;
    	          if (coder1.PrevPrevValue==3) coder1.PrevPrevValue=0; 
    	          if (coder1.PrevPrevPrevValue==0) coder1.PrevPrevPrevValue=3;
    	          if (coder1.PrevPrevPrevValue==1) coder1.PrevPrevPrevValue=2;
    	          if (coder1.PrevPrevPrevValue==2) coder1.PrevPrevPrevValue=1;
    	          if (coder1.PrevPrevPrevValue==3) coder1.PrevPrevPrevValue=0; 
   	         	 
    		 	 if (backlash_X.tension==0)  
    		 		{backlash_X.tension=1; }
    	         else {backlash_X.tension=0;}   
    	         switch (mode)
        			{case (1): {mode1.xoffset=0-mode1.xoffset;break;}
             		 case (2): {mode2.xoffset=0-mode2.xoffset;break;}
            		 case (3): {mode3.xoffset=0-mode3.xoffset;break;}
            		 case (4): {mode4.xoffset=0-mode4.xoffset;break;}  
            		} 
    	         
    	         }
    	     if (xyzletter=='y')   //the y key
    		     {if (coder2.cnt_updown==0) {coder2.cnt_updown=1;cout<<"+"<<flush; }
    	          else {coder2.cnt_updown=0; cout<<"-"<<flush;}
    	          encoder2 = 0-encoder2;
    	          backlash_Y.currently_displayed = 0-backlash_Y.currently_displayed; 
    	         
    	          backlash_Y.swapsign=1;  //this being on will be found in the backlash function and will change the sign of the numbers in there too
    	          if (coder2.PrevValue==0) coder2.PrevValue=3;
    	          if (coder2.PrevValue==1) coder2.PrevValue=2;
    	          if (coder2.PrevValue==2) coder2.PrevValue=1;
    	          if (coder2.PrevValue==3) coder2.PrevValue=0;
    	          if (coder2.PrevPrevValue==0) coder2.PrevPrevValue=3;
    	          if (coder2.PrevPrevValue==1) coder2.PrevPrevValue=2;
    	          if (coder2.PrevPrevValue==2) coder2.PrevPrevValue=1;
    	          if (coder2.PrevPrevValue==3) coder2.PrevPrevValue=0; 
    	          if (coder2.PrevPrevPrevValue==0) coder2.PrevPrevPrevValue=3;
    	          if (coder2.PrevPrevPrevValue==1) coder2.PrevPrevPrevValue=2;
    	          if (coder2.PrevPrevPrevValue==2) coder2.PrevPrevPrevValue=1;
    	          if (coder2.PrevPrevPrevValue==3) coder2.PrevPrevPrevValue=0; 
   	         	 
    		 	 if (backlash_Y.tension==0)  
    		 		{backlash_Y.tension=1;}
    	         else {backlash_Y.tension=0; }  
    	         switch (mode)
        			{case (1): {mode1.yoffset=0-mode1.yoffset;break;}
             		 case (2): {mode2.yoffset=0-mode2.yoffset;break;}
            		 case (3): {mode3.yoffset=0-mode3.yoffset;break;}
            		 case (4): {mode4.yoffset=0-mode4.yoffset;break;}  
            		} 
    	         }     
    	     if (xyzletter=='z')   //the z key
    		     {if (coder3.cnt_updown==0) {coder3.cnt_updown=1;cout<<"+"<<flush; }
    	          else {coder3.cnt_updown=0; cout<<"-"<<flush;}
    	          encoder3 = 0-encoder3; 
    	          backlash_Z.currently_displayed = 0-backlash_Z.currently_displayed;
    	          backlash_Z.swapsign=1;  //this being on will be found in the backlash function and will change the sign of the numbers in there too
    	          if (coder3.PrevValue==0) coder3.PrevValue=3;
    	          if (coder3.PrevValue==1) coder3.PrevValue=2;
    	          if (coder3.PrevValue==2) coder3.PrevValue=1;
    	          if (coder3.PrevValue==3) coder3.PrevValue=0;
    	          if (coder3.PrevPrevValue==0) coder3.PrevPrevValue=3;
    	          if (coder3.PrevPrevValue==1) coder3.PrevPrevValue=2;
    	          if (coder3.PrevPrevValue==2) coder3.PrevPrevValue=1;
    	          if (coder3.PrevPrevValue==3) coder3.PrevPrevValue=0; 
    	          if (coder3.PrevPrevPrevValue==0) coder3.PrevPrevPrevValue=3;
    	          if (coder3.PrevPrevPrevValue==1) coder3.PrevPrevPrevValue=2;
    	          if (coder3.PrevPrevPrevValue==2) coder3.PrevPrevPrevValue=1;
    	          if (coder3.PrevPrevPrevValue==3) coder3.PrevPrevPrevValue=0; 
   	         	
    		 	 if (backlash_Z.tension==0)  
    		 		{backlash_Z.tension=1; }
    	         else {backlash_Z.tension=0; }  
    	         switch (mode)
        			{case (1): {mode1.zoffset=0-mode1.zoffset;break;}
             		 case (2): {mode2.zoffset=0-mode2.zoffset;break;}
            		 case (3): {mode3.zoffset=0-mode3.zoffset;break;}
            		 case (4): {mode4.zoffset=0-mode4.zoffset;break;}  
            		} 
    	         }    
    	   goto dede;
    	   }      
    	   if (cc==114)  //the r key  reset to zero
    		{
    		 coder1.changed==1;coder2.changed==1;coder3.changed==1;//tell it that the encoders have changed it will redraw the values again when it returns to the main loop.This was done cause after rezeroing it would not show zero when it returns
    		 if (a==0) a=1;  //the a just toggles back and forth
    		 else a=0;  
    		 _settextposition (7,18);_outtext("             "); //current value spot 
    		 //cout<<"                "<<flush;  
    		 _settextposition (7,18); 
    		 if (a==1) cout<<"0"<<flush;    
    		 if (xyzletter=='x')  
    		 	{if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		 		 switch (mode)
        			   {case (1): {save_offset=mode1.xoffset; mode1.xoffset=encoder1;break;}
             		 	case (2): {save_offset=mode2.xoffset; mode2.xoffset=encoder1;break;}
            			case (3): {save_offset=mode3.xoffset; mode3.xoffset=encoder1;break;}
            		 	case (4): {save_offset=mode4.xoffset; mode4.xoffset=encoder1;break;}  
            		   }
    		 		 if (encoder1>backlash_X.currently_displayed) dif=encoder1-backlash_X.currently_displayed;
    		 		 else dif=backlash_X.currently_displayed-encoder1;
    		 		 if (dif>(backlash_X.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_X.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_X.tension=1; }
    	         		 else {backlash_X.tension=0; }
    		 		    }
    		 	    switch (mode) //if in any other mode than master mode
        			   {case (1): { mode1.xoffset=encoder1;break;}
             		 	case (2): { mode2.xoffset=encoder1;break;}
            			case (3): { mode3.xoffset=encoder1;break;}
            		 	case (4): { mode4.xoffset=encoder1;break;}  
            		   }
    		 	    backlash_X.currently_displayed=encoder1;
    		 	    zero_all_X_mode=1;  //this turns on the zero stuff in the backlash function
    		 		coder1.possible_error_cnt=0; //set the error counter back to zero too
                    _settextposition (5,18);cout<<"Now Zero    "<<flush;
    		 		 
    		 	     }
    		 	 else {_settextposition (7,18);  cout<<encoder1-save_offset<<flush; _settextposition (5,18); cout<<"Zero Undone"<<flush;
    		 	 	   switch (mode)
        			   		{case (1): {mode1.xoffset=save_offset; break;}
             		 		 case (2): {mode2.xoffset=save_offset; break;}
            				 case (3): {mode3.xoffset=save_offset; break;}
            		 		 case (4): {mode4.xoffset=save_offset; break;}
            		  		} 
    		 	 	   zero_all_X=0;   //turn the zero stuff back off in the backlash function.  Guess we don't need it.
    		 	 	   backlash_X.tension=x_save_tension;  
    		 	 	   //coder1.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	  }
    		 	 } 
    		 	if (xyzletter=='y')  
    		 	{if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		 		 switch (mode)
        			   {case (1): {save_offset=mode1.yoffset; mode1.yoffset=encoder2;break;}
             		 	case (2): {save_offset=mode2.yoffset; mode2.yoffset=encoder2;break;}
            			case (3): {save_offset=mode3.yoffset; mode3.yoffset=encoder2;break;}
            		 	case (4): {save_offset=mode4.yoffset; mode4.yoffset=encoder2;break;}  
            		   }
    		
    		 		if (encoder2>backlash_Y.currently_displayed) dif=encoder2-backlash_Y.currently_displayed;
    		 		 else dif=backlash_Y.currently_displayed-encoder2;
    		 		 if (dif>(backlash_Y.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_Y.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Y.tension=1; }
    	         		 else {backlash_Y.tension=0; }
    		 		    }
    		 	    switch (mode) //if in any other mode than master mode
        			   {case (1): { mode1.yoffset=encoder2;break;}
             		 	case (2): { mode2.yoffset=encoder2;break;}
            			case (3): { mode3.yoffset=encoder2;break;}
            		 	case (4): { mode4.yoffset=encoder2;break;}  
            		   }
    		 	    backlash_Y.currently_displayed=encoder2;
    		 	    zero_all_Y_mode=1;  //this turns on the zero stuff in the backlash function
    		 		coder2.possible_error_cnt=0; //set the error counter back to zero too
                    _settextposition (5,18);cout<<"Now Zero    "<<flush;
    		 		 
    		 		 }
    		 	 else { _settextposition (7,18);  cout<<encoder2-save_offset<<flush; _settextposition (5,18); cout<<"Zero Undone"<<flush;
    		 	 	   switch (mode)
        			   		{case (1): {mode1.yoffset=save_offset; break;}
             		 		 case (2): {mode2.yoffset=save_offset; break;}
            				 case (3): {mode3.yoffset=save_offset; break;}
            		 		 case (4): {mode4.yoffset=save_offset; break;}
            		  		} 
    		 	 	   zero_all_Y=0;   //turn the zero stuff back off in the backlash function.  Guess we don't need it.
    		 	 	   backlash_Y.tension=y_save_tension;  
    		 	 	  // coder2.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	  }
    		 	 }  
    		 	 if (xyzletter=='z')  
    		 	{if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		 		 switch (mode)
        			   {case (1): {save_offset=mode1.zoffset; mode1.zoffset=encoder3;break;}
             		 	case (2): {save_offset=mode2.zoffset; mode2.zoffset=encoder3;break;}
            			case (3): {save_offset=mode3.zoffset; mode3.zoffset=encoder3;break;}
            		 	case (4): {save_offset=mode4.zoffset; mode4.zoffset=encoder3;break;}  
            		   }
    		 		 if (encoder3>backlash_Z.currently_displayed) dif=encoder3-backlash_Z.currently_displayed;
    		 		 else dif=backlash_Z.currently_displayed-encoder3;
    		 		 if (dif>(backlash_Z.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_Z.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Z.tension=1; }
    	         		 else {backlash_Z.tension=0; }
    		 		    }
    		 	    switch (mode) //if in any other mode than master mode
        			   {case (1): { mode1.zoffset=encoder3;break;}
             		 	case (2): { mode2.zoffset=encoder3;break;}
            			case (3): { mode3.zoffset=encoder3;break;}
            		 	case (4): { mode4.zoffset=encoder3;break;}  
            		   }
    		 	    backlash_Z.currently_displayed=encoder3;
    		 	    zero_all_Z_mode=1;  //this turns on the zero stuff in the backlash function
    		 		coder3.possible_error_cnt=0; //set the error counter back to zero too
                    _settextposition (5,18);cout<<"Now Zero    "<<flush;
    		 		 
    		 		 }
    		 	 else { _settextposition (7,18);  cout<<encoder3-save_offset<<flush; _settextposition (5,18); cout<<"Zero Undone"<<flush;
    		 	 	   switch (mode)
        			   		{case (1): {mode1.zoffset=save_offset; break;}
             		 		 case (2): {mode2.zoffset=save_offset; break;}
            				 case (3): {mode3.zoffset=save_offset; break;}
            		 		 case (4): {mode4.zoffset=save_offset; break;}
            		  		} 
    		 	 	   zero_all_Z=0;   //turn the zero stuff back off in the backlash function.  Guess we don't need it.
    		 	 	   backlash_Z.tension=z_save_tension;  
    		 	 	   //coder3.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	  }
    		 	 } 
    	
    	     }
    	
    	
       }while  (cc!=27);// only go back after the esc key	 
    
} 
 
void encoder_setup(char xyzletter)
{
    int cc, a=0,ccc;
	char letter[2];
	char string[25]; 
	long double longdouble, dif;  
	char *capital; 
	long double save_1, save_2, save_3; 
	int only_once=1;   
	int x_save_tension=backlash_X.tension;
	int y_save_tension=backlash_Y.tension;
	int z_save_tension=backlash_Z.tension;
    long int save_error;    
    int skip_getch=0;
    
REDRAWSETUP:
   
   
   letter[0]= xyzletter;
    letter[1]='\0';  
    capital = _strupr(letter);
    _setbkcolor (9);  //light blue
	_clearscreen(_GCLEARSCREEN);
	_setbkcolor (15); _settextposition(1,1);_outtext("                                        ");
	_settextposition (1,14);  
	_settextcolor(0);    //black
    _outtext (capital);
    _outtext(" Master Mode");   
   
    _setbkcolor (9);  //light blue    
    _settextposition (25,1);     
    _settextcolor(7);    //white
    cout<<"  F2 Conversion         Esc to return"<<flush;  

    _settextposition (4,1);
    cout<<"    Count up/down"<<"\n\n"
        <<"    Reset to Zero"<<"\n\n"
    	<<"    Current Value"<<"\n\n"
    	<<"    Unit to Count by"<<"\n\n"
    	<<"    Backlash on/off"<<"\n\n"
        <<"    Lash Distance"<<"\n\n"
        <<"    Lash Tension Dir."<<"\n\n"
        <<"    Possible Errors"<<"\n\n"
        <<"    Places Shown Right of Decimal"<<flush;
    _settextcolor(4);    //red    
    _settextposition (25,3);_outtext("F2");
    _settextposition (4,5);_outtext("C");
    _settextposition (6,5);_outtext("R"); 
    _settextposition (8,13);_outtext("V"); 
    _settextposition (10,5);_outtext("U");   
    _settextposition (12,5);_outtext("B");   
    _settextposition (14,5);_outtext("L");    
    _settextposition (16,10);_outtext("T");  
    _settextposition (18,5);_outtext("P");   
    _settextposition (20,12);_outtext("S");
   // _settextcolor(7);    //white
    _settextcolor(14);  //yello  
    _settextposition (4,24);_outtext("                 ");   //from now on this place will be yellow with couts.                           
    _settextposition (6,24);_outtext("                 ");   //from now on this place will be yellow with couts.   
    _settextposition (8,24);_outtext("                 ");   //from now on this place will be yellow with couts.  
    _settextposition (10,24);_outtext("                 ");   //from now on this place will be yellow with couts.        
    _settextposition (12,24);_outtext("                 ");   //from now on this place will be yellow with couts.  
    _settextposition (14,24);_outtext("                 ");   //from now on this place will be yellow with couts.   
    _settextposition (16,24);_outtext("                 ");   //from now on this place will be yellow with couts.
    _settextposition (18,24);_outtext("                 ");   //from now on this place will be yellow with couts.  
    _settextposition (20,35);_outtext("   ");   //from now on this place will be yellow with couts.
    _settextposition (22,1);_outtext("                                        ");   //from now on this place will be yellow with couts.  

    
    _settextposition (20,35);
    if  (xyzletter=='x') cout<<coder1.decimal_places_shown<<flush;//the x key   
    if  (xyzletter=='y') cout<<coder2.decimal_places_shown<<flush;//the y key   
    if  (xyzletter=='z') cout<<coder3.decimal_places_shown<<flush;//the z key  
    
    _settextposition (8,24);   
    if  (xyzletter=='x') sprintf(string, "%.6f",(double) backlash_X.currently_displayed); //the x key   
    if  (xyzletter=='y') sprintf(string, "%.6f",(double) backlash_Y.currently_displayed); //the y key   
    if  (xyzletter=='z') sprintf(string, "%.6f",(double) backlash_Z.currently_displayed); //the z key 
    cout<<string<<flush;    
    
 
    _settextposition (4,24);
    if  (xyzletter=='x')
    	{if (coder1.cnt_updown==0) cout<<"-"<<flush; 
    	if (coder1.cnt_updown==1) cout<<"+"<<flush;}   
    if  (xyzletter=='y') 
    	{if (coder2.cnt_updown==0) cout<<"-"<<flush; 
    	if (coder2.cnt_updown==1) cout<<"+"<<flush;}  
    if  (xyzletter=='z')  
    	{if (coder3.cnt_updown==0) cout<<"-"<<flush; 
    	if (coder3.cnt_updown==1) cout<<"+"<<flush;}  
       
    
    _settextposition (10,24); 
    if  (xyzletter=='x')
    	{sprintf(string, "%.14f",(double)x_unit);  //convert it to a string
		 cout<<string<<flush; }  
	if  (xyzletter=='y')
    	{sprintf(string, "%.14f",(double)y_unit);  //convert it to a string
		 cout<<string<<flush; } 
	if  (xyzletter=='z')
    	{sprintf(string, "%.14f",(double)z_unit);  //convert it to a string
		 cout<<string<<flush; }  	  	 
    
    _settextposition (12,24); 
    if  (xyzletter=='x') 
        {if (backlash_X.onoff==1) cout<<"ON "<<flush;
    	 else cout<<"OFF"<<flush;}
    if  (xyzletter=='y') 
    	{if (backlash_Y.onoff==1) cout<<"ON "<<flush;
    	 else cout<<"OFF"<<flush;}
    if  (xyzletter=='z') 
        {if (backlash_Z.onoff==1) cout<<"ON "<<flush;
    	 else cout<<"OFF"<<flush;}
 
    _settextposition (14,24);
    if  (xyzletter=='x')  cout<<backlash_X.dist<<flush; //the x key   
    if  (xyzletter=='y')  cout<<backlash_Y.dist<<flush; //the y key   
    if  (xyzletter=='z')  cout<<backlash_Z.dist<<flush; //the z key    
     
    _settextposition (16,24); 
    if  (xyzletter=='x') 
        {if (backlash_X.tension==1) cout<<"Right"<<flush;
    	 else cout<<"Left "<<flush;}
    if  (xyzletter=='y') 
    	{if (backlash_Y.tension==1) cout<<"Right"<<flush;
    	 else cout<<"Left "<<flush;}
    if  (xyzletter=='z') 
        {if (backlash_Z.tension==1) cout<<"Right"<<flush;
    	 else cout<<"Left "<<flush;}  
    	 
    _settextposition (18,24);	     
    if  (xyzletter=='x') 
        {if (coder1.possible_error==1) cout<<"Displayed    "<<flush;
    	 else cout<<"Not Displayed"<<flush;}
    if  (xyzletter=='y') 
    	{if (coder2.possible_error==1) cout<<"Displayed    "<<flush;
    	 else cout<<"Not Displayed"<<flush;}
    if  (xyzletter=='z') 
        {if (coder3.possible_error==1) cout<<"Displayed    "<<flush;
    	 else cout<<"Not Displayed"<<flush;} 	 
    
  
   do {
    	cc=_getch(); 
    	//cout<<cc<<flush;
    
       
    
    	if (cc==4)  //"ctr d" key combination //this is a backdoor to turn on couts of varables to the screen  
    	   {if (mywatch==0) 
    	   		{mywatch=1; }
    	   		           
    	    else mywatch=0;
    	   } 
    	if (cc==115)  //s key //places shown right of decimal      
    	   {_settextposition (20,35);
    	   	if (xyzletter=='x')   //the x key  
    	   		{switch (coder1.decimal_places_shown)
    	   			{case (0): coder1.decimal_places_shown=1; cout<<coder1.decimal_places_shown<<flush; break; 
    	   			 case (1): coder1.decimal_places_shown=2; cout<<coder1.decimal_places_shown<<flush; break; 
    	   			 case (2): coder1.decimal_places_shown=3; cout<<coder1.decimal_places_shown<<flush; break; 
    	   			 case (3): coder1.decimal_places_shown=4; cout<<coder1.decimal_places_shown<<flush; break; 
    	   			 case (4): coder1.decimal_places_shown=5; cout<<coder1.decimal_places_shown<<flush; break;
    	   			 case (5): coder1.decimal_places_shown=6; cout<<coder1.decimal_places_shown<<flush; break;  
    	   			 case (6): coder1.decimal_places_shown=0; cout<<coder1.decimal_places_shown<<flush; break;   
    	   			}
    	   		} 	  
    	   	if (xyzletter=='y')   //the y key  
    	   		{switch (coder2.decimal_places_shown)
    	   			{case (0): coder2.decimal_places_shown=1; cout<<coder2.decimal_places_shown<<flush; break; 
    	   			 case (1): coder2.decimal_places_shown=2; cout<<coder2.decimal_places_shown<<flush; break; 
    	   			 case (2): coder2.decimal_places_shown=3; cout<<coder2.decimal_places_shown<<flush; break; 
    	   			 case (3): coder2.decimal_places_shown=4; cout<<coder2.decimal_places_shown<<flush; break; 
    	   			 case (4): coder2.decimal_places_shown=5; cout<<coder2.decimal_places_shown<<flush; break;
    	   			 case (5): coder2.decimal_places_shown=6; cout<<coder2.decimal_places_shown<<flush; break; 
    	   			 case (6): coder2.decimal_places_shown=0; cout<<coder2.decimal_places_shown<<flush; break;    
    	   			}
    	   		} 	     
    	   	if (xyzletter=='z')   //the z key  
    	   		{switch (coder3.decimal_places_shown)
    	   			{case (0): coder3.decimal_places_shown=1; cout<<coder3.decimal_places_shown<<flush; break; 
    	   			 case (1): coder3.decimal_places_shown=2; cout<<coder3.decimal_places_shown<<flush; break; 
    	   			 case (2): coder3.decimal_places_shown=3; cout<<coder3.decimal_places_shown<<flush; break; 
    	   			 case (3): coder3.decimal_places_shown=4; cout<<coder3.decimal_places_shown<<flush; break; 
    	   			 case (4): coder3.decimal_places_shown=5; cout<<coder3.decimal_places_shown<<flush; break;
    	   			 case (5): coder3.decimal_places_shown=6; cout<<coder3.decimal_places_shown<<flush; break; 
    	   			 case (6): coder3.decimal_places_shown=0; cout<<coder3.decimal_places_shown<<flush; break; 
    	   			    
    	   			}
    	   		} 	  
    	   }    
    	if (cc==60)//the F2 key Conversion   mm / inch   
    	    {
    	     //_settextrows(10);
    	     _settextwindow(5,10,20,30);    //largest is 1,1,25,40//this would be full screen
    	     _setbkcolor (8);    
    	     _clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   
    	     _settextwindow(5,10,19,29);    //largest is 1,1,25,40//this would be full screen
    	     _setbkcolor (7);    
    	     _clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN 
    	     _settextcolor(15);    
    	     //cout<<"hello"<<flush;
    	     //_outtext("F2f");
    	     //_settextposition (1,1);   
    	     _setbkcolor (6);
    	     _outtext("                    ");     
    	     _settextposition (1,1); 
    	     _outtext("  Conversion setup\n\n\n\n");  
    	     _setbkcolor (7);
    	     _settextcolor(0);    //black
             _outtext("   Conversion\n\n");
             _outtext("   inch TO mm\n\n");
             _outtext("   mm TO inch");  
             _settextcolor(4);  //red     
             _settextposition (5,4);   
             _outtext("C");   
             _settextposition (7,4); 
             _outtext("i"); 
             _settextposition (9,4); 
             _outtext("m");      
             _settextposition (25,1); 
             _settextcolor(0);    //black
             _outtext("                Esc"); 
             //_settextcolor(4);  //red  
             _settextcolor(14); //yellow
             goto skiper;
 
           do {
           	 
           	   if(skip_getch==1) {skip_getch=0; goto jump;}
           	   ccc=_getch();
  jump: 	
               
               
               if (ccc==99)//the c key conversion      //0 is off 1 is (inch to mm) and 2 is (mm to inch) 
               	   {if  (xyzletter=='x')  
              			{if (coder1.conversion==0) coder1.conversion=1;
               			else coder1.conversion=0;}                	
               	   	if  (xyzletter=='y')  
              			{if (coder2.conversion==0) coder2.conversion=1;
               			else coder2.conversion=0;}    	 
                    if  (xyzletter=='z')  
              			{if (coder3.conversion==0) coder3.conversion=1;
               			else coder3.conversion=0;}    
                   }
               
                 if (ccc==105)//the i key inch to metric  
               		{
               		 if  (xyzletter=='x')  
               		 	{if(coder1.conversion==0)
               		 		{_settextposition (11,4);
               		 		 _outtext("Turn Conversion\n      on first.");
               		 		 ccc=_getch(); skip_getch=1;
               		 		 _settextposition (11,4);
               		 		 _outtext("               \n               ");
               		 		 }
               		     if(coder1.conversion==2)coder1.conversion=1; 	
               		     if(coder1.conversion==1){ }//do nothing it is already set 	  
               		    }	
               		 if  (xyzletter=='y')  
               		 	{if(coder2.conversion==0)
               		 		{_settextposition (11,4);
               		 		 _outtext("Turn Conversion\n      on first.");
               		 		 ccc=_getch(); skip_getch=1;
               		 		 _settextposition (11,4);
               		 		 _outtext("               \n               ");
               		 		 }
               		     if(coder2.conversion==2)coder2.conversion=1; 	
               		     if(coder2.conversion==1){ }//do nothing it is already set 	  
               		    }	
               		 if  (xyzletter=='z')  
               		 	{if(coder3.conversion==0)
               		 		{_settextposition (11,4);
               		 		 _outtext("Turn Conversion\n      on first.");
               		 		 ccc=_getch(); skip_getch=1;
               		 		 _settextposition (11,4);
               		 		 _outtext("               \n               ");
               		 		 }
               		     if(coder3.conversion==2)coder3.conversion=1; 	
               		     if(coder3.conversion==1){ }//do nothing it is already set 	  
               		    }	   
               		}
               
                 if (ccc==109)//the m key mm to inch   
               		{
               		 if  (xyzletter=='x')  
               		 	{if(coder1.conversion==0)
               		 		{_settextposition (11,4);
               		 		 _outtext("Turn Conversion\n      on first.");
               		 		 ccc=_getch(); skip_getch=1;
               		 		 _settextposition (11,4);
               		 		 _outtext("               \n               ");
               		 		 }
               		     if(coder1.conversion==2){ }//do nothing it is already set 
              		     if(coder1.conversion==1)coder1.conversion=2; 	
               		    } 
               		 if  (xyzletter=='y')  
               		 	{if(coder2.conversion==0)
               		 		{_settextposition (11,4);
               		 		 _outtext("Turn Conversion\n      on first.");
               		 		 ccc=_getch(); skip_getch=1;
               		 		 _settextposition (11,4);
               		 		 _outtext("               \n               ");
               		 		 }
               		     if(coder2.conversion==2){ }//do nothing it is already set 
              		     if(coder2.conversion==1)coder2.conversion=2; 	
               		    } 
               		 if  (xyzletter=='z')  
               		 	{if(coder3.conversion==0)
               		 		{_settextposition (11,4);
               		 		 _outtext("Turn Conversion\n      on first.");
               		 		 ccc=_getch(); skip_getch=1;
               		 		 _settextposition (11,4);
               		 		 _outtext("               \n               ");
               		 		 }
               		     if(coder3.conversion==2){ }//do nothing it is already set 
              		     if(coder3.conversion==1)coder3.conversion=2; 	
               		    }       
               		}  
skiper:           
              _settextposition (5,16);      
              if(xyzletter=='x')
	            {if(coder1.conversion==0)
            		{cout<<"Off"<<flush;
            		 _settextposition (7,16);cout<<"Off"<<flush;
            		 _settextposition (9,16);cout<<"Off"<<flush;
            		} 
             	if(coder1.conversion==1)
            		{cout<<"On "<<flush;
            		 _settextposition (7,16);cout<<"On "<<flush;
            		 _settextposition (9,16);cout<<"Off"<<flush;
            		} 
           	  	if(coder1.conversion==2) 
            		{cout<<"On "<<flush;
            		_settextposition (7,16);cout<<"Off"<<flush;
            		 _settextposition (9,16);cout<<"On "<<flush;
                    }  
               }      
             if(xyzletter=='y')
	            {if(coder2.conversion==0)
            		{cout<<"Off"<<flush;
            		 _settextposition (7,16);cout<<"Off"<<flush;
            		 _settextposition (9,16);cout<<"Off"<<flush;
            		} 
             	if(coder2.conversion==1)
            		{cout<<"On "<<flush;
            		 _settextposition (7,16);cout<<"On "<<flush;
            		 _settextposition (9,16);cout<<"Off"<<flush;
            		} 
           	  	if(coder2.conversion==2) 
            		{cout<<"On "<<flush;
            		_settextposition (7,16);cout<<"Off"<<flush;
            		 _settextposition (9,16);cout<<"On "<<flush;
                    }  
               }      
             if(xyzletter=='z')
	            {if(coder3.conversion==0)
            		{cout<<"Off"<<flush;
            		 _settextposition (7,16);cout<<"Off"<<flush;
            		 _settextposition (9,16);cout<<"Off"<<flush;
            		} 
             	if(coder3.conversion==1)
            		{cout<<"On "<<flush;
            		 _settextposition (7,16);cout<<"On "<<flush;
            		 _settextposition (9,16);cout<<"Off"<<flush;
            		} 
           	  	if(coder3.conversion==2) 
            		{cout<<"On "<<flush;
            		_settextposition (7,16);cout<<"Off"<<flush;
            		 _settextposition (9,16);cout<<"On "<<flush;
                    }  
               }
    	   }while  (ccc!=27);// only go back after the esc key   
    	     _settextwindow(1,1,25,40); 
    	     _settextcolor(7);    //white 
    	     ccc=0;
    	     goto REDRAWSETUP; 
    	   //cout<<"hello"<<flush;    	   
    	   } 
    	   
    	if (cc==99)//the c key for count other way (direction) -+    
    	   { _settextposition (4,24);
    	     if (xyzletter=='x')   //the x key        
    		     {
    		      if (coder1.cnt_updown==0) {coder1.cnt_updown=1;cout<<"+"<<flush; }
    	          else {coder1.cnt_updown=0; cout<<"-"<<flush;}
    	          encoder1 = 0-encoder1;//subtract by 0 to swap the sign of the number -/+
    	          backlash_X.currently_displayed = 0-backlash_X.currently_displayed;
    	          backlash_X.swapsign=1;  //this being on will be found in the backlash function and will change the sign of the numbers in there too
    	          if (coder1.PrevValue==0) coder1.PrevValue=3;
    	          if (coder1.PrevValue==1) coder1.PrevValue=2;
    	          if (coder1.PrevValue==2) coder1.PrevValue=1;
    	          if (coder1.PrevValue==3) coder1.PrevValue=0;
    	          if (coder1.PrevPrevValue==0) coder1.PrevPrevValue=3;
    	          if (coder1.PrevPrevValue==1) coder1.PrevPrevValue=2;
    	          if (coder1.PrevPrevValue==2) coder1.PrevPrevValue=1;
    	          if (coder1.PrevPrevValue==3) coder1.PrevPrevValue=0; 
    	          if (coder1.PrevPrevPrevValue==0) coder1.PrevPrevPrevValue=3;
    	          if (coder1.PrevPrevPrevValue==1) coder1.PrevPrevPrevValue=2;
    	          if (coder1.PrevPrevPrevValue==2) coder1.PrevPrevPrevValue=1;
    	          if (coder1.PrevPrevPrevValue==3) coder1.PrevPrevPrevValue=0; 
   	         	 _settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 	 if (backlash_X.tension==0)  
    		 		{backlash_X.tension=1;cout<<"Right"<<flush; }
    	         else {backlash_X.tension=0; cout<<"Left "<<flush;}  
                 
            
                 _settextposition (8,24); 
                 sprintf(string, "%.6f",(double) backlash_X.currently_displayed);
                 cout<<string<<flush;
 // 			switch (mode)
 //       			{case (1): {mode1.xoffset=0-mode1.xoffset;break;}
 //            		 case (2): {mode2.xoffset;break;}
//           		 case (3): {mode3.xoffset;break;}
 //           		 case (4): {mode4.xoffset;break;}  
 //           		} 
    	         }
    	     if (xyzletter=='y')   //the y key
    		     {if (coder2.cnt_updown==0) {coder2.cnt_updown=1;cout<<"+"<<flush; }
    	          else {coder2.cnt_updown=0; cout<<"-"<<flush;}
    	          encoder2 = 0-encoder2;
    	          backlash_Y.currently_displayed = 0-backlash_Y.currently_displayed; 
    	          backlash_Y.swapsign=1;  //this being on will be found in the backlash function and will change the sign of the numbers in there too
    	          if (coder2.PrevValue==0) coder2.PrevValue=3;
    	          if (coder2.PrevValue==1) coder2.PrevValue=2;
    	          if (coder2.PrevValue==2) coder2.PrevValue=1;
    	          if (coder2.PrevValue==3) coder2.PrevValue=0;
    	          if (coder2.PrevPrevValue==0) coder2.PrevPrevValue=3;
    	          if (coder2.PrevPrevValue==1) coder2.PrevPrevValue=2;
    	          if (coder2.PrevPrevValue==2) coder2.PrevPrevValue=1;
    	          if (coder2.PrevPrevValue==3) coder2.PrevPrevValue=0; 
    	          if (coder2.PrevPrevPrevValue==0) coder2.PrevPrevPrevValue=3;
    	          if (coder2.PrevPrevPrevValue==1) coder2.PrevPrevPrevValue=2;
    	          if (coder2.PrevPrevPrevValue==2) coder2.PrevPrevPrevValue=1;
    	          if (coder2.PrevPrevPrevValue==3) coder2.PrevPrevPrevValue=0; 
   	         	 _settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 	 if (backlash_Y.tension==0)  
    		 		{backlash_Y.tension=1;cout<<"Right"<<flush; }
    	         else {backlash_Y.tension=0; cout<<"Left "<<flush;}    
    	         
                 _settextposition (8,24); 
                 sprintf(string, "%.6f",(double) backlash_Y.currently_displayed);
                 cout<<string<<flush;
    //	         switch (mode)
    //    			{case (1): {mode1.yoffset;break;}
    //         		 case (2): {mode2.yoffset;break;}
    //        		 case (3): {mode3.yoffset;break;}
    //        		 case (4): {mode4.yoffset;break;}  
    //        		} 
    	         }     
    	     if (xyzletter=='z')   //the z key
    		     {if (coder3.cnt_updown==0) {coder3.cnt_updown=1;cout<<"+"<<flush; }
    	          else {coder3.cnt_updown=0; cout<<"-"<<flush;}
    	          encoder3 = 0-encoder3; 
    	          backlash_Z.currently_displayed = 0-backlash_Z.currently_displayed;
    	          backlash_Z.swapsign=1;  //this being on will be found in the backlash function and will change the sign of the numbers in there too
    	          if (coder3.PrevValue==0) coder3.PrevValue=3;
    	          if (coder3.PrevValue==1) coder3.PrevValue=2;
    	          if (coder3.PrevValue==2) coder3.PrevValue=1;
    	          if (coder3.PrevValue==3) coder3.PrevValue=0;
    	          if (coder3.PrevPrevValue==0) coder3.PrevPrevValue=3;
    	          if (coder3.PrevPrevValue==1) coder3.PrevPrevValue=2;
    	          if (coder3.PrevPrevValue==2) coder3.PrevPrevValue=1;
    	          if (coder3.PrevPrevValue==3) coder3.PrevPrevValue=0; 
    	          if (coder3.PrevPrevPrevValue==0) coder3.PrevPrevPrevValue=3;
    	          if (coder3.PrevPrevPrevValue==1) coder3.PrevPrevPrevValue=2;
    	          if (coder3.PrevPrevPrevValue==2) coder3.PrevPrevPrevValue=1;
    	          if (coder3.PrevPrevPrevValue==3) coder3.PrevPrevPrevValue=0; 
   	         	 _settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 	 if (backlash_Z.tension==0)  
    		 		{backlash_Z.tension=1;cout<<"Right"<<flush; }
    	         else {backlash_Z.tension=0; cout<<"Left "<<flush;} 
                 
                 _settextposition (8,24); 
                 sprintf(string, "%.6f",(double) backlash_Z.currently_displayed);
                 cout<<string<<flush;
    //	         switch (mode)
    //    			{case (1): {mode1.zoffset;break;}
    //         		 case (2): {mode2.zoffset;break;}
    //        		 case (3): {mode3.zoffset;break;}
    //        		 case (4): {mode4.zoffset;break;}  
    //        		} 
    	         }    
    	   //goto REDRAWSETUP;
    	   }      
        if (cc==112) // the p key    show or not show possible error
           { _settextposition (18,24);
             if (xyzletter=='x')   //the x key        
    		     {
    		      if (coder1.possible_error==0) {coder1.possible_error=1;cout<<"Displayed    "<<flush; }
    	          else {coder1.possible_error=0; cout<<"Not Displayed"<<flush;}
    	         }
    	     if (xyzletter=='y')   //the y key
    		     {if (coder2.possible_error==0) {coder2.possible_error=1;cout<<"Displayed    "<<flush; }
    	          else {coder2.possible_error=0; cout<<"Not Displayed"<<flush;}
    	         }     
    	     if (xyzletter=='z')   //the z key
    		     {if (coder3.possible_error==0) {coder3.possible_error=1;cout<<"Displayed    "<<flush; }
    	          else {coder3.possible_error=0; cout<<"Not Displayed"<<flush;}
    	         }
           } 
            
        if (cc==117)//the u key    unit to count by
    		{
   		    longdouble=numbers_only (22, 25,"Enter counting unit "); 
   		    if (longdouble != -99999.0)
   		    	{sprintf(string, "%.14f",(double) longdouble); //convert printfloat into a string
   				_settextposition (10,24);
    	    	cout<<string<<flush;
    			if  (xyzletter=='x') x_unit=longdouble;
    	    	if  (xyzletter=='y') y_unit=longdouble;    			  
		    	if  (xyzletter=='z') z_unit=longdouble;
    			_settextposition (20,1); 
    			}
          }
    	if (cc==98)   //the b key      backlash on/off
    		{_settextposition (12,24);
    		 if (xyzletter=='x')   //the x key        
    		     {
    		      if (backlash_X.onoff==0) {backlash_X.onoff=1;cout<<"On "<<flush; }
    	          else {backlash_X.onoff=0; cout<<"Off"<<flush;}
    	         }
    	     if (xyzletter=='y')   //the y key
    		     {if (backlash_Y.onoff==0) {backlash_Y.onoff=1;cout<<"On "<<flush; }
    	          else {backlash_Y.onoff=0; cout<<"Off"<<flush;}
    	         }     
    	     if (xyzletter=='z')   //the z key
    		     {if (backlash_Z.onoff==0) {backlash_Z.onoff=1;cout<<"On "<<flush; }
    	          else {backlash_Z.onoff=0; cout<<"Off"<<flush;}
    	         }
    		}  
    	if (cc==116)   //the l key      lash tension dir.
    		{_settextposition (16,24);
    		 if (xyzletter=='x')   //the x key        
    		     {
    		      if (backlash_X.tension==0) {backlash_X.tension=1;cout<<"Right"<<flush; }
    	          else {backlash_X.tension=0; cout<<"Left "<<flush;}
    	         }
    	     if (xyzletter=='y')   //the y key
    		     {if (backlash_Y.tension==0) {backlash_Y.tension=1;cout<<"Right"<<flush; }
    	          else {backlash_Y.tension=0; cout<<"Left "<<flush;}
    	         }     
    	     if (xyzletter=='z')   //the z key
    		     {if (backlash_Z.tension==0) {backlash_Z.tension=1;cout<<"Right"<<flush; }
    	          else {backlash_Z.tension=0; cout<<"Left "<<flush;}
    	         }
    		}
    	if (cc==114)  //the r key  reset to zero
    		{
    		 if (a==0) a=1;  //the a just toggles back and forth
    		 else a=0;  
    		 _settextposition (8,24); 
    		 cout<<"                "<<flush;  
    		 _settextposition (8,24); 
    		 if (a==1) cout<<0.0000<<flush;    
    		 
    		 if (xyzletter=='x')  
    		 	{if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		 		 if (encoder1>backlash_X.currently_displayed) dif=encoder1-backlash_X.currently_displayed;
    		 		 else dif=backlash_X.currently_displayed-encoder1;
    		 		 
    		 		 if (dif>(backlash_X.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                     //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{_settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 		 	 if (backlash_X.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_X.tension=1;cout<<"Right"<<flush; }
    	         		 else {backlash_X.tension=0; cout<<"Left "<<flush;}
    		 		    }
    		 		 zero_all_X=1;  //this turns on the zero stuff in the backlash function
    		 		 save_1=encoder1;
    		 		 backlash_X.currently_displayed=0;
    		 		 encoder1=0; _settextposition (6,24); cout<<"Now Zero    "<<flush;
    		 		 save_error=coder1.possible_error_cnt;//save the error count
    		 		 coder1.possible_error_cnt=0;} //set the error counter back to zero too
    		 	 else {encoder1=save_1; _settextposition (8,24);  cout<<encoder1<<flush; _settextposition (6,24); cout<<"Zero Undone"<<flush;
    		 	 	zero_all_X=0;   //turn the zero stuff back off in the backlash function.  Guess we don't need it.
    		 	 	backlash_X.tension=x_save_tension;  
    		 	 	_settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 	 	if (backlash_X.tension==1)cout<<"Right"<<flush; 
    	            else cout<<"Left "<<flush;
    		 	 	coder1.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	}
    		 	 }
      	     if (xyzletter=='y')
      	        {if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		 		 if (encoder2>backlash_Y.currently_displayed) dif=encoder2-backlash_Y.currently_displayed;
    		 		 else dif=backlash_Y.currently_displayed-encoder2;
    		 		 
    		 		 if (dif>(backlash_Y.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                     //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{_settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 		 	 if (backlash_Y.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Y.tension=1;cout<<"Right"<<flush; }
    	         		 else {backlash_Y.tension=0; cout<<"Left "<<flush;}
    		 		    }
    		 		 zero_all_Y=1;  //this turns on the zero stuff in the backlash function
    		 		 save_2=encoder2;
    		 		 backlash_Y.currently_displayed=0;
    		 		 encoder2=0; _settextposition (6,24); cout<<"Now Zero    "<<flush;
    		 		 save_error=coder2.possible_error_cnt;//save the error count
    		 		 coder2.possible_error_cnt=0;} //set the error counter back to zero too
    		 	 else {encoder2=save_2; _settextposition (8,24);  cout<<encoder2<<flush; _settextposition (6,24); cout<<"Zero Undone"<<flush;
    		 	 	zero_all_Y=0;   //turn the zero stuff back off in the backlash function.  Guess we don't need it.
    		 	 	backlash_Y.tension=y_save_tension;  
    		 	 	_settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 	 	if (backlash_Y.tension==1)cout<<"Right"<<flush; 
    	            else cout<<"Left "<<flush;
    		 	 	coder2.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	}
    		 	 }
    	     if (xyzletter=='z') 
    	        {if (a==1)  //0 is undo and 1 is rezero
    		 		{
    		 		 if (encoder3>backlash_Z.currently_displayed) dif=encoder3-backlash_Z.currently_displayed;
    		 		 else dif=backlash_Z.currently_displayed-encoder3;
    		 		 
    		 		 if (dif>(backlash_Z.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                     //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{_settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 		 	 if (backlash_Z.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Z.tension=1;cout<<"Right"<<flush; }
    	         		 else {backlash_Z.tension=0; cout<<"Left "<<flush;}
    		 		    }
    		 		 zero_all_Z=1;  //this turns on the zero stuff in the backlash function
    		 		 save_3=encoder3;
    		 		 backlash_Z.currently_displayed=0;
    		 		 encoder3=0; _settextposition (6,24); cout<<"Now Zero    "<<flush;
    		 		 save_error=coder3.possible_error_cnt;//save the error count
    		 		 coder3.possible_error_cnt=0;} //set the error counter back to zero too
    		 	 else {encoder3=save_3; _settextposition (8,24);  cout<<encoder3<<flush; _settextposition (6,24); cout<<"Zero Undone"<<flush;
    		 	 	zero_all_Z=0;   //turn the zero stuff back off in the backlash function.  Guess we don't need it.
    		 	 	backlash_Z.tension=z_save_tension;  
    		 	 	_settextposition (16,24);//set the position to where the lash tension direction is displayed
    		 	 	if (backlash_Z.tension==1)cout<<"Right"<<flush; 
    	            else cout<<"Left "<<flush;
    		 	 	coder3.possible_error_cnt=save_error;    //set this back to what it was
    		 	 	}
    		 	 }		
    		 }
    	if (cc==118)  //the v key   current value
    		{  
    		 longdouble=numbers_only (22, 25,"Change current value to "); 
    		 if (longdouble != -99999.0)
   		    	{sprintf(string, "%.6f",(double) longdouble); //convert printfloat into a string      
   		    	_settextposition (6,24); cout<<"               "<<flush;  //coverup the zero info text above 
   				_settextposition (8,24); cout<<"               "<<flush;  
   				_settextposition (8,24);
    	    	cout<<string<<flush;
    			if (xyzletter=='x')  {encoder1 =longdouble; zero_all_X_mode=1; backlash_X.currently_displayed=encoder1;} //the x key
    	    	if (xyzletter=='y')  {encoder2 =longdouble; zero_all_Y_mode=1; backlash_Y.currently_displayed=encoder2;}//the y key
    	    	if (xyzletter=='z')  {encoder3 =longdouble; zero_all_Z_mode=1; backlash_Z.currently_displayed=encoder3;}//the z key 
    	    	
    	    	}
    	   }	
    	if (cc==108) 	//the l key   lash distance   
    	   { 
    	    longdouble=numbers_only (22, 25,"Amount of backlash ");
   		    if (longdouble != -99999.0)
   		    	{sprintf(string, "%.6f",(double) longdouble); //convert printfloat into a string
   				_settextposition (14,24); cout<<"               "<<flush;  
   				_settextposition (14,24);
    	    	cout<<string<<flush;
    	    	if (xyzletter=='x')  backlash_X.dist =longdouble;  //the x key
    	    	if (xyzletter=='y')  backlash_Y.dist =longdouble;  //the y key
    	    	if (xyzletter=='z')  backlash_Z.dist =longdouble; //the z key 
    	    	}
      	   }		                                                                         
    	}while  (cc!=27);// only go back after the esc key
} 	
	    
	    
	    

 
long double numbers_only(int row, int colum, char message[])         //function to input just a few numbers
{
	long double longdouble;
	char buffer[21];
	int i=-1, c, len; 
	_settextcursor (0x0707);  //turn blinking cursor on 
	len=strlen(message);                  //see how many charchacters make up the string
	_settextposition (row, (colum-len));  
	cout<<message<<flush;

  	while (i < 15) 			     
	   {_settextposition (row, colum); 
		c=getch();  
		if (((c>=48)&&(c<=57))||(c==46)||(c==45)||(c==27)||(c==13)||(c==8))     //if the input keys are any of the number keys (or a .) find out what they are and add the to the string.
	    	{
	    	 i++;
	    	 colum++;    
	    	 if (c==0x8) //backspace
	   			{i--;
	   			colum--;
	   			colum--;   
	   		 	_settextposition(row, colum);   
	   		 	cout<<" "<<flush;
	   		 	buffer[i]=' ';
	   		 	i--;}
	    	if (c==48)
	   			{buffer[i] = '0';
	   			//_outtext("0");}                     //the i= are for displaying the input with the cout and the bottom of this loop.
	  		     cout<<"0"<<flush;}
	  		if (c==49)
	   			{buffer[i] = '1';
	  			 //_outtext("1");} 
	  			 cout<<"1"<<flush;}
	  		if (c==50)
	   			{buffer[i] = '2';
	   			//_outtext("2");}
	   			cout<<"2"<<flush;}
	  		if (c==51)
	   			{buffer[i] = '3';
	   			//_outtext("3");}
	   			cout<<"3"<<flush;}
	  		if (c==52)
	   			{buffer[i] = '4';
	   			//_outtext("4");}
	   			cout<<"4"<<flush;}
	  		if (c==53)
	   			{buffer[i] = '5';
	   			//_outtext("5");}
	   			cout<<"5"<<flush;}
	  		if (c==54)
	   			{buffer[i] = '6';
	   			//_outtext("6");}
	   			cout<<"6"<<flush;}
	  		if (c==55)
	   			{buffer[i] = '7';
	   			//_outtext("7");}
	   			cout<<"7"<<flush;}                                      
	  		if (c==56)
	   			{buffer[i] = '8';
	   	   		//_outtext("8");}
	   	   		cout<<"8"<<flush;}
	  		if (c==57)
	   			{buffer[i] = '9';
	   			//_outtext("9");}
	   			cout<<"9"<<flush;}
	  		if (c==46)
	   			{buffer[i] = '.';
	   			//_outtext(".");}
	   			cout<<"."<<flush;}  
	   		if (c==45) 
	   			{buffer[i] = '-';
	   			//_outtext("-");} 
	   			cout<<"-"<<flush;}
	   		if (c==27)     //ESC key
	   			{_settextposition (row, (((colum-i)-len)-1)); 
	   			 cout<<"                                           "<<flush;    //cover up any message that was displayed
	   			 _settextcursor (0x2000);  //cursor off
	   			 return -99999;}  //esc key	   return a strange number and check for it in the calling function so you know if the esc was hit         
	        if (c==13)  //enter
	        	{ break; } 	//will exit the loop and do the rest of the function before returning   
	    	} 
		}//end of for loop	
	_settextposition (row, (((colum-i)-len)-1)); 		    	
	cout<<"                                           "<<flush;    //cover up any message that was displayed
  	buffer[i] = '\0';   //terminate string with null character:				   
  	longdouble=_atold(buffer);   
  	_settextcursor (0x2000);  //cursor off
  	return longdouble;				   
}
 
int intnumbers_only(int row, int colum, char message[])         //function to input just a few numbers
{
	int intnumber;
	char buffer[21];
	int i=-1, c, len; 
	_settextcursor (0x0707);  //turn blinking cursor on 
	len=strlen(message);                  //see how many charchacters make up the string
	_settextposition (row, (colum-len));  
	cout<<message<<flush;

  	while (i < 15) 			     
	   {_settextposition (row, colum); 
		c=getch();  
		if (((c>=48)&&(c<=57))||(c==46)||(c==45)||(c==27)||(c==13)||(c==8))     //if the input keys are any of the number keys (or a .) find out what they are and add the to the string.
	    	{
	    	 i++;
	    	 colum++;    
	    	 if (c==0x8) //backspace
	   			{i--;
	   			colum--;
	   			colum--;   
	   		 	_settextposition(row, colum);   
	   		 	cout<<" "<<flush;
	   		 	buffer[i]=' ';
	   		 	i--;}
	    	if (c==48)
	   			{buffer[i] = '0';
	   			//_outtext("0");}                     //the i= are for displaying the input with the cout and the bottom of this loop.
	  		     cout<<"0"<<flush;}
	  		if (c==49)
	   			{buffer[i] = '1';
	  			 //_outtext("1");} 
	  			 cout<<"1"<<flush;}
	  		if (c==50)
	   			{buffer[i] = '2';
	   			//_outtext("2");}
	   			cout<<"2"<<flush;}
	  		if (c==51)
	   			{buffer[i] = '3';
	   			//_outtext("3");}
	   			cout<<"3"<<flush;}
	  		if (c==52)
	   			{buffer[i] = '4';
	   			//_outtext("4");}
	   			cout<<"4"<<flush;}
	  		if (c==53)
	   			{buffer[i] = '5';
	   			//_outtext("5");}
	   			cout<<"5"<<flush;}
	  		if (c==54)
	   			{buffer[i] = '6';
	   			//_outtext("6");}
	   			cout<<"6"<<flush;}
	  		if (c==55)
	   			{buffer[i] = '7';
	   			//_outtext("7");}
	   			cout<<"7"<<flush;}                                      
	  		if (c==56)
	   			{buffer[i] = '8';
	   	   		//_outtext("8");}
	   	   		cout<<"8"<<flush;}
	  		if (c==57)
	   			{buffer[i] = '9';
	   			//_outtext("9");}
	   			cout<<"9"<<flush;}
	  		if (c==46)
	   			{buffer[i] = '.';
	   			//_outtext(".");}
	   			cout<<"."<<flush;}  
	   		if (c==45) 
	   			{buffer[i] = '-';
	   			//_outtext("-");} 
	   			cout<<"-"<<flush;}
	   		if (c==27)     //ESC key
	   			{_settextposition (row, (((colum-i)-len)-1)); 
	   			 cout<<"                                           "<<flush;    //cover up any message that was displayed
	   			 _settextcursor (0x2000);  //cursor off
	   			 return 9999;}  //esc key	   return a strange number and check for it in the calling function so you know if the esc was hit         
	        if (c==13)  //enter
	        	{ break; } 	//will exit the loop and do the rest of the function before returning   
	    	} 
		}//end of for loop	
	_settextposition (row, (((colum-i)-len)-1)); 		    	
	cout<<"                                           "<<flush;    //cover up any message that was displayed
  	buffer[i] = '\0';   //terminate string with null character:				   
  	intnumber=atoi(buffer);   
  	_settextcursor (0x2000);  //cursor off
  	return intnumber;				   
}
  
 
 
 //--------------------------------------------------------------------------------------------------------------

 /*
int check_key()
	{	
	int c2;
	
	c2=_getch();  
    if (c2==0) c2 = _getch();  //this if is because when the function keys are pressed they will return 0 and if read again return the decimal code for that key.  (they need to be read twice)
    if (c2==27){ return 1;}    //the Esc key
    return 0;
    }
 */      
  
 //-------------------------------------------------------------------------------------------------------------- 
  



   
   
void draw_encode_screen ()   
{ 
 _setvideomode(_TEXTC40);
 _settextposition (2,14); 
   
_settextcolor(14); //yellow
 //_settextcolor(7);    //white
 if (mode==0) _outtext("Master Mode");  
 if (mode==1) _outtext("  Mode A   ");
 if (mode==2) _outtext("  Mode B   ");
 if (mode==3) _outtext("  Mode C   ");
 if (mode==4) _outtext("  Mode D   ");  
 
  /* AGE1  Added this to mode display*/
    _settextposition (1,16);    
 	_settextcolor(14);    //yellow
	_outtext( machine );             

/* end of added this to mode display */
 
 //if (rotary.x_show_revolutions==1)  
 
 _settextposition (10,10);
 _outtext("X");
 _settextposition (12,10);   
 _outtext("Y");
 _settextposition (14,10); 	 _outtext("Z");
 _settextposition (25,1);  	 cout<<" X  Y  Z      Mode      Rotary"<<flush;   
 _settextcolor(4);    //red
 _settextposition (25,1);	 _outtext (" X  Y  Z      M"); 
 _settextposition (25,25);    _outtext ("R");   
 _settextposition (25,37);    _outtext ("Esc");
// _settextposition (25,14);	 _outtext ("C");
 _settextcolor(7);     //white  
 
                 //if in rotary mode turn various things off as they are for linar.  //set the unit to count by to 1.  As this is what the rotary uses from the encoders counting by whole numbers 1,2,3...
 if (rotary.x_onoff==1) {x_unit=1;backlash_X.onoff==0; coder1.conversion=0; coder1.possible_error=1; backlash_X.onoff=0;}
 if (rotary.y_onoff==1) {y_unit=1;backlash_Y.onoff==0; coder2.conversion=0; coder2.possible_error=1; backlash_Y.onoff=0;} 
 if (rotary.z_onoff==1) {z_unit=1;backlash_Z.onoff==0; coder3.conversion=0; coder3.possible_error=1; backlash_Z.onoff=0;}
/* 
 switch (xyzletter)      //if in rotary mode turn various things off as they are for linar.  //set the unit to count by to 1.  As this is what the rotary uses from the encoders counting by whole numbers 1,2,3...
    	{case ('x'): {x_unit=1;backlash_X.onoff==0; coder1.conversion=0; coder1.possible_error=1; backlash_X.onoff=0; break;}
    	 case ('y'): {y_unit=1;backlash_Y.onoff==0; coder2.conversion=0; coder2.possible_error=1; backlash_Y.onoff=0; break;}
         case ('z'): {z_unit=1;backlash_Z.onoff==0; coder3.conversion=0; coder3.possible_error=1; backlash_Z.onoff=0; break;}
        } 
*/ 
 
 
 if (coder1.possible_error==1)  //if user has the error display thingy on display the number of errors
 	{_settextposition (10,35);
	 cout<<"E"<<coder1.possible_error_cnt<<flush;}     
 if (coder2.possible_error==1)
 	{_settextposition (12,35);
	 cout<<"E"<<coder2.possible_error_cnt<<flush;}  
 if (coder3.possible_error==1)
 	{_settextposition (14,35);
	 cout<<"E"<<coder3.possible_error_cnt<<flush;}  	 	 
	 
   	_settextcolor(15);//bright white  
    _settextposition (10,16); _outtext("            ");   //from now on any couts to this area will be bright white
    _settextposition (12,16); _outtext("            ");   
 	_settextposition (14,16); _outtext("            ");
 	_settextcolor(7);//white	  
 if (coder1.conversion!=0)
     {_settextposition (10,31);cout<<"C"<<flush;} 
 if (coder2.conversion!=0)
     {_settextposition (12,31);cout<<"C"<<flush;}
 if (coder3.conversion!=0)
     {_settextposition (14,31);cout<<"C"<<flush;} 
      
 if (rotary.x_onoff!=0)
     {_settextposition (10,33);cout<<"R"<<flush;} 
 if (rotary.y_onoff!=0)
     {_settextposition (12,33);cout<<"R"<<flush;}
 if (rotary.z_onoff!=0)
     {_settextposition (14,33);cout<<"R"<<flush;}  
 
 if (backlash_X.onoff==1)        
     {_settextposition (10,33);cout<<"B"<<flush;}  
 if (backlash_Y.onoff==1)        
     {_settextposition (12,33);cout<<"B"<<flush;}        
 if (backlash_Z.onoff==1)        
     {_settextposition (14,33);cout<<"B"<<flush;}       
          
	
coder1.changed=1;coder2.changed=1;coder3.changed=1;  //have to set these on so when it returns it will also display a value for the encoders.  Else they will not show until the encoders are moved  
}  
 
 
 



long double checkbacklash_X (long double backlash, long double encoder)
{
 	static long double lasttime, lastlasttime, lastlastlasttime, changed_direction_point; //limit;
 	long double dif;
 	int far_enough=0;
 
    if (backlash_X.swapsign==1)    //sutracting by zero with swap the sign of the number +/-
    	{backlash_X.swapsign=0; 
    	 if (backlash_X.rightleft==0)backlash_X.rightleft=1;
    	 else backlash_X.rightleft=0; 
         lasttime=0-lasttime;
    	 lastlasttime=0-lastlasttime;   
    	 lastlastlasttime=0-lastlastlasttime;
    	 changed_direction_point=0-changed_direction_point;   
    	 if (encoder>lastlastlasttime)
    	 	{lasttime=encoder-x_unit;
    	     lastlasttime=lasttime-x_unit;
    	     lastlastlasttime=lastlasttime-x_unit;
    	     }
    	 if (encoder<lastlastlasttime)
    	  	{lasttime=encoder+x_unit;
    	     lastlasttime=lasttime+x_unit;
    	     lastlastlasttime=lastlasttime+x_unit;
	   	     }  
    	}
 
    if (zero_all_X==1)  //this if in incase the user rezeros  from the master mode 0
    
    	    	{changed_direction_point=0;encoder=0;zero_all_X=0;
    			if (backlash_X.tension==0) {lasttime=x_unit; lastlasttime=(x_unit*2); backlash_X.rightleft=1;}//fldk(1 and 2) along with swaping the 1 and 2 around in InPort where it figures what value to use from the binary[8]
    	 		if (backlash_X.tension==1) {lasttime=(0-x_unit); lastlasttime=(lasttime-x_unit);backlash_X.rightleft=0;}  
    	    	}

   if (zero_all_X_mode==1)  //this if in incase the user rezeros while in mode 1,2,3 or 4 (but not master mode 0)
    	{coder1.changed=1;  zero_all_X_mode=0;   // Without it it will sometime show zero or the backlash distance, or nothing.  With it it will redraw with the current changes this if has done.
    	 changed_direction_point=encoder1;   
    	 
    	 if (mode==0) //user this for modesetup when the user enters a new value (used to reset to whatever they enter)
    	      {if (backlash_X.tension==0) {lasttime=encoder1+x_unit; lastlasttime=lasttime+x_unit; backlash_X.rightleft=1;}
    	 	   if (backlash_X.tension==1) {lasttime=encoder1-x_unit; lastlasttime=lasttime-x_unit; backlash_X.rightleft=0;}
    		  }     
    	 else    //else for all the other modes (1,2,3,4) do this for rezeroing
    	      {if (backlash_X.tension==0)   
    	    	 {lasttime=changed_direction_point+x_unit; lastlasttime=lasttime+x_unit; backlash_X.rightleft=1;}//swap backlash_X.rightleft around (1 and 2) a
    	 	   if (backlash_X.tension==1) 
    	 		 {lasttime=changed_direction_point-x_unit; lastlasttime=lasttime-x_unit; backlash_X.rightleft=0;}
    	      }
    	 return encoder1;
    	}			   				  	  			   			   

//   if (zero_all_X_mode==1)  //this if in incase the user rezeros from the encoder setup
//    	{changed_direction_point=mode1.xoffset;  zero_all_X_mode=0;
//    	 if (backlash_X.tension==0) {lasttime=mode1.xoffset+x_unit; lastlasttime=mode1.xoffset+(x_unit*2); backlash_X.rightleft=1;}//swap backlash_X.rightleft around (1 and 2) a
//    	 if (backlash_X.tension==1) {lasttime=mode1.xoffset-(0-x_unit); lastlasttime=mode1.xoffset-(lasttime-x_unit);backlash_X.rightleft=0;}  
//    	} 	
    
    if ((encoder>changed_direction_point)&&(backlash_X.rightleft==0)) far_enough=1;          //backlash_direction would be set to 0
    if ((encoder<changed_direction_point)&&(backlash_X.rightleft==1)) far_enough=1;          //backlash_direction would be set to 1
	
	if (changed_direction_point>encoder)      //gone down since the last change point
 		{dif=changed_direction_point-encoder;
 		if (dif>=backlash) far_enough=1;  
 		} 
 	if (changed_direction_point<encoder)      //gone up since the last change point
 		{dif=encoder-changed_direction_point;
 		if (dif>=backlash) far_enough=1; 
 		}	
		
 //these couts are for testing  
   if (mywatch==1) 
       {_settextposition (14,1); 
    	cout<<"\n"<<"dif "<<dif<<"\n"<<"far_enough "<<far_enough<<"\n"<<"backlash_X.rightleft "<<backlash_X.rightleft<<flush;
 		cout<<"\n"<<"encoder "<<encoder<<"\n"<<"lasttime "<<lasttime<<"\n"<<"lastlasttime "<<lastlasttime<<flush;
 		cout<<"\n"<<"changed_direction_point "<<changed_direction_point<<"\n"<<"backlash_X.tension "<<backlash_X.tension
 			<<"\n"<<"lastlastlasttime"<<lastlastlasttime<<flush;
 	   }
 	//if ((lastlastlasttime<lastlasttime)&&(lastlasttime<lasttime)) //was going up     
 	if (lastlasttime<lasttime)//was going up
 	   {if (lasttime>encoder)   //now going down   
 	   		{if (far_enough==1) 
 	    		{changed_direction_point=encoder; //set the change point here    
 	    		 lastlastlasttime=lastlasttime;
 	    		 lastlasttime=lasttime; 
                 lasttime=encoder; 
                 backlash_X.rightleft=0;
                 return 999;}  //in the call funciton this will not print anything
 	    	 if (far_enough==0)
 	    	 	{
                 //cout<<"dog"<<flush;  
                 lastlastlasttime=lastlasttime;
                 lastlasttime=lasttime; 
                 lasttime=encoder; 	
                 return 999;}  //in the call funciton this will not print anything	
 	        }    
 	   if (lasttime<encoder) // still going up     //means compensate if backlash_direction is 1
 	       {if (far_enough==1)//&&(backlash_direction==1)) 
 	        	{lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                //_settextposition (14,30); cout<<8<<flush; 
                if (backlash_X.tension==1) return encoder;
 	        	return encoder-backlash;}
	        if ((backlash_X.rightleft==1)&&(far_enough==0))  
 	        	{//_settextposition (14,30); cout<<1<<flush;
 	        	lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_X.tension==1)
                	{ return changed_direction_point+backlash;}
 	        	return changed_direction_point; }
 	        if ((backlash_X.rightleft==0)&&(far_enough==0))
 	            {//_settextposition (14,30); cout<<2<<flush;
 	            lastlastlasttime=lastlasttime;
 	            lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_X.tension==1)
                	{return changed_direction_point;}
                return changed_direction_point-backlash;   
 	        	}
 	       }
 	   }             
 //if ((lastlastlasttime>lastlasttime)&&(lastlasttime>lasttime)) //was going down 
 	if (lastlastlasttime>lastlasttime) //was going down
 	   {if (lasttime<encoder)   //now going up   
 	    	{if (far_enough==1) 
 	    		{changed_direction_point=encoder; //set the change point here     
 	    		 lastlastlasttime=lastlasttime;
 	    		 lastlasttime=lasttime; 
                 lasttime=encoder;  
                 backlash_X.rightleft=1;    
                 return 999; } //in the call funciton this will not print anything
 	    	 if (far_enough==0)
 	    	 	{
                // cout<<"cat"<<flush;
                 lastlastlasttime=lastlasttime;
                 lastlasttime=lasttime; 
                 lasttime=encoder; 	
                 return 999;}  //in the call funciton this will not print anything                 
 	        }    
 	    if (lasttime>encoder) // still going down     //means compensate if backlash_direction is 0
 	        {if (far_enough==1)  //&&(backlash_direction==1)) 
 	        	{lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                //_settextposition (14,30); cout<<9<<flush;
                if (backlash_X.tension==1) {return encoder+backlash;}
 	        	return encoder;}
 	        if ((backlash_X.rightleft==1)&&(far_enough==0))  
 	        	{//_settextposition (14,30); cout<<3<<flush;
 	        	lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder;  
                if (backlash_X.tension==1)
                	{return changed_direction_point+backlash;}
 	       		return changed_direction_point; }
 	        if ((backlash_X.rightleft==0)&&(far_enough==0))
 	            {//_settextposition (14,30); cout<<4<<flush;
 	            lastlastlasttime=lastlasttime;
 	            lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_X.tension==1)
                	{return changed_direction_point;}					
                return changed_direction_point-backlash;    
 	        	}
 	       }
 	   }        
  lastlastlasttime=lastlasttime;
  lastlasttime=lasttime; 
  lasttime=encoder; 
  return backlash_X.currently_displayed;   //should not get to this point.  If it does 777 should show up on the screen   //will sometimes happen if there are errors
}  
 
  
long double checkbacklash_Y (long double backlash, long double encoder)
{
 	static long double lasttime, lastlasttime, lastlastlasttime, changed_direction_point; //limit;
 	long double dif;
 	int far_enough=0;
 
    if (backlash_Y.swapsign==1)    //sutracting by zero with swap the sign of the number +/-
    	{backlash_Y.swapsign=0; 
    	 if (backlash_Y.rightleft==0)backlash_Y.rightleft=1;
    	 else backlash_Y.rightleft=0; 
         lasttime=0-lasttime;
    	 lastlasttime=0-lastlasttime;   
    	 lastlastlasttime=0-lastlastlasttime;
    	 changed_direction_point=0-changed_direction_point;   
    	 if (encoder>lastlastlasttime)
    	 	{lasttime=encoder-y_unit;
    	     lastlasttime=lasttime-y_unit;
    	     lastlastlasttime=lastlasttime-y_unit;
    	     }
    	 if (encoder<lastlastlasttime)
    	  	{lasttime=encoder+y_unit;
    	     lastlasttime=lasttime+y_unit;
    	     lastlastlasttime=lastlasttime+y_unit;
	   	     }  
    	}
    
    if (zero_all_Y==1)  //this if in incase the user rezeros from the encoder setup
    	{changed_direction_point=0;encoder=0;zero_all_Y=0;
    	 if (backlash_Y.tension==0) {lasttime=y_unit; lastlasttime=(y_unit*2); backlash_Y.rightleft=1;}//swap backlash_Y.rightleft around (1 and 2) along with swaping the 1 and 2 around in InPort where it figures what value to use from the binary[8]
    	 if (backlash_Y.tension==1) {lasttime=(0-y_unit); lastlasttime=(lasttime-y_unit);backlash_Y.rightleft=0;}  
    	} 
    if (zero_all_Y_mode==1)  //this if in incase the user rezeros while in mode 1,2,3 or 4 (but not master mode 0)
    	{coder2.changed=1;  zero_all_Y_mode=0;   // Without it it will sometime show zero or the backlash distance, or nothing.  With it it will redraw with the current changes this if has done.
    	 changed_direction_point=encoder2;   
    	 
    	 if (mode==0) //user this for modesetup when the user enters a new value (used to reset to whatever they enter)
    	      {if (backlash_Y.tension==0) {lasttime=encoder2+y_unit; lastlasttime=lasttime+y_unit; backlash_Y.rightleft=1;}
    	 	   if (backlash_Y.tension==1) {lasttime=encoder2-y_unit; lastlasttime=lasttime-y_unit; backlash_Y.rightleft=0;}
    		  }     
    	 else    //else for all the other modes (1,2,3,4) do this for rezeroing
    	      {if (backlash_Y.tension==0)   
    	    	 {lasttime=changed_direction_point+y_unit; lastlasttime=lasttime+y_unit; backlash_Y.rightleft=1;}//swap backlash_Y.rightleft around (1 and 2) a
    	 	   if (backlash_Y.tension==1) 
    	 		 {lasttime=changed_direction_point-y_unit; lastlasttime=lasttime-y_unit; backlash_Y.rightleft=0;}
    	      }
    	 return encoder2;
    	}			   				  	   
    

    if ((encoder>changed_direction_point)&&(backlash_Y.rightleft==0)) far_enough=1;          //backlash_direction would be set to 0
    if ((encoder<changed_direction_point)&&(backlash_Y.rightleft==1)) far_enough=1;          //backlash_direction would be set to 1
	
	if (changed_direction_point>encoder)      //gone down since the last change point
 		{dif=changed_direction_point-encoder;
 		if (dif>=backlash) far_enough=1;  
 		} 
 	if (changed_direction_point<encoder)      //gone up since the last change point
 		{dif=encoder-changed_direction_point;
 		if (dif>=backlash) far_enough=1; 
 		}	
/*		
 //these couts are for testing  
   if (mywatch==1) 
       {_settextposition (14,1); 
    	cout<<"\n"<<"dif "<<dif<<"\n"<<"far_enough "<<far_enough<<"\n"<<"backlash_Y.rightleft "<<backlash_Y.rightleft<<flush;
 		cout<<"\n"<<"encoder "<<encoder<<"\n"<<"lasttime "<<lasttime<<"\n"<<"lastlasttime "<<lastlasttime<<flush;
 		cout<<"\n"<<"changed_direction_point "<<changed_direction_point<<"\n"<<"backlash_Y.tension "<<backlash_Y.tension
 			<<"\n"<<"lastlastlasttime"<<lastlastlasttime<<flush;
 	   }
*/ 	//if ((lastlastlasttime<lastlasttime)&&(lastlasttime<lasttime)) //was going up     
 	if (lastlasttime<lasttime)//was going up
 	   {if (lasttime>encoder)   //now going down   
 	   		{if (far_enough==1) 
 	    		{changed_direction_point=encoder; //set the change point here    
 	    		 lastlastlasttime=lastlasttime;
 	    		 lastlasttime=lasttime; 
                 lasttime=encoder; 
                 backlash_Y.rightleft=0;
                 return 999;}  //in the call funciton this will not print anything
 	    	 if (far_enough==0)
 	    	 	{
                 //cout<<"dog"<<flush;  
                 lastlastlasttime=lastlasttime;
                 lastlasttime=lasttime; 
                 lasttime=encoder; 	
                 return 999;}  //in the call funciton this will not print anything	
 	        }    
 	   if (lasttime<encoder) // still going up     //means compensate if backlash_direction is 1
 	       {if (far_enough==1)//&&(backlash_direction==1)) 
 	        	{lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                //_settextposition (14,30); cout<<8<<flush; 
                if (backlash_Y.tension==1) return encoder;
 	        	return encoder-backlash;}
	        if ((backlash_Y.rightleft==1)&&(far_enough==0))  
 	        	{//_settextposition (14,30); cout<<1<<flush;
 	        	lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_Y.tension==1)
                	{ return changed_direction_point+backlash;}
 	        	return changed_direction_point; }
 	        if ((backlash_Y.rightleft==0)&&(far_enough==0))
 	            {//_settextposition (14,30); cout<<2<<flush;
 	            lastlastlasttime=lastlasttime;
 	            lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_Y.tension==1)
                	{return changed_direction_point;}
                return changed_direction_point-backlash;   
 	        	}
 	       }
 	   }             
 //if ((lastlastlasttime>lastlasttime)&&(lastlasttime>lasttime)) //was going down 
 	if (lastlastlasttime>lastlasttime) //was going down
 	   {if (lasttime<encoder)   //now going up   
 	    	{if (far_enough==1) 
 	    		{changed_direction_point=encoder; //set the change point here     
 	    		 lastlastlasttime=lastlasttime;
 	    		 lastlasttime=lasttime; 
                 lasttime=encoder;  
                 backlash_Y.rightleft=1;    
                 return 999; } //in the call funciton this will not print anything
 	    	 if (far_enough==0)
 	    	 	{
                // cout<<"cat"<<flush;
                 lastlastlasttime=lastlasttime;
                 lastlasttime=lasttime; 
                 lasttime=encoder; 	
                 return 999;}  //in the call funciton this will not print anything                 
 	        }    
 	    if (lasttime>encoder) // still going down     //means compensate if backlash_direction is 0
 	        {if (far_enough==1)  //&&(backlash_direction==1)) 
 	        	{lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                //_settextposition (14,30); cout<<9<<flush;
                if (backlash_Y.tension==1) {return encoder+backlash;}
 	        	return encoder;}
 	        if ((backlash_Y.rightleft==1)&&(far_enough==0))  
 	        	{//_settextposition (14,30); cout<<3<<flush;
 	        	lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder;  
                if (backlash_Y.tension==1)
                	{return changed_direction_point+backlash;}
 	       		return changed_direction_point; }
 	        if ((backlash_Y.rightleft==0)&&(far_enough==0))
 	            {//_settextposition (14,30); cout<<4<<flush;
 	            lastlastlasttime=lastlasttime;
 	            lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_Y.tension==1)
                	{return changed_direction_point;}					
                return changed_direction_point-backlash;    
 	        	}
 	       }
 	   }        
  lastlastlasttime=lastlasttime;
  lastlasttime=lasttime; 
  lasttime=encoder; 
  return backlash_Y.currently_displayed;   //should not get to this point.  If it does 777 should show up on the screen   //will sometimes happen if there are errors
}
long double checkbacklash_Z (long double backlash, long double encoder)
{
 	static long double lasttime, lastlasttime, lastlastlasttime, changed_direction_point; //limit;
 	long double dif;
 	int far_enough=0;
 
    if (backlash_Z.swapsign==1)    //sutracting by zero with swap the sign of the number +/-
    	{backlash_Z.swapsign=0; 
    	 if (backlash_Z.rightleft==0)backlash_Z.rightleft=1;
    	 else backlash_Z.rightleft=0; 
         lasttime=0-lasttime;
    	 lastlasttime=0-lastlasttime;   
    	 lastlastlasttime=0-lastlastlasttime;
    	 changed_direction_point=0-changed_direction_point;   
    	 if (encoder>lastlastlasttime)
    	 	{lasttime=encoder-z_unit;
    	     lastlasttime=lasttime-z_unit;
    	     lastlastlasttime=lastlasttime-z_unit;
    	     }
    	 if (encoder<lastlastlasttime)
    	  	{lasttime=encoder+z_unit;
    	     lastlasttime=lasttime+z_unit;
    	     lastlastlasttime=lastlasttime+z_unit;
	   	     }  
    	}
    
    if (zero_all_Z==1)  //this if in incase the user rezeros from the encoder setup
    	{changed_direction_point=0;encoder=0;zero_all_Z=0;
    	 if (backlash_Z.tension==0) {lasttime=z_unit; lastlasttime=(z_unit*2); backlash_Z.rightleft=1;}//swap backlash_Z.rightleft around (1 and 2) along with swaping the 1 and 2 around in InPort where it figures what value to use from the binary[8]
    	 if (backlash_Z.tension==1) {lasttime=(0-z_unit); lastlasttime=(lasttime-z_unit);backlash_Z.rightleft=0;}  
    	} 
    if (zero_all_Z_mode==1)  //this if in incase the user rezeros while in mode 1,2,3 or 4 (but not master mode 0)
    	{coder3.changed=1;  zero_all_Z_mode=0;   // Without it it will sometime show zero or the backlash distance, or nothing.  With it it will redraw with the current changes this if has done.
    	 changed_direction_point=encoder3;   
    	 
    	 if (mode==0) //user this for modesetup when the user enters a new value (used to reset to whatever they enter)
    	      {if (backlash_Z.tension==0) {lasttime=encoder3+z_unit; lastlasttime=lasttime+z_unit; backlash_Z.rightleft=1;}
    	 	   if (backlash_Z.tension==1) {lasttime=encoder3-z_unit; lastlasttime=lasttime-z_unit; backlash_Z.rightleft=0;}
    		  }      
    	 else    //else for all the other modes (1,2,3,4) do this for rezeroing
    	      {if (backlash_Z.tension==0)   
    	    	 {lasttime=changed_direction_point+z_unit; lastlasttime=lasttime+z_unit; backlash_Z.rightleft=1;}//swap backlash_Z.rightleft around (1 and 2) a
    	 	   if (backlash_Z.tension==1) 
    	 		 {lasttime=changed_direction_point-z_unit; lastlasttime=lasttime-z_unit; backlash_Z.rightleft=0;}
    	      }
    	 return encoder3;
    	}			   				  	   
   
   
    if ((encoder>changed_direction_point)&&(backlash_Z.rightleft==0)) far_enough=1;          //backlash_direction would be set to 0
    if ((encoder<changed_direction_point)&&(backlash_Z.rightleft==1)) far_enough=1;          //backlash_direction would be set to 1
	
	if (changed_direction_point>encoder)      //gone down since the last change point
 		{dif=changed_direction_point-encoder;
 		if (dif>=backlash) far_enough=1;  
 		} 
 	if (changed_direction_point<encoder)      //gone up since the last change point
 		{dif=encoder-changed_direction_point;
 		if (dif>=backlash) far_enough=1; 
 		}	
/*		
 //these couts are for testing  
   if (mywatch==1) 
       {_settextposition (14,1); 
    	cout<<"\n"<<"dif "<<dif<<"\n"<<"far_enough "<<far_enough<<"\n"<<"backlash_Z.rightleft "<<backlash_Z.rightleft<<flush;
 		cout<<"\n"<<"encoder "<<encoder<<"\n"<<"lasttime "<<lasttime<<"\n"<<"lastlasttime "<<lastlasttime<<flush;
 		cout<<"\n"<<"changed_direction_point "<<changed_direction_point<<"\n"<<"backlash_Z.tension "<<backlash_Z.tension
 			<<"\n"<<"lastlastlasttime"<<lastlastlasttime<<flush;
 	   }
*/ 	//if ((lastlastlasttime<lastlasttime)&&(lastlasttime<lasttime)) //was going up     
 	if (lastlasttime<lasttime)//was going up
 	   {if (lasttime>encoder)   //now going down   
 	   		{if (far_enough==1) 
 	    		{changed_direction_point=encoder; //set the change point here    
 	    		 lastlastlasttime=lastlasttime;
 	    		 lastlasttime=lasttime; 
                 lasttime=encoder; 
                 backlash_Z.rightleft=0;
                 return 999;}  //in the call funciton this will not print anything
 	    	 if (far_enough==0)
 	    	 	{
                 //cout<<"dog"<<flush;  
                 lastlastlasttime=lastlasttime;
                 lastlasttime=lasttime; 
                 lasttime=encoder; 	
                 return 999;}  //in the call funciton this will not print anything	
 	        }    
 	   if (lasttime<encoder) // still going up     //means compensate if backlash_direction is 1
 	       {if (far_enough==1)//&&(backlash_direction==1)) 
 	        	{lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                //_settextposition (14,30); cout<<8<<flush; 
                if (backlash_Z.tension==1) return encoder;
 	        	return encoder-backlash;}
	        if ((backlash_Z.rightleft==1)&&(far_enough==0))  
 	        	{//_settextposition (14,30); cout<<1<<flush;
 	        	lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_Z.tension==1)
                	{ return changed_direction_point+backlash;}
 	        	return changed_direction_point; }
 	        if ((backlash_Z.rightleft==0)&&(far_enough==0))
 	            {//_settextposition (14,30); cout<<2<<flush;
 	            lastlastlasttime=lastlasttime;
 	            lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_Z.tension==1)
                	{return changed_direction_point;}
                return changed_direction_point-backlash;   
 	        	}
 	       }
 	   }             
 //if ((lastlastlasttime>lastlasttime)&&(lastlasttime>lasttime)) //was going down 
 	if (lastlastlasttime>lastlasttime) //was going down
 	   {if (lasttime<encoder)   //now going up   
 	    	{if (far_enough==1) 
 	    		{changed_direction_point=encoder; //set the change point here     
 	    		 lastlastlasttime=lastlasttime;
 	    		 lastlasttime=lasttime; 
                 lasttime=encoder;  
                 backlash_Z.rightleft=1;    
                 return 999; } //in the call funciton this will not print anything
 	    	 if (far_enough==0)
 	    	 	{
                // cout<<"cat"<<flush;
                 lastlastlasttime=lastlasttime;
                 lastlasttime=lasttime; 
                 lasttime=encoder; 	
                 return 999;}  //in the call funciton this will not print anything                 
 	        }    
 	    if (lasttime>encoder) // still going down     //means compensate if backlash_direction is 0
 	        {if (far_enough==1)  //&&(backlash_direction==1)) 
 	        	{lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder; 
                //_settextposition (14,30); cout<<9<<flush;
                if (backlash_Z.tension==1) {return encoder+backlash;}
 	        	return encoder;}
 	        if ((backlash_Z.rightleft==1)&&(far_enough==0))  
 	        	{//_settextposition (14,30); cout<<3<<flush;
 	        	lastlastlasttime=lastlasttime;
 	        	lastlasttime=lasttime; 
                lasttime=encoder;  
                if (backlash_Z.tension==1)
                	{return changed_direction_point+backlash;}
 	       		return changed_direction_point; }
 	        if ((backlash_Z.rightleft==0)&&(far_enough==0))
 	            {//_settextposition (14,30); cout<<4<<flush;
 	            lastlastlasttime=lastlasttime;
 	            lastlasttime=lasttime; 
                lasttime=encoder; 
                if (backlash_Z.tension==1)
                	{return changed_direction_point;}					
                return changed_direction_point-backlash;    
 	        	}
 	       }
 	   }        
  lastlastlasttime=lastlasttime;
  lastlasttime=lasttime; 
  lasttime=encoder; 
  return backlash_Z.currently_displayed;   //should not get to this point.  If it does 777 should show up on the screen   //will sometimes happen if there are errors

}
 
char *decimal_to_degrees(long double decimal)   

{
    char s_degrees [6], s_minutes [5], s_seconds [5];  
    long double fraction;
    int degrees, minutes, seconds;
    char answer [20];  
    //char *p_answer;
    char char_degree[3];
    char char_minutes[]="m ";
    char char_seconds[]="s ";   
    char_degree[0]=0xf8;  //the little circle that stands for degrees
    char_degree[1]=' ';
    char_degree[2]='\0';
    
    degrees=(int)decimal;                       //final degrees  
    fraction=decimal-(long double)degrees; //just the fraction of decimal  
    decimal=fraction*60;  
	minutes=(int)decimal;  
	fraction=decimal-(long double)minutes;
    decimal=fraction*60;  
    seconds=(int)decimal;  
    
    degrees= abs (degrees); //if they are negative this will make them positive
    minutes= abs (minutes);
    seconds= abs (seconds);
    
    _itoa(degrees, s_degrees, 10); 
    _itoa(minutes, s_minutes, 10);
    _itoa(seconds, s_seconds, 10);
    
    strcpy(answer,s_degrees);//will append   
    strcat(answer,char_degree);
    strcat(answer,s_minutes);//will append 
    strcat(answer,"\' ");
    strcat(answer,s_seconds);//will append 
    strcat(answer,"\" ");
    //p_answer=answer;  
    //return (p_answer);
    return (answer); 
    
}

long double degrees_to_decimal(char string[])  
{  
int cnt=0;
int length=0,i;
long double degrees, minutes, seconds;  
char string_number[15]; 
long double answer,minutes_fraction,seconds_fraction;
int cnt_comas=0;   
int check_next=0;

length=strlen(string);
/*   
for (i=0; i!=(length+1); i++)
  {
    if (check_next==1) 
    	{if ((string[i]==',')||(string[i]=='\0')||(cnt_comas>2)) string[i]='0';
    	 else check_next=0;	
    	} 
    if (string[i]==',')
    	{cnt_comas++; check_next=1;}  
    
    //if (cnt_comas>2){return -99999; } //place a null 0 on the 3rd coma if there is one  (this will shorten the string so it will only have two comas.  (Only used when the user enters bad input)  without it 
   }  
   */
//length=strlen(string); 
strcpy(string_number, "            ");   //copy a \0 to the whole string to reset it to nothing    ******need this or it will have garbage in it before you start  with out it this would happen: if you entered a 4 you would get 47
degrees=0; minutes=0; seconds=0;
for (i=0; i!=(length+1); i++)    
	{if ((string[i]!='1')&&(string[i]!='2')&&(string[i]!='3')&&(string[i]!='4')&&(string[i]!='5')&&(string[i]!='6')&&(string[i]!='7')&&(string[i]!='8')&&(string[i]!='9')&&(string[i]!='0')&&(string[i]!=',')&&(string[i]!='\0'))       
		{cout<<"Bad input.  Shame on you!"<<flush; break;}  
	 if (string[i]=='\0') 
	 	{strcat(string_number, "\0");  //append \0 to make it a finished string
	 	 seconds=_atold(string_number);}      //convert into a long double
	 if (string[i]==',') 
	 	{cnt=0;
	 	 strcat(string_number, "\0");   //append \0 to make it a finished string
	 	 if (degrees==0) degrees=atoi(string_number); 
	 	 else  if (minutes==0) minutes=atoi(string_number);	 	 
	 	 strcpy(string_number, "            ");   //copy a \0 to the whole string to reset it to nothing
	 	}
	 else  {string_number[cnt]=string[i];
	 		cnt++;} 	 
	} 

for (i=0; i!=length; i++)
	{if (string[i]==',') cnt_comas++;}
 
if (cnt_comas==2)     //hopfully user has three inputs with comas between them   //if not find out how many comas there are and still have it input something
	{seconds_fraction=seconds*.0002777777778;  
	minutes_fraction=(minutes*60)*.0002777777778;
	answer=degrees+minutes_fraction+seconds_fraction; 
    
	}
if (cnt_comas==1)     
	{
	 minutes_fraction=(seconds*60)*.0002777777778;   
	 seconds_fraction=0;
	 answer=degrees+minutes_fraction+seconds_fraction; 
	 
	}
if (cnt_comas==0)
	{
	 degrees=seconds;
	 minutes_fraction=0;
	 seconds_fraction=0;
	 //return seconds;
	}
if (cnt_comas>2) return -99999;			
answer=degrees+minutes_fraction+seconds_fraction; 	
//1 second is = to .00027778 degrees
return answer;
}
 
long double auto_degrees_decimal(char message[],int row_string,int colum_string,int row_prompt, int colum_prompt)  
{
	long double longdouble;
	char buffer[15];
	int i=-1, c;  
//	len=strlen(message);                  //see how many charchacters make up the string
	//_settextposition (row+number_of_returns, (colum-len));
	_settextposition (row_string, colum_string); 
	//_settextposition (20, 10);   
	_outtext(message);
    
  	while (i < 10) 			     
	   {_settextposition (row_prompt, colum_prompt); 
		c=getch();  
		//cout<<c<<flush; 
	  	 
		if (((c>=48)&&(c<=57))||(c==27)||(c==13)||(c==8)||(c==44))   //or a coma  //if the input keys are any of the number keys    find out what they are and add the to the string.
	    	{
	    	 i++;
	    	 colum_prompt++;    
	    	 if (c==0x8) //backspace
	   			{i--;
	   			colum_prompt--;
	   			colum_prompt--;   
	   		 	_settextposition(row_prompt, colum_prompt);   
	   		 	cout<<" "<<flush;
	   		 	buffer[i]=' ';
	   		 	i--;}
	    	
	    	if (c==44)     //only let it get 2 comas (degrees,minutes,seconds)  
	    	   {
	   			buffer[i] = ',';  
	   			cout<<","<<flush;  
	   		   } 	    	  
	    	if (c==48)
	   			{buffer[i] = '0';
	   			//_outtext("0");}                     //the i= are for displaying the input with the cout and the bottom of this loop.
	  		     cout<<"0"<<flush;}
	  		if (c==49)
	   			{buffer[i] = '1';
	  			 //_outtext("1");} 
	  			 cout<<"1"<<flush;}
	  		if (c==50)
	   			{buffer[i] = '2';
	   			//_outtext("2");}
	   			cout<<"2"<<flush;}
	  		if (c==51)
	   			{buffer[i] = '3';
	   			//_outtext("3");}
	   			cout<<"3"<<flush;}
	  		if (c==52)
	   			{buffer[i] = '4';
	   			//_outtext("4");}
	   			cout<<"4"<<flush;}
	  		if (c==53)
	   			{buffer[i] = '5';
	   			//_outtext("5");}
	   			cout<<"5"<<flush;}
	  		if (c==54)
	   			{buffer[i] = '6';
	   			//_outtext("6");}
	   			cout<<"6"<<flush;}
	  		if (c==55)
	   			{buffer[i] = '7';
	   			//_outtext("7");}
	   			cout<<"7"<<flush;}                                      
	  		if (c==56)
	   			{buffer[i] = '8';
	   	   		//_outtext("8");}
	   	   		cout<<"8"<<flush;}
	  		if (c==57)
	   			{buffer[i] = '9';
	   			//_outtext("9");}
	   			cout<<"9"<<flush;}
	  		//if (c==46)
	   		//	{buffer[i] = '.';
	   			
	   		//	cout<<"."<<flush;}  
	   	//	if (c==45) 
	   		//	{buffer[i] = '-';
	   			
	   		//	cout<<"-"<<flush;}
	   		if (c==27)   //the esc key
	   			{//_settextposition (row_prompt, (((colum_prompt-i)-len)-1)); 
	   			 //cout<<"                                           "<<flush;    //cover up any message that was displayed
	   			 return -99999;}  //esc key	   return a strange number and check for it in the calling function so you know if the esc was hit         
	        if (c==13)  //enter
	        	{//_settextposition (row_prompt, (((colum_prompt-i)-len)-1)); 
	   			//cout<<"                                           "<<flush;    //cover up any message that was displayed
	        	 break;  }	//will exit the loop and do the rest of the function before returning   	
	    	} 
		 
	
		}//end of for loop		    	
	
  	buffer[i] = '\0';   //terminate string with null character:				   
  	
    
  	longdouble=degrees_to_decimal(buffer);
  	
  	//longdouble=_atold(buffer);
  	return longdouble;				   
  
}
  	     
 
 
 
void quick_rezero (char xyzletter) 
{
long double dif;
switch (xyzletter)
  {case ('x'):{if (mode!=0)  //if not in master mode '0' then rezero with this   
                   {
                    
                     if (encoder1>backlash_X.currently_displayed) dif=encoder1-backlash_X.currently_displayed;
    		 		 else dif=backlash_X.currently_displayed-encoder1;
    		 		 if (dif>(backlash_X.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_X.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_X.tension=1; }
    	         		 else {backlash_X.tension=0; }
    		 		    }
    		 	    switch (mode) //if in any other mode than master mode
        			   {case (1): { mode1.xoffset=encoder1;break;}
             		 	case (2): { mode2.xoffset=encoder1;break;}
            			case (3): { mode3.xoffset=encoder1;break;}
            		 	case (4): { mode4.xoffset=encoder1;break;}  
            		   }
    		 	    backlash_X.currently_displayed=encoder1;
    		 	    zero_all_X_mode=1;  //this turns on the zero stuff in the backlash function
    		 		coder1.possible_error_cnt=0; //set the error counter back to zero too
                   // coder1.changed=1;
                   }
               else{  //else do this if in master mode  (also will do this if useing rotary)
       		   		 if (encoder1>backlash_X.currently_displayed) dif=encoder1-backlash_X.currently_displayed;
    		 		 else dif=backlash_X.currently_displayed-encoder1;
    		 		 if (dif>(backlash_X.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_X.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_X.tension=1; }
    	         		 else {backlash_X.tension=0; }
    		 		    }
    		 	     zero_all_X=1;  //this turns on the zero stuff in the backlash function
    		 		 backlash_X.currently_displayed=0;
    		 		 encoder1=0; 
    		 		 coder1.possible_error_cnt=0; //set the error counter back to zero too
    			     break;
    			  }
    		 break;
    		 }
    			
    			 
    case ('y'):{if (mode!=0)  //if not in master mode '0' then rezero with this   
                   {
                    
                     if (encoder2>backlash_Y.currently_displayed) dif=encoder2-backlash_Y.currently_displayed;
    		 		 else dif=backlash_Y.currently_displayed-encoder2;
    		 		 if (dif>(backlash_Y.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_Y.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Y.tension=1; }
    	         		 else {backlash_Y.tension=0; }
    		 		    }
    		 	    switch (mode) //if in any other mode than master mode
        			   {case (1): { mode1.yoffset=encoder2;break;}
             		 	case (2): { mode2.yoffset=encoder2;break;}
            			case (3): { mode3.yoffset=encoder2;break;}
            		 	case (4): { mode4.yoffset=encoder2;break;}  
            		   }
    		 	    backlash_Y.currently_displayed=encoder2;
    		 	    zero_all_Y_mode=1;  //this turns on the zero stuff in the backlash function
    		 		coder2.possible_error_cnt=0; //set the error counter back to zero too
               
                   }
               else{  //else do this if in master mode  (also will do this if useing rotary)
       		   		 if (encoder2>backlash_Y.currently_displayed) dif=encoder2-backlash_Y.currently_displayed;
    		 		 else dif=backlash_Y.currently_displayed-encoder2;
    		 		 if (dif>(backlash_Y.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_Y.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Y.tension=1; }
    	         		 else {backlash_Y.tension=0; }
    		 		    }
    		 	     zero_all_Y=1;  //this turns on the zero stuff in the backlash function
    		 		 backlash_Y.currently_displayed=0;
    		 		 encoder2=0; 
    		 		 coder2.possible_error_cnt=0; //set the error counter back to zero too
    			     break;
    			  } 
    		 break;	  
    		 }
   case ('z'):{if (mode!=0)  //if not in master mode '0' then rezero with this   
                   {
                    
                     if (encoder3>backlash_Z.currently_displayed) dif=encoder3-backlash_Z.currently_displayed;
    		 		 else dif=backlash_Z.currently_displayed-encoder3;
    		 		 if (dif>(backlash_Z.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_Z.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Z.tension=1; }
    	         		 else {backlash_Z.tension=0; }
    		 		    }
    		 	    switch (mode) //if in any other mode than master mode
        			   {case (1): { mode1.zoffset=encoder3;break;}
             		 	case (2): { mode2.zoffset=encoder3;break;}
            			case (3): { mode3.zoffset=encoder3;break;}
            		 	case (4): { mode4.zoffset=encoder3;break;}  
            		   }
    		 	    backlash_Z.currently_displayed=encoder3;
    		 	    zero_all_Z_mode=1;  //this turns on the zero stuff in the backlash function
    		 		coder3.possible_error_cnt=0; //set the error counter back to zero too
  
                   }
               else{  //else do this if in master mode  (also will do this if useing rotary)
       		   		 if (encoder3>backlash_Z.currently_displayed) dif=encoder3-backlash_Z.currently_displayed;
    		 		 else dif=backlash_Z.currently_displayed-encoder3;
    		 		 if (dif>(backlash_Z.dist*.25))// *.25 is the same as /4 (1/4th) //(x_unit*5))  //take about 5 units and see if we are over that many blips and if so the user must of came up from the other way with the tension before he rezeros.  
    		 		                               //so then whatever the current lash direction is it needs to be set the other way.  If not leave it along. It is already set correct
    		 		 	{
    		 		 	 if (backlash_Z.tension==0)  //only let it do this once per call to this function.  This is to keep 
    		 		 	 	{backlash_Z.tension=1; }
    	         		 else {backlash_Z.tension=0; }
    		 		    }
    		 	     zero_all_Z=1;  //this turns on the zero stuff in the backlash function
    		 		 backlash_Z.currently_displayed=0;
    		 		 encoder3=0; 
    		 		 coder3.possible_error_cnt=0; //set the error counter back to zero too
    			     break;
    			  } 
    	     break;  
    		 }
    			 			 	 		 			  		
    	 		
  } 
 
 
} 
 
 
void open_scroll_help()                 /* AGE1 */
 {
 	int lines_down=2; 
    int cntt;
 	int max_characters=60;
 	FILE *stream;
    int   cnt, h;
 	char file_name[]="quad.hlp"; 
    char text_line[60];
 	
 	_settextcursor (0x2000);  //no cursor  //0x0707 is the regular cursor                            
 	_setvideomode(_TEXTC80);   
 	_setbkcolor (9);  //blue
 	_clearscreen(_GCLEARSCREEN); 
 	_settextposition (25,1);
 	_setbkcolor (15);   _outtext("                                                                                "); 
 	_settextposition (24,1);
 	_setbkcolor (9);  _outtext("                                                                                "); 
 	_setbkcolor (15); 
 	_settextposition (1,1);
 	_setbkcolor (15); _outtext("                                                                                ");
 	_settextposition (1,38);    
    _settextcolor(0);    //black
 	_outtext("HELP");
 	_settextposition (25,1);
 	_outtext("      Up & Down arrow keys        PgUp        PgDn        Esc to return...  ");           
    _settextcolor(14); _outtext(" "); //couts will print the color of whatever was set the lasttime _outtext was used (so no couts will be yellow)
    
    _settextwindow(2,10,24,70);  
    _setbkcolor (9); //8 is black 14  yello    9 is blue
    _clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   
    
    
    
    _settextposition (11,1); 
    stream=fopen(file_name,"r");
 	if (!stream) 
 		{ cout<<"                 File does not exist"
 		<<"\n"<<"                    Press any key to continue..."<<flush; _getch(); return;}  
 
    do
		{
	      fseek(stream,0L, SEEK_SET);
	      _clearscreen(_GWINDOW);
	      if (lines_down<=23)  lines_down=23;
		  cntt=0;
	      for (cnt=0; cnt<=lines_down; cnt++)    
	      	{fgets (text_line, 75, stream);
	      
        	   if(cnt>=(lines_down-21))   //only print within 23 lines to the end of the for loop
        	   	     {cntt++; 
        	   	      
        	   	     _settextposition(cntt,1); _outtext(text_line);}//cout<<"\n"<<flush; 
        	   	  
             }
        backup:
            
         h=_getch(); if (h==0) h=_getch();
		 if (h==DOWNARROW)
		    {// _clearscreen(_GWINDOW);
		     lines_down=lines_down+1;}
		 if (h==UPARROW)
		    {//_clearscreen(_GWINDOW);
		     lines_down=lines_down-1;}
		 if (h==PAGEDOWN)
		    {//_clearscreen(_GWINDOW);
		     lines_down=lines_down+21;}
		 if (h==PAGEUP)
		    {//_clearscreen(_GWINDOW);
		     lines_down=lines_down-21;}
		 if ((h!=PAGEUP)&&(h!=PAGEDOWN)&&(h!=UPARROW)&&(h!=DOWNARROW)&&(h!=ESC))
		     {cout<<"\a"<<flush;    //beep
		      goto backup;     } 
		
         
	   }while (h!=27);
       fclose (stream);
 
  return;
 
 }	
 
 
 
void overlay_box ()
{                      
/*AGE1 Below is an attempt to clear the first background screen*/
_settextwindow(5,15,23,65);    //set screen size to bigh background
_setbkcolor (9);               // blue is wanted
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   


_settextwindow(5,15,20,65);    //largest is 1,1,25,40//this would be full screen
_setbkcolor (0);    
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   
_settextwindow(5,15,19,64);    //largest is 1,1,25,40//this would be full screen
_setbkcolor (15);  //7  
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN 

}     		  
            
  void smalloverlay_box ()
{                     
//  small
_settextwindow(10,15,23,65);    //set screen size to bigh background
_setbkcolor (9);               // blue is wanted  red=4 yellow=14 brightwhite=15 normal white =7   black=0   light blue=9
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   


_settextwindow(11,17,20,65);    //largest is 1,1,25,40//this would be full screen
_setbkcolor (0);    
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   
_settextwindow(11,17,19,64);    //largest is 1,1,25,40//this would be full screen
_setbkcolor (15);  //7  
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN 

}     		  
 
            
            
            
            
void bigoverlay_box ()
{  
_settextwindow(3,10,23,70);    //largest is 1,1,25,40//this would be full screen
_setbkcolor (0);    
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN   
_settextwindow(3,10,22,69);    //largest is 1,1,25,40//this would be full screen
_setbkcolor (15);  //7  
_clearscreen(_GWINDOW); //_GVIEWPORT _GCLEARSCREEN 
}     		   
 
 
 /* 
 again:   
 
                          
 _clearscreen(_GCLEARSCREEN); 
//_setvideomoderows(_TEXTC80,1 );  
 _setvideomode (_DEFAULTMODE); 
 _settextposition (29,60); cout<<"Esc to Quit"<<flush; 
 _settextposition (1,1);
 cout<<"                                ENCODER READER"<<"\n\n";
 cout<<"1: read DATA REGISTER"<<"\n"<<"2: read STATUS REGISTER"<<"\n"<<"3: read CONTROL REGISTER"<<"\n"
 <<"4: send bits to data register"<<"\n"<<"5: send bits to control register"<<"\n"<<"6: Change Port"<<"\n"<<"7: Loop reading encoder value"<<"\n"
 <<"F1: Read encoders"<<"\n"<<"F2: Zero encoders"<<"\n"<<"F3: Time display value"<<"\n"<<"F4: Change encoder unit"<<"\n"
 <<"F5: Backlash Util"<<"\n"<<flush;
 cout<<flush;  //have to use this when you use things like _getch.  Otherwise the cout statements won't show up.
 
 c = _getch(); 
 if (c==0) c = _getch();  //this if is because when the function keys are pressed they will return 0 and if read again return the decimal code for that key.  (they need to be read twice)
 //cout<<c<<flush; 
 if ((c==113)||(c==81)||(c==27))           //if Q or q or ESC then quit  
    { _setvideomode (_DEFAULTMODE); return 0;} 
 if (c==49)    //the 1 key    
    {getnum = _inp (dataport); 
     cout<<"The data register is a decimal: "<<getnum<<"\n"; goto again;}    
 if (c==50)   //the 2 key      
    {getnum = _inp (statusport);   
 	 cout<<"The status register is a decimal: "<<getnum<<"\n"; goto again;}  
 if (c==51) //the 3 key
    {getnum = _inp (controlport);    
 	 cout<<"The control register is a decimal: "<<getnum<<"\n"; goto again;}   
 if (c==52)//the 4 key
    {cout<<"Enter decimal to send to the data register: "<<"\n";
 	 cin>> getnum;  _outp (dataport, getnum ); goto again;} 	
 if (c==53)//the 5 key
 	{cout<<"Enter decimal to send to the control register: "<<"\n";
 	 cin>> getnum;  _outp (controlport, getnum ); goto again;} 
 if (c==54)//the 6 key  
    {cout<<"Enter the port decimal... lpt1-888, lpt2-632, lpt3-956"<<"\n";
 	 cin>> getnum; 
 	 if (getnum == 888) {dataport=888; statusport=889; controlport=890;}
 	 if (getnum == 632) {dataport=632; statusport=633; controlport=634;}
 	 if (getnum == 956) {dataport=956; statusport=957; controlport=658;}
 	 goto again;}  
 if (c==55)//the 7 key	
    {cout<<"number of loops to make while encoder changes: "<<"\n";
 	 cin>>loops; 
 	 ctr = 0;
 	 while  (ctr!=loops)
 	 	{getnum = _inp (dataport);  
 	 	if (getnum != savegetnum) 
 	 	   {ctr++;
 	 	    array [ctr] = getnum;} 
 	 	    savegetnum = getnum;
 	 	   }
 	 	for (ctr=0; ctr<=loops; ctr++)   
		   {cout <<array [ctr]<<"\n";} 	
 	 	goto again;}   
 if (c==60) //the F2 key
     {encoder1=0; encoder2=0; encoder3=0; goto again;}  
 if (c==61)//the F3 key
    {cout<<"Curent time display is "<<timedisplay<<"   Enter new time display value:";
 	 cin>>timedisplay; goto again;}  
 if (c==62)//the F4 key
    {printfloat=unit; //convert unit from long double to float 
	 sprintf(string, "%.20f", printfloat); //convert printfloat into a string
    cout<<"current unit is "<<string<<"  Enter new value ";
    cin>>unit; goto again;}	 
 if (c==63) { backlash_setup(); goto again;}//the F5 key            	 
 if ((c!=63)&&(c!=59)&&(c!=49)&&(c!=113)&&(c!=81)&&(c!=27)&&(c!=50)&&(c!=51)&&(c!=52)&&(c!=60)&&(c!=61)&&(c!=54)&&(c!=55)&&(c!=62)) {cout<<"\a";goto again;}   //beep   //59 is F1 this will let it go through to the rest of the program from here

   */
  

  
  
  
  
  
  
  
       
       
 //cout <<setiosflags(ios::fixed);  //this will keep expotents from printing with cout
//cout<<setprecision(4);  //set cout precision to 4 places right of the decimal    
     
       //for (ctr=0; ctr<=7; ctr++)
//	{outt.SaveBinary [ctr] = 0;}
// for (count=0; count<=10; count++) 
//cout <<_setfont("t'ROMAN'h60w50b");

       //_clearscreen(_GCLEARSCREEN); 
      //_gcvt (encoder1, 4, string);  
      
      //_moveto (50,200);
      //_outgtext (string);  
      

      //sprintf(string, "X %f", encoder1); //copy the cordinates into a character buffer along with the "X" or "Y" or "Z" character
      //_outgtext (string); 
     
      //_moveto (50,300); 
      //sprintf(string, "Y %f", encoder2);
      //_outgtext (string);  
    
      //_moveto (50,400);
      //sprintf(string, "Z %f", encoder3);
      //_outgtext (string);    
      
      //cout<<encoder1<<"\n"<<encoder2<<"\n"<<encoder3;  
    
  
	  
/* Set 43-line graphics mode if available. */  
/*  struct _videoconfig config;
   if( !_setvideomoderows(_VRES16COLOR, 43 ) )  //640x480 16 color
   {
      _outtext( "EGA or VGA required" );
      exit( 1 );
   }
   _getvideoconfig( &config );  
   if (_registerfonts ("roman.fon" ) < 0) cout<<"cannot find ROMAN.FON";       
*/   
  
  // if (_registerfonts ("roman.fon" ) < 0) cout<<"cannot find ROMAN.FON";       
     	  
	  

                                                  
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    
                                                    