9 years, 10 months ago.

Return in the main !

Hello, I would like, once in my void function, to return back to my main () how? Voic my program Thank you

int main() {
 mkdir("/sd/louise",0777);
 fp=fopen("/sd/louise/DAC.txt","r");
            if (fp==NULL){
                lcd.printf("not file non\n\n");
            }
            led1=!led1;
            lcd.printf("Lecture en cour \n\n");
            pc.attach(&interruption);
}


void interruption() {
  int keyIn = pc.getc();  // find out what key was pressed.
  if (keyIn==' ') {           // if _ for stop
    flipper.detach();
    main.attach;
    }
  else {
    flipper.attach(&flip,intervalle2); // setup flipper to call flip after x seconds 
    }
}

1 Answer

9 years, 10 months ago.

As soon as an interrupt routine has finished control automatically returns to wherever it was before the interrupt happened. In your case main() had reached the end and exited so there is nowhere for it to return to. If you want control to return to main then you need to keep main running in a loop.

You probably want something like this:

volatile bool flipperRunning = false; // volatile so that main will see changes that happen in interrupts

FILE *fp;
SDFileSystem sd([some pins]);

int main() {
 mkdir("/sd/louise",0777);
 fp=fopen("/sd/louise/DAC.txt","r");
 if (fp==NULL){
    lcd.printf("not file non\n\n");
 }

 led1=!led1;
 lcd.printf("Lecture en cour \n\n");
 pc.attach(&interruption);
 while (true) {
   if (flipperRunning) {
      // do this if running
   } else {
     // do this if not running
  }
 }

 // any code here will only be reached if you have a break; command in the above while loop. 

}
 

void interruption() {
  int keyIn = pc.getc();  // find out what key was pressed.
  if (keyIn==' ') {           // if _ for stop
    flipper.detach();
    flipperRunning = false;
  } else { // not the stop key. Start again.
    if (!flipperRunning) { // only attach to flipper if it's not already running.
      flipper.attach(&flip,intervalle2); // setup flipper to call flip after x seconds  
      flipperRunning = true;
    }
  }
}

Accepted Answer

In addition to this answer: if you want to quite the void function halfway (for example when an if statement is met), you can simply use return;

posted by Erik - 06 Jun 2014