Hi Krittawit,
Here is the macroloop, as Balázs wrote.
There's an error check in it: if you set the RPM higher than 2000 then it will switch to null. Also, if reset is active, the null position will be selected. Change the values of port1, pin1, neg1, port2, pin2, neg2 accroding to your machine.
- Code: Select all
// ================================================================================================
// Automatic Gear change
// ================================================================================================
double RPMOvr = 0.0;
// 0 1 -> Max RPM 2000
// 0 0 -> Max RPM 1000
// 1 0 -> Max RPM 500
// 1 1 -> 0
if (exec.GetLED(ResetLED))
{
if (NeedCheck)
{
SetPin(1);
SetPin(2);
LastRPM = 0.0;
NeedCheck = false;
}
}
else
{
RPMOvr = AS3.Getfielddouble(RPMOvrFld);
if (RPMOvr != LastRPM)
{
LastRPM = RPMOvr;
if (RPMOvr == 0.0)
{
SetPin(1);
SetPin(2);
}
else if (RPMOvr <= 500.0)
{
SetPin(1);
ClrPin(2);
}
else if (RPMOvr <= 1000.0)
{
ClrPin(1);
ClrPin(2);
}
else if (RPMOvr <= 2000.0)
{
ClrPin(1);
SetPin(2);
}
else
{
SetPin(1);
SetPin(2);
exec.AddStatusmessage("RPM too high!");
}
}
NeedCheck = true;
}
// ================================================================================================
#Events
// ================================================================================================
const int ResetLED = 25;
const int RPMOvrFld = 2451;
const int port1 = 2;
const int pin1 = 1;
const bool neg1 = false;
const int port2 = 2;
const int pin2 = 2;
const bool neg2 = false;
static bool NeedCheck = true;
static double LastRPM = 0;
// ================================================================================================
void SetPin(int outno)
{
if (outno == 1)
{
if (neg1)
exec.Clroutpin(port1, pin1);
else
exec.Setoutpin(port1, pin1);
}
else
{
if (neg2)
exec.Clroutpin(port2, pin2);
else
exec.Setoutpin(port2, pin2);
}
}
void ClrPin(int outno)
{
if (outno == 1)
{
if (neg1)
exec.Setoutpin(port1, pin1);
else
exec.Clroutpin(port1, pin1);
}
else
{
if (neg2)
exec.Setoutpin(port2, pin2);
else
exec.Clroutpin(port2, pin2);
}
}
// ================================================================================================