#include <stdio.h>
#include <hc11.h>

#define topofrobot *(unsigned char volatile *)	(0xB590)
#define	limitswitches *(unsigned char volatile *) (0xB5C0)


void main (void)
{
   int helper=0, ball_deploy=0, raise_var=0, counter=0, stop=0;
// enable the robot arm
// this allows the displays to be active and motors to be going forward
   topofrobot = 0x07;
   helper = limitswitches & 0x30; 
while (1)
{   
while (stop == 0)
{
   scanf ( "%d" , &stop );

   if ( helper == 0x10 )  // crane is at ball station and ready to lower
   { 
	  while ( helper != 0x20 ) // while crane isnt down
	  {
   	   		
			topofrobot = 0x57; // turn on magnets and motor 
   			helper = limitswitches & 0x30; // reset helper
	  		if ( helper == 0x20 )	// when crane is down
	  		{
	   	   	   while ( helper != 0x10 )     // when crane is not in up position
			   {
			  
			   		 topofrobot = 0x4F; 	  	   		// bring crane back up
	  		   		 helper = limitswitches & 0x30;
			   		 raise_var = 1;
			   }
			}		
	   if ( raise_var == 1 )
	   {
	   raise_var = 0;
	   break;
	   }
	   }
	 while (counter <= 1000)
	 {
	
      topofrobot = 0x03; 					//when crane is back up, turn 
	  counter ++;
	 }
	 counter = 0; 			   							//everything off but leave wheels forward 
   }
   topofrobot = 0x03;
}   
}
}	
	
	  	
