在退出之前执行代码

时间:2015-12-03 16:12:07

标签: arduino

有人知道在退出执行特定代码之前我该怎么办? (仅在离开之前)。 我不能使用else,因为它使代码序列无法正常工作

EDIT:

Currently I have three screens:
1. Home screen
2. flow different 0
3. configurations

The software starts from the home screen;
If the flow != 0 it performs the screen 2;
If flow is back to 0 the display returns to screen 1;
If I'm on the screen 1 and press a button it goes to screen 3;

The problem is that if I execute the function immediately after the while I will never go to the screen 3.

非常感谢。

PS:我正在使用C和Arduino。

while (flow != 0){
  Serial.print("running flow");
  // It does some things...
  // Performs an action only before leaving the while
}
void loop_lcd(){
  loop_flow();
  if (flow != 0){
    update_time();
    set_state(2);        
  }    
  else{     
    switch(state){
      case 1: // SCREEN 1
      Serial.println("screen 1");
        switch(check_button()){
          case (bLEFT):
            Serial.println("LEFT button pressed");
            lcd.clear(); set_state(3);
            break;
          case (bRIGHT):
            Serial.println("RIGHT button pressed");
            lcd.clear(); set_state(3);
            break;          
          default:
            set_state(1);  
        }
        break; 
      case 3: // SCREEN 3
        Serial.println("executando tela 3");
        // ............
        // CODE
        // ............
        }
        break;
    }
  }
}
void set_state(char index) {
  state = index;
  switch(state){ 
    case 1: // STATE 1
      //LINE 1
      lcd.setCursor(2, 1); 
      print_day_and_time();
      break;
    case 2: // STATE 2
      // LINE 0
      lcd.setCursor(2, 0);
      print_day_and_time(); 
      // LINE 1
      lcd.setCursor(0,1);
      print_time();
      // ............
      // CODE
      // ............   
    case 3: // STATE 3
      // screen 3
      // ............
      // CODE
      // ............  
    break;
  }
}

1 个答案:

答案 0 :(得分:1)

不确定我是否正确理解了您的问题,但有一种方法是:

thisOb.parent(...)

但我不这样做,实际上对我来说,你的问题听起来像是在寻找一个没有问题的解决方案。只需这样做:

while (flow != 0){
  Serial.print("running flow");
  // It does some things...
  if (flow == 0) { foo(); }
}

如果你希望在while停止运行后执行while (flow != 0){ Serial.print("running flow"); // It does some things... } foo();