我有一个函数,我想一直运行直到按下按钮(sw2_trig 和 sw3_trig),这使它进入主循环中的一种模式。问题是我在主循环之外尝试了一个while循环和一个do while循环来实现这一点,但它不起作用,它所做的只是循环函数,即使我按下sw2或sw3,我也无法进入模式1或2。这是我的代码。
/*********************************************************************
Intereupt Handlers
********************************************************************/
void sw2_interrupt (void) //adapted from driving_test_2
{
sw2_trig = 1;
}
void sw3_interrupt (void) //adapted from driving_test_2
{
sw3_trig = 1;
}
/*******************************************************************************
Functions
*******************************************************************************/
//I want to keep this function running until sw2_trig or sw3_trig is pressed inside the main loop.
void furElise() {
float notes[] = { e, d, e, d, e, b, D, c, _, a, _, c, e, a, b, _, e, g, b, c, _, e, d, e, d, a, e, a, _, e, e, g, a, e, a};
for (int i = 0; i < sizeof(notes) / sizeof(float); i++) {
if (notes[i] == _) {
spkr = 0.0f;
}
else {
spkr = 0.5f;
spkr.period(1 / notes[i]);
}
wait(0.25f);
}
spkr = 0.0f;
}
*****************************************************************************************
Begin Program
****************************************************************************************/
int main()
{
sw2_trig = 0;
sw3_trig = 0;
sw2_int.mode (PullUp);
sw2_int.fall (&sw2_interrupt);
sw3_int.mode (PullUp);
sw3_int.fall (&sw3_interrupt);
int mode = 0; //Don't show anything
furElise(); // want to keep this running if sw2 or sw3 aren't pressed.
while (1) {
spkr = 0.0;
wait(0.2f); //wait a little
switch (mode) {
case 1:
lm_temp.read();
break();
case 2:
yellowPurple();
break;
if(sw2_trig) {
mode = 1;
sw2_trig = 0;
shld_lcd.cls();
}
if(sw3_trig) {
mode = 2;
sw3_trig = 0;
shld_lcd.cls();
}
}
}