-1

Here is an example of my code i can place if condition to check While condition flag and put break after each line of the body but it increase overhead. WHAT SHOULD I DO??????

  while(!((kvp < (Motor_pos+2)) && (kvp > (Motor_pos-2))) ){
               
                if ( (kvp > (Motor_pos+2))){
//                    Relay_75  = LOW;
                    while (kvp > (Motor_pos+2)) {
                        VAR_CW  = HIGH;    // M1 - Red LED
                        VAR_CCW = LOW;    // M2 - Orange LED
                        for(uint8_t i=0; i < 5 ; i++){
                            ADC_s = ADC_s + Read_VAR();
                        }
                        Motor_pos = ADC_sum/5;
                        ADC_sum =0;
//                        UART_Write( Motor_pos);
                        __delay_ms(50);
                    }
                    break;
                }
                else if ((kvp < (Motor_pos-2))){
//                    Relay_100  = LOW;
                    while (kvp < (Motor_pos-2)) {
                        VAR_CW = LOW;   // M1 - Red LED
                        VAR_CCW = HIGH;  // M2 - Orange LED
                        for(uint8_t i=0; i < 5 ; i++){
                            ADC_s = ADC_s + Read_VAR();
                        }
                        Motor_pos = ADC_s/5;
                        ADC_s =0;
//                        UART_Write( Motor_pos);
                        __delay_ms(50);
                    }
                    break;
                }
                else break;
            }
            Motor_pos = 0;
            ADC_s = 0;
            VAR_CW = LOW;   // M1 - Red LED
            VAR_CCW = LOW;  // M2 - Orange LED
  • This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the *real* problem that you are trying to solve which makes you think you need to have an `if` condition after every line of code? Provide a higher level description of the requirements. – kaylum Oct 04 '21 at 22:05
  • The Condition of While is start point to starting motor procedure. if this flag cleared the procedure have to terminate immediately and shut down the procedure. So after each line I have to check if this flag is Ture or NOT. – Atef El Naggar Oct 04 '21 at 22:25
  • @Yunnosch. Sorry it's WHAT not WHY – Atef El Naggar Oct 04 '21 at 22:27
  • Why do you need those inner `while` loops that have the same condition as their corresponding `if` checks? I suspect that just having everything handled directly in the `if` blocks would already be good enough – UnholySheep Oct 04 '21 at 22:30
  • Okay let me make it more clear. I receive a Flag from UART to drive motor to a certain position So i built this function also I can receive termination flag from UART to STOP this operation. So i need something could stop the code inside the while or even If condition. hence that the Body take more than 2 sec so i can receive a message within this time. – Atef El Naggar Oct 05 '21 at 14:45
  • So HOW could i check the condition of "If" or " While" while executing their bodies ? – Atef El Naggar Oct 05 '21 at 14:46
  • To break; the Loop – Atef El Naggar Oct 05 '21 at 14:47

1 Answers1

0

Your logic has several problems, and it is in no way obvious what the intent of your code is. This is a reduction in the detail of your logic:

while (a || b) {
   if (a) {
      while (a) {
          do something a;
      }
      break;
   } else if (b) {
      while (b) {
          do something b;
      }
      break;
   } else {
      break;
   }
}

devolves into:

    if (a) {
        while (a) {
           do something a;
        }
    } else if (b) {
        while (b) {
           do something b;
        }
    }

So you really need to concentrate first on your simplifying steps, then the other.

mevets
  • 10,070
  • 1
  • 21
  • 33
  • this is good answer, but the feedback from the motor overshoots sometimes so i need to check if it within this range or exceed the other – Atef El Naggar Oct 05 '21 at 15:04
  • 1
    If *kvp* is ever with +- 2 of *Motor_Pos*, how will that condition ever change? It seems you only sample ADC_s when it is outside of that range? Also, if overhead is your problem, why are you delaying for 50ms? Shouldn't you have this code run on a 50ms event instead? Relative delays are almost always wrong in reactive systems. – mevets Oct 05 '21 at 21:38