Page 126 -
P. 126
243_MasterPieces_02b.qxd 4/18/03 6:59 PM Page 98
98 Masterpiece 2 • The Learning Brick Sorter
The remaining part of the program is rather trivial. Four simple routines provide con-
trol of the switch valves, operating the corresponding motors for a very short time
defined by the constant SWITCH_TIME:
#define SWITCH_TIME 20
void OpenHand()
{
OnFwd(OUT_C);
Wait(SWITCH_TIME);
Off(OUT_C);
}
void CloseHand()
{
OnRev(OUT_C);
Wait(SWITCH_TIME);
Off(OUT_C);
}
void LiftArm()
{
OnFwd(OUT_B);
Wait(SWITCH_TIME);
Off(OUT_B);
}
void LowerArm()
{
OnRev(OUT_B);
Wait(SWITCH_TIME);
Off(OUT_B);
}
Controlling the rotation of the turntable requires a bit more code.The global variable
pos maintain the information about which position the arm is currently located.
int pos; // current arm position: -1=pick up station, 0=bin0, 1=bin1...
The routine Move() rotates the arm until the required destination is reached, at the
same time updating the variable pos with the new position. Each step toward a new posi-
tion is counted waiting for the touch sensor to be opened and then closed again:
void Move(int dest)
{
int dir;
// if arm is already at proper position, exit

