PROGRAM Elevator;
{Name:		Gregory A. Perkins
			CS 65
  Due:		11 October 1996}
  
{This program demonstrates the ability of software to make the logical decisions
 needed to control an elevator.  The program simulates the elevators actions and
 the necessary commands to the hoist and doors by display those actions on the
 screen.  "DELAYS" are placed throughout the program to simulate the natural
 pauses of operation of an actual elevator.}
 
{************ THE PROGRAM DOES NOTHING TO PREVENT THE USER ****************}
{************ FROM ENTERING NEGATIVE FLOOR NUMBERS.  IT IS ****************}
{************ ASSUMED BY THE PROGRAMMER THAT THE CONTROL   ****************}
{************ PANEL WILL NOT HAVE THESE NUMBERS ON IT!!!!! ****************}
 
USES cs65;  {The cs65 unit contains the DELAY procedure.}

{Declarations section}
VAR FloorNum, Cycle, NewFloor : INTEGER;
VAR NumFloors, Step, Incr : INTEGER;
VAR Up, Down, Stop, Open, Close : BOOLEAN;
CONST Trips = 10;

BEGIN {program}

	{Initialize variables}
	FloorNum := 1;
	Up := FALSE; Down := FALSE; Close := FALSE;
	Stop := TRUE; Open := TRUE; {simulates hoist stopped and doors open initially}

	{Inform user of current elevator position.}
	WRITELN('The elevator is currently at Floor 1.');
	WRITELN('The doors to the elevator are currently open.');

	{main loop}
	FOR Cycle := 1 TO Trips DO

		BEGIN {main loop}
	
			{Prompt the user for a floor.}
			WRITE('The next request comes from floor: ');
			READLN(NewFloor);

			{Go to that floor after deciding which way to go.}
			NumFloors := NewFloor - FloorNum;
			
			IF NumFloors <> 0 THEN
				BEGIN{Actions if user picks different floor from one currently on}
					Close := TRUE; Open := FALSE;
					WRITELN;
					WRITELN('Sending "close" signal to doors.');
					DELAY(10000);
					WRITELN('The doors are now closed.');
					WRITELN;
					
					IF NumFloors > 0 THEN
		
						BEGIN
							Up := TRUE; Down := FALSE; Stop := FALSE;
							WRITELN('Sending "up" signal to hoist.');
							DELAY(5000);{adds realism -- elevators don't "shoot up"}
							Incr := 1;
						END
		
						ELSE BEGIN
							Down := TRUE; Up := FALSE; Stop := FALSE;
							WRITELN('Sending "down" signal to hoist.');
							DELAY(5000);{adds realism -- elevators don't "shoot up"}
							Incr := -1;
						END;
		
					{Make NumFloors positive.}
					NumFloors := ABS(NumFloors);
	
					{elevator motion loop}
					FOR Step := 1 TO NumFloors DO
						BEGIN {motion loop}
							FloorNum := FloorNum + Incr;
							DELAY(5000);
							WRITELN('At floor ', FloorNum:1, '...');
						END; {motion loop}
		
					{Elevator has arrived at new floor.}
					Up := FALSE; Down := FALSE; Stop := TRUE;
					WRITELN('The elevator has arrived at floor ', FloorNum:1, '.');
					WRITELN;
					WRITELN('Sending "stop" signal to hoist.');
					DELAY(10000);{adds realism -- elevators don't "stop on a dime."}
					Open := TRUE; Close := FALSE;
					WRITELN('Sending "open" signal to doors.');
					DELAY(10000);
					WRITELN('The doors are now open.');
					WRITELN;{blank line adds to the readability of the execution}
	
				END
				
				ELSE BEGIN {Actions if user picks the CURRENT floor}
					WRITELN;
					WRITELN('You are already on floor ', FloorNum:1, '.');
					DELAY(5000);
					WRITELN;
				END;
					
			{warning of last trip}
			IF Cycle = Trips - 1 THEN
				WRITELN('You have one trip left.');
									
		END; {main loop}
	
END. {program}