User`s manual

76 www.rabbit.com Applications Programming
The rest of this section will detail the main changes that have been made to this program. In fact, you
should delete the entire main while loop in speaker_tone.c, since we will be replacing it. For conve-
nience, here is the complete listing for the new while loop.
while (1) {
costate {
switch_pressed = NULL;
while (!switch_pressed) {
// Check for a switch pressed. When we detect one, then save off the pin structure
// for the switch pressed, the pin structure for the corresponding LED, and the number
// of the switch pressed.
if (flexDigIn(&flex_digin31)) {
switch_pressed = &flex_digin31;
led = &flex_digout32;
switchnum = 1;
}
else if (flexDigIn(&flex_digin33)) {
switch_pressed = &flex_digin33;
led = &flex_digout34;
switchnum = 2;
}
else if (flexDigIn(&flex_digin35)) {
switch_pressed = &flex_digin35;
led = &flex_digout36;
switchnum = 3;
}
else if (flexDigIn(&flex_digin37)) {
switch_pressed = &flex_digin37;
led = &flex_digout38;
switchnum = 4;
}
}
// Wait 50 ms to make sure the switch stays pressed (debouncing)
waitfor(DelayMs(50));
// Check if the switch is still pressed
if (flexDigIn(switch_pressed) == 1) {
// Light the corresponding output LED
flexDigOut(led, 0);
// Play the tone
flexToneLoad(tones[switchnum], tone_len[switchnum],
REPEAT_TONE);