Reputation: 1283
I have a function, which I want to keep running until a button is pressed (sw2_trig and sw3_trig), which makes it enter one of the modes in the main loop. The problem is that I tried a while loop and a do while loop outside the main loop to achieve this but it is not working, all it does is loop the function and I cannot enter mode 1 or 2 even if I press sw2 or sw3. here is my code.
/*********************************************************************
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();
}
}
}
Upvotes: 1
Views: 1473
Reputation: 1147
You can keep Polling
inside furElise()
function until the sw2_trig
and sw3_trig
trigger not equal to 1. Once it is equal to 1, you can break the loop.
Dummy Code
if(sw2_trig == 1 || sw3_trig == 1)
{
return;
}
So when the interrupt comes, either of these values become 1 in interrupt handler.
Polling is not the best technique, but this is one way you can solve your issue.
Hope this helps.
Upvotes: 5