#include <stdio.h>
#include <conio.h>
#include <dos.h>

//Neill Li 2 - BS CS
//Hans Tan 4 - BS ME

int DataPort = 0x378;		//the data port of the parallel port has 0x378 as its address
int StatPort = 0x379;		//the status port of the parallel port has 0x379 as its address
int CtrlPort = 0x37a;		//the control port of the parallel port has 0x37a  as its address
int ctr2=0;

int forw[] = { 152, 28, 52, 38, 98, 67, 193, 137, 152};
int turnRight[] = { 144, 16, 48, 32, 96, 64, 192, 128, 144};
int turnLeft[] = { 8, 12, 4, 6, 2, 3, 1, 9, 8};

void forward()
{
	int ctr = 0;
	while (ctr < 9)
	{
		outportb (0x378, forw[ctr]);
		ctr++;
		delay(1);
	}
}
void backward()
{
    int ctr = 0;
	while (ctr < 9)
	{
		outportb (0x378, forw[ctr]);
		ctr--;
		delay(1);
	}
}

void right()
{
	int ctr1 = 0;
	while (ctr1 < 9)
	{
		outportb (0x378, turnRight[ctr1]);
		ctr1++;
		delay(1);
	}
}

void left()
{
	int ctr2 = 0;
	while (ctr2 < 9)
	{
		outportb (0x378, turnLeft[ctr2]);
		ctr2++;
		delay(1);
	}
}

void main ()
{


	int Orig37a = 0;
	int NewCtrl = 0;
	int NewCtrlLeft = 0;
	int DataLeft = 0;
	int NewCtrlRight = 0;
	int DataRight = 0;
	int ctr = 0;
	int prevdleft=0;
	int prevdright =0;
	int cycle = 0;

	ctr2++;

	Orig37a = inportb(CtrlPort);
	NewCtrl = Orig37a | 0x20;
	outportb (CtrlPort, NewCtrl);

	while(ctr<5)
	{
	    //Read from left
		NewCtrlLeft = NewCtrl | 0x03;
		outportb (CtrlPort, NewCtrlLeft);
		DataLeft = inportb (DataPort);
		printf("\n1dataleft %d",DataLeft);
		NewCtrlLeft =NewCtrl & 0xFC;
		delay(1);

	    //Read from right
		NewCtrlRight = NewCtrl | 0x01;
		outportb (CtrlPort, NewCtrlRight);
		DataRight = inportb (DataPort);
		printf("\n2dataright %d",DataRight);
		NewCtrlRight =NewCtrl & 0xFC;
		ctr++;
	}
	ctr=0;
	prevdleft=DataLeft;
	prevdright=DataRight;

	printf ( " \n Initial DataLeft %d", DataLeft);
	printf ("\n iniital DataRight %d", DataRight);

	while(ctr<5)
	{
	    //Read from left
		NewCtrlLeft = NewCtrl | 0x03;
		outportb (CtrlPort, NewCtrlLeft);
		DataLeft = inportb (DataPort);
		NewCtrlLeft =NewCtrl & 0xFC;

	    //Read from right
		NewCtrlRight = NewCtrl | 0x01;
		outportb (CtrlPort, NewCtrlRight);
		DataRight = inportb (DataPort);
		NewCtrlRight =NewCtrl & 0xFC;
		ctr++;
	}

	ctr=0;
	printf ( " \n 2nd DataLeft %d", DataLeft);
	printf ("\n 2nd DataRight %d", DataRight);

	while(DataRight>prevdright || DataLeft>prevdleft)  // this loop is used to search for the max sound
	{
		prevdleft=DataLeft;
		prevdright=DataRight;

		printf ( " \n Up DataLeft %d", DataLeft);
		printf ("\n Up DataRight %d", DataRight);
			while(ctr<5)
			{
	    //Read from left
		NewCtrlLeft = NewCtrl | 0x03;
		outportb (CtrlPort, NewCtrlLeft);
		DataLeft = inportb (DataPort);
		NewCtrlLeft =NewCtrl & 0xFC;

	    //Read from right
		NewCtrlRight = NewCtrl | 0x01;
		outportb (CtrlPort, NewCtrlRight);
		DataRight = inportb (DataPort);
			NewCtrlRight =NewCtrl & 0xFC;
			ctr++;
		}
		ctr=0;
	}

	outportb (0x37a, Orig37a);

	printf ( " \n Comparison DataLeft %d", DataLeft);
	printf ("\n Comparison DataRight %d", DataRight);

	if(DataLeft ==0 && DataRight == 0)
	{
		//stop the stepper
	}

	else if (DataLeft == DataRight)
	{
		delay(1);
		while (cycle < 1000)
		{
			 forward();
			 cycle++;
		}
	}

	else if (DataLeft > DataRight)
	{
		delay(5);
		while (cycle < 1000)
					 {
							 right();
							 cycle++;
					 }
		//make the delay a variable and increase the speed ofthe left
	}
	else
	{
		delay(5);
		while (cycle < 1000)
					 {
							left();
							cycle++;
					 }
					 //do the same for the above except for the right
	}

}

