int startUpDelay = 50;


int ATCreleasePort = 2;
int ATCreleasePin = 16;
int ATCclampPort = 3;
int ATCclampPin = 3;
int ATCreleaseOutLED = 17;
int ATCreleaseLED = 7;
bool ATCbuttonPressed = false;
int BlowChuckport = 1;
int BlowChuckpin = 17;

int popupPort = 3;
int popupPin = 14;
int VakuumPort = 3;
int VakuumPin = 16;

int DammskyddUppPort = 1;
int DammskyddUppPin = 1;
int SpansugPort = 3;
int SpansugPin = 9;

int dustshoePort = 1;
int dustshoePin = 1;

bool ResetKlar = false;
bool servoErrorOutSet = false;
bool dustshoeButtonPreviouslyPressed = false;
bool vakuumButtonPreviouslyPressed = false;
bool popupButtonPreviouslyPressed = false;


while(RunMacroPump)
{
	Thread.Sleep(50);
	if(exec.GetLED(ATCreleaseLED) && !exec.GetLED(ATCreleaseOutLED) && !exec.GetLED(50)) //Om spindel cw on
	{ 
		exec.Setoutpin(ATCreleasePort, ATCreleasePin);//Port / pin släpp kona
		exec.Clroutpin(ATCclampPort, ATCclampPin);//Port / pin Dra kona
		exec.Setoutpin(BlowChuckport, BlowChuckpin); // Blow the chuck with pneumatic valve
		ATCbuttonPressed = true;
			
	}
	else if(!exec.GetLED(ATCreleaseLED) && (ATCbuttonPressed) )
	{
		exec.Clroutpin(ATCreleasePort, ATCreleasePin);
		exec.Setoutpin(ATCclampPort, ATCclampPin);
		Thread.Sleep(500);
		exec.Clroutpin(BlowChuckport, BlowChuckpin); // Blow the chuck with pneumatic valve
		ATCbuttonPressed = false;
	}

	
	if(exec.GetLED(19) || AS3.Getbutton(130) ) //Om run
	{
		exec.Clroutpin(popupPort, popupPin);//stoppa popup
		

	}
	
		if(AS3.Getbutton(130) ) //Om STOPPKNAPP
	{
		exec.Clroutpin(DammskyddUppPort, DammskyddUppPin); // Kör upp dammskydd
		exec.Clroutpin(SpansugPort, SpansugPin); // Stoppa Spånsug
		exec.Clroutpin(VakuumPort, VakuumPin); // Stoppa Vakuum
	}
	
			if(exec.GetLED(25) ) //Om Cykelstopp	
	{

		exec.Clroutpin(DammskyddUppPort, DammskyddUppPin); // Kör upp dammskydd
		exec.Clroutpin(SpansugPort, SpansugPin); // Stoppa Spånsug
		exec.Clroutpin(VakuumPort, VakuumPin); // Stoppa Vakuum
	}
	bool dustshoeButtonPressedCurrently = AS3.Getbutton(20103);

    if (dustshoeButtonPressedCurrently && !dustshoeButtonPreviouslyPressed)
    {
        if (!exec.GetLED(1)) // Om spånkopp Uppe 
        { 
            exec.Setoutpin(dustshoePort, dustshoePin); // Kör ner spånkopp
        }
        else // Om spånkopp nere (If vacuum is on)
        {
            exec.Clroutpin(dustshoePort, dustshoePin); // Kör upp spånkopp
        }
    }
    dustshoeButtonPreviouslyPressed = dustshoeButtonPressedCurrently;
	
	bool vakuumButtonPressedCurrently = AS3.Getbutton(20105);
	
	if (vakuumButtonPressedCurrently && !vakuumButtonPreviouslyPressed)
	{	
	if(!exec.GetLED(101))
		{
			exec.Setoutpin(VakuumPort, VakuumPin); // Kör popup

		}
	else
		{
			exec.Clroutpin(VakuumPort, VakuumPin); // Kör popup
		
		}
	}
	vakuumButtonPreviouslyPressed = vakuumButtonPressedCurrently;
	
	bool popupButtonPressedCurrently = AS3.Getbutton(20104);
	
	if (popupButtonPressedCurrently && !popupButtonPreviouslyPressed)
	{
	if(!exec.GetLED(50) && !exec.GetLED(99)) //Om spindel cw on
		{
			exec.Setoutpin(popupPort, popupPin); // Kör popup

		}
	else
		{
			exec.Clroutpin(popupPort, popupPin); // Kör popup
		
		}
	}
	popupButtonPreviouslyPressed = popupButtonPressedCurrently;
}

	

#Events

static int delayCounterBeforeStart = 0;
static bool RunMacroPump = true;
 

