I didn't wait for you answer. Set the limits and the output pins as your needs. If the outputs are active low, change ventxNeg to "true". Save code to Mxxxxx.txt where xxxxx is a number (e.g. M10000.txt) and go to General settings / Config macroloops, add 10000 (or the number you selected) to an empty row, set Auto run, click Run, click Save settings. Ready.
Code:
// ================================================================================================
// Auto open vents
// ================================================================================================
int newState = 0;
double yPos = 0.0;
if (exec.GetLED(RunLED))
{
yPos = exec.GetYmachpos();
if ((yPos >= lim0) && (yPos < lim1))
newState = 1;
else if ((yPos >= lim1) && (yPos < lim2))
newState = 2;
else if ((yPos >= lim2) && (yPos < lim3))
newState = 3;
else if ((yPos >= lim3) && (yPos < lim4))
newState = 4;
}
if (newState != lastState)
{
Clroutpin(vent1Port, vent1Pin, vent1Neg);
Clroutpin(vent2Port, vent2Pin, vent2Neg);
Clroutpin(vent3Port, vent3Pin, vent3Neg);
Clroutpin(vent4Port, vent4Pin, vent4Neg);
switch (newState)
{
case 1:
Setoutpin(vent1Port, vent1Pin, vent1Neg);
break;
case 2:
Setoutpin(vent2Port, vent2Pin, vent2Neg);
break;
case 3:
Setoutpin(vent3Port, vent3Pin, vent3Neg);
break;
case 4:
Setoutpin(vent4Port, vent4Pin, vent4Neg);
break;
}
lastState = newState;
}
// ================================================================================================
#Events
// ================================================================================================
// =============== Constants
const int vent1Port = 2; // Port number of vent 1
const int vent1Pin = 2; // Pin number of vent 1
const bool vent1Neg = false; // Invert pin of vent 1
const int vent2Port = 2; // Port number of vent 2
const int vent2Pin = 3; // Pin number of vent 2
const bool vent2Neg = false; // Invert pin of vent 2
const int vent3Port = 2; // Port number of vent 3
const int vent3Pin = 4; // Pin number of vent 3
const bool vent3Neg = false; // Invert pin of vent 3
const int vent4Port = 2; // Port number of vent 4
const int vent4Pin = 5; // Pin number of vent 4
const bool vent4Neg = false; // Invert pin of vent 4
const double lim0 = 0.0; // Zone limits
const double lim1 = 1000.0;
const double lim2 = 2000.0;
const double lim3 = 3000.0;
const double lim4 = 4000.0;
const int RunLED = 19; // On when code is running
// =============== Static variables
static int lastState = 0;
// =============== Set/Clr output
void Setoutpin(int port, int pin, bool neg)
{
if (neg)
exec.Clroutpin(port, pin);
else
exec.Setoutpin(port, pin);
}
void Clroutpin(int port, int pin, bool neg)
{
if (neg)
exec.Setoutpin(port, pin);
else
exec.Clroutpin(port, pin);
}