Hi Jeff,
A general way to handle multiple events using a single Thread is to use a State Machine, See the untested example below:
Code:
#include "KMotionDef.h"
// Press Brake control
enum { IDLE, JOG_DOWN, DELAY, HOME }; // states
#define BIT_PEDAL 1033
#define BIT_NEG_LIMIT 1035
#define BIT_TOP_LIMIT 1034
#define BIT_AUTO 46
#define ZAXIS 0
#define DWELL 2.0 // delay at bottom seconds
void main()
{
int state=IDLE;
double DelayTime;
for (;;)
{
switch (state)
{
case IDLE:
if (ReadBit(BIT_PEDAL))
{
Jog(ZAXIS, -100); // Jog down
state = JOG_DOWN;
}
break;
case JOG_DOWN:
// stop Jogging down if pedal released and not Auto mode
if (!ReadBit(BIT_PEDAL) && !ReadBit(BIT_AUTO))
{
Jog(ZAXIS, 0); // Stop
state = IDLE;
}
else
{
// hit negative limit?
if (ReadBit(BIT_NEG_LIMIT))
{
Jog(ZAXIS, 0); // Stop
DelayTime = Time_sec() + DWELL;
state = DELAY;
}
}
break;
case DELAY:
if (Time_sec() > DelayTime) // Dwell finished?
{
Jog(ZAXIS, 400); // Begin Homing
state = HOME;
}
break;
case HOME:
if (ReadBit(BIT_TOP_LIMIT)) // Homed?
{
Jog(ZAXIS, 0); // Stop
state = IDLE;
}
break;
}
}
}