User`s manual

RabbitFLEX User’s Manual www.rabbit.com 77
while (1) {
// Wait for the switch to be released
waitfor(flexDigIn(switch_pressed) == 0);
// Wait additional 200 ms
waitfor(DelayMs(200));
// If the switch is still released, then break out of the while loop
if (flexDigIn(switch_pressed) == 0) {
// Turn the LED back off
flexDigOut(led, 1);
break;
} // end if statement
} // end while loop
} // end if statement
} // end costate
} // end main while loop
Now we will go through each part of the while loop step-by-step.
while (1) {
costate {
switch_pressed = NULL;
while (!switch_pressed) {
Note that we are using a costate in this sample. The costate will allow us to more easily delay for imple-
menting switch debouncing. Also note that we set switch_pressed to NULL. Recall that
switch_pressed is of the type Flex_IOPin*. It will be used to point to a switch (or digital input)
that has been engaged. We initialize this to NULL, and will remain in the loop until a switch has been
pressed.
if (flexDigIn(&flex_digin31)) {
switch_pressed = &flex_digin31;
led = &flex_digout32;
switchnum = 1;
}
We have already seen that flexDigOut() manipulates digital outputs. Therefore, it is not surprising
that flexDigIn() manipulates digital inputs. In this case, flex_digin31 indicates the digital input
that the program is reading. Always remember that you must pass a pointer to the RabbitFLEX I/O struc-
tures, and not the structure itself.
If flexDigIn() indicates that flex_digin31 has been pressed, then we save off the switch that has
been pressed, the LED that we should turn on, and the number of the switch that has been pressed.
else if (flexDigIn(&flex_digin33)) {
switch_pressed = &flex_digin33;
led = &flex_digout34;
switchnum = 2;
}