Thread: In range function check arduino

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    13

    In range function check arduino

    Here is my code written on Arduino.I wanted to load OL_Time value if
    R,Y, current range of

    Here is my code written on Arduino.I wanted to load OL_Time value if
    R,Y, current range of



    Code:
    
    
    
    unsigned int OL_Point = 10;
    unsigned int percentage2C_Limit = 20;
    unsigned int percentage5C_Limit = 50;
    unsigned int percentage10C_Limit = 100;
    unsigned char OL_Curve = 1;
    unsigned int OverLoad_Flag = 0;
    unsigned char Pgm_mode = 0;
    unsigned int OCRT_Dly_Cntr = 0;
    unsigned int OL_Time = 30;
    unsigned int OLTR_Time = 0;
    unsigned char Milli10_Sec_Cntr_X = 0, Milli100_Sec_Cntr_X = 0, Milli1000_Sec_Cntr_X = 0, Power_on_Valid_Cntr = 0;
    unsigned char Cntrl_flag = 1;
    unsigned int Rph_Current = 16;
    unsigned int Yph_Current = 16;
    unsigned int Bph_Current = 16;
    unsigned int Percenatge_Calculated = 0;
    unsigned int Counter_OCRT = 0;
    unsigned char Rph_OLFlag = 0;
    unsigned char Bph_OLFlag = 0;
    unsigned char Yph_OLFlag = 0;
    float Rph_Current_Float = 0.0;
    float Yph_Current_Float = 0.0;
    float Bph_Current_Float = 0.0;
    
    
    float OL_Point_Float = 0.0;
    
    
    float Per1_Cal = 0, Per5_Cal = 0, Per10_Cal = 0, Per15_Cal = 0, Per20_Cal = 0;
    float Per30_Cal = 0, Per40_Cal = 0, Per50_Cal = 0, Per60_Cal = 0, Per70_Cal = 0, Per80_Cal = 0, Per90_Cal = 0, Per100_Cal = 0;
    
    
    
    
    void setup() {
      // put your setup code here, to run once:
    
    
      Serial.begin(9600);
    
    
    }
    
    
    void loop()
    
    
    {
    
    
      if (((Rph_Current > OL_Point) || (Yph_Current > OL_Point) || (Bph_Current > OL_Point))) {
        if (OL_Curve == 1) {
          Percenatge_Calculated = ((int)(OL_Point * percentage2C_Limit) + OL_Point);
          Rph_Current_Float = Rph_Current * 1.0;
          Yph_Current_Float = Yph_Current * 1.0;
          Bph_Current_Float = Bph_Current * 1.0;
          OL_Point_Float = (double) OL_Point / 1.0;
    
    
          Per1_Cal = (OL_Point_Float + ((OL_Point_Float * 1) / 100));
          Per5_Cal = (OL_Point_Float + ((OL_Point_Float * 5) / 100));
          Per10_Cal = (OL_Point_Float + ((OL_Point_Float * 10) / 100));
          Per15_Cal = (OL_Point_Float + ((OL_Point_Float * 15) / 100));
          Per20_Cal = (OL_Point_Float + ((OL_Point_Float * 20) / 100));
    
    
          // Percenatge_Calculated= OL_Point+((OL_Point*1)/100;
    
    
          Counter_OCRT++;
        /*  if (Counter_OCRT >= 5) {
    
    
                if ((Rph_Current_Float > Per1_Cal) && (Per5_Cal < Rph_Current_Float) || (Yph_Current_Float > Per1_Cal) && (Per5_Cal < Yph_Current_Float) || (Bph_Current_Float > Per1_Cal) && (Per5_Cal < Bph_Current_Float))
            {
              OL_Time = 20;
            } else if ((Rph_Current_Float > Per5_Cal) && (Per10_Cal < Rph_Current_Float) || (Yph_Current_Float > Per5_Cal) && (Per10_Cal < Yph_Current_Float) || (Bph_Current_Float > Per5_Cal) && (Per10_Cal < Bph_Current_Float))
            {
              OL_Time = 15;
            } else
    
    
              if ((Rph_Current_Float > Per10_Cal) && (Per15_Cal < Rph_Current_Float) || (Yph_Current_Float > Per10_Cal) && (Per15_Cal < Yph_Current_Float) || (Bph_Current_Float > Per10_Cal) && (Per15_Cal < Bph_Current_Float))
              {
                OL_Time = 10;
              } else if ((Rph_Current_Float > Per15_Cal) && (Per20_Cal < Rph_Current_Float) || (Yph_Current_Float > Per15_Cal) && (Per20_Cal < Yph_Current_Float) || (Bph_Current_Float > Per15_Cal) && (Per20_Cal < Bph_Current_Float))
              {
                OL_Time = 5;
              }
            Counter_OCRT = 0;
    
    
          }*/
    
    
    
    
          function();
    
    
        }
    
    
      } else {
    
    
        // Counter_OCRT = 0;
        OverLoad_Flag = 0;
        Rph_OLFlag = 0;
        Bph_OLFlag = 0;
        Yph_OLFlag = 0;
      }
      delay(1000);
    
    
      Serial.print("RPH:"); Serial.print(Rph_Current); Serial.print("\tYPH:"); Serial.print(Yph_Current); Serial.print("\tBPH:"); Serial.print(Bph_Current);
      Serial.print("\t OL_Point_Float:"); Serial.println(OL_Point_Float);
      Serial.print("RPH-float:"); Serial.print(Rph_Current_Float); Serial.print("\tYPH-float:"); Serial.print(Yph_Current_Float); Serial.print("\tBPH-float:"); Serial.println(Bph_Current_Float);
      Serial.print("Per1_Cal:"); Serial.print(Per1_Cal); Serial.print("\tPer5_Cal:"); Serial.print(Per5_Cal); Serial.print("\tPer10_Cal :"); Serial.print(Per10_Cal );
      Serial.print("\tPer15_Cal:"); Serial.print(Per15_Cal); Serial.print("\tPer20_Cal:"); Serial.println(Per20_Cal);
      Serial.print("OL_Time:"); Serial.println(OL_Time);
      Serial.println("...................................................................................");
      delay(1000);
    }
    
    
    
    
    
    
    void function()
    {
    
    
    
    
    
    
    if (Counter_OCRT >= 5) {
          // OverLoad_Flag = 1;
          Counter_OCRT = 0;
          
          if ((Rph_Current_Float >= Per1_Cal) && (Per5_Cal <= Rph_Current_Float) || (Yph_Current_Float >= Per1_Cal) && (Per5_Cal <= Yph_Current_Float) || (Bph_Current_Float >= Per1_Cal) && (Per5_Cal <= Bph_Current_Float)) {
            OL_Time = 20;
            OverLoad_Flag = 1;
          } else if ((Rph_Current_Float >= Per5_Cal) && (Per10_Cal <= Rph_Current_Float) || (Yph_Current_Float >= Per5_Cal) && (Per10_Cal <= Yph_Current_Float) || (Bph_Current_Float >= Per5_Cal) && (Per10_Cal <= Bph_Current_Float)) {
            OL_Time = 15;
            OverLoad_Flag = 1;
          } else
    
    
          if ((Rph_Current_Float >= Per10_Cal) && (Per15_Cal <= Rph_Current_Float) || (Yph_Current_Float >= Per10_Cal) && (Per15_Cal <= Yph_Current_Float) || (Bph_Current_Float >= Per10_Cal) && (Per15_Cal <= Bph_Current_Float)) {
            OL_Time = 10;
            OverLoad_Flag = 1;
          } else if (( Rph_Current_Float >= Per20_Cal) || ( Yph_Current_Float >= Per20_Cal) &&( Bph_Current_Float >= Per20_Cal)) {
            OL_Time = 5;
            OverLoad_Flag = 1;
          }
    
    
        }
    
    
    }
    Attached Images Attached Images In range function check arduino-serial-jpg 
    Last edited by ajitnayak87; 01-25-2019 at 12:53 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,126
    What is your question?
    And if you are going to post a screenshot, make it a png or gif, not a jpg.
    But if

    What is your question?
    And if you are going to post a screenshot, make it a png or gif, not a jpg.
    But if
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    13
    Quote Originally Posted by john.c View Post
    What is your question?
    And if you are going to post a screenshot, make it a png or gif, not a jpg.
    But if

    What is your question?
    And if you are going to post a screenshot, make it a png or gif, not a jpg.
    But if
    I am looking for condition statement where i could load differnt time value

    if(perc1>RYBcurrent OL_Time=20
    else
    if(perc5>RYBcurrent OL_Time=15
    else
    if(perc15>RYBcurrent OL_Time=10
    else
    if(perc20>RYBcurrent)
    OL_Time=5

    In above condition i could not able to achieve this

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,657
    Step 1 would be to format your code for readability.
    Code:
    unsigned int OL_Point = 10;
    unsigned int percentage2C_Limit = 20;
    unsigned int percentage5C_Limit = 50;
    unsigned int percentage10C_Limit = 100;
    unsigned char OL_Curve = 1;
    unsigned int OverLoad_Flag = 0;
    unsigned char Pgm_mode = 0;
    unsigned int OCRT_Dly_Cntr = 0;
    unsigned int OL_Time = 30;
    unsigned int OLTR_Time = 0;
    unsigned char Milli10_Sec_Cntr_X = 0, 
                  Milli100_Sec_Cntr_X = 0, 
                  Milli1000_Sec_Cntr_X = 0, 
                  Power_on_Valid_Cntr = 0;
    unsigned char Cntrl_flag = 1;
    unsigned int Rph_Current = 16;
    unsigned int Yph_Current = 16;
    unsigned int Bph_Current = 16;
    unsigned int Percenatge_Calculated = 0;
    unsigned int Counter_OCRT = 0;
    unsigned char Rph_OLFlag = 0;
    unsigned char Bph_OLFlag = 0;
    unsigned char Yph_OLFlag = 0;
    float Rph_Current_Float = 0.0;
    float Yph_Current_Float = 0.0;
    float Bph_Current_Float = 0.0;
    float OL_Point_Float = 0.0;
    float Per1_Cal = 0, Per5_Cal = 0, Per10_Cal = 0, Per15_Cal = 0, Per20_Cal = 0;
    float Per30_Cal = 0, Per40_Cal = 0, Per50_Cal = 0, Per60_Cal = 0;
    float Per70_Cal = 0, Per80_Cal = 0, Per90_Cal = 0, Per100_Cal = 0;
    
    void setup()
    {
      // put your setup code here, to run once:
      Serial.begin(9600);
    }
    
    void loop()
    {
      if (((Rph_Current > OL_Point) || 
           (Yph_Current > OL_Point) || 
           (Bph_Current > OL_Point))) {
        if (OL_Curve == 1) {
          Percenatge_Calculated = ((int) (OL_Point * percentage2C_Limit) + OL_Point);
          Rph_Current_Float = Rph_Current * 1.0;
          Yph_Current_Float = Yph_Current * 1.0;
          Bph_Current_Float = Bph_Current * 1.0;
          OL_Point_Float = (double) OL_Point / 1.0;
    
          Per1_Cal = (OL_Point_Float + ((OL_Point_Float * 1) / 100));
          Per5_Cal = (OL_Point_Float + ((OL_Point_Float * 5) / 100));
          Per10_Cal = (OL_Point_Float + ((OL_Point_Float * 10) / 100));
          Per15_Cal = (OL_Point_Float + ((OL_Point_Float * 15) / 100));
          Per20_Cal = (OL_Point_Float + ((OL_Point_Float * 20) / 100));
    
          // Percenatge_Calculated= OL_Point+((OL_Point*1)/100;
          Counter_OCRT++;
          /*  if (Counter_OCRT >= 5) {
             if ((Rph_Current_Float > Per1_Cal) && (Per5_Cal < Rph_Current_Float) || (Yph_Current_Float > Per1_Cal) && (Per5_Cal < Yph_Current_Float) || (Bph_Current_Float > Per1_Cal) && (Per5_Cal < Bph_Current_Float))
             {
             OL_Time = 20;
             } else if ((Rph_Current_Float > Per5_Cal) && (Per10_Cal < Rph_Current_Float) || (Yph_Current_Float > Per5_Cal) && (Per10_Cal < Yph_Current_Float) || (Bph_Current_Float > Per5_Cal) && (Per10_Cal < Bph_Current_Float))
             {
             OL_Time = 15;
             } else
             if ((Rph_Current_Float > Per10_Cal) && (Per15_Cal < Rph_Current_Float) || (Yph_Current_Float > Per10_Cal) && (Per15_Cal < Yph_Current_Float) || (Bph_Current_Float > Per10_Cal) && (Per15_Cal < Bph_Current_Float))
             {
             OL_Time = 10;
             } else if ((Rph_Current_Float > Per15_Cal) && (Per20_Cal < Rph_Current_Float) || (Yph_Current_Float > Per15_Cal) && (Per20_Cal < Yph_Current_Float) || (Bph_Current_Float > Per15_Cal) && (Per20_Cal < Bph_Current_Float))
             {
             OL_Time = 5;
             }
             Counter_OCRT = 0;
             } */
          function();
        }
      } else {
        // Counter_OCRT = 0;
        OverLoad_Flag = 0;
        Rph_OLFlag = 0;
        Bph_OLFlag = 0;
        Yph_OLFlag = 0;
      }
      delay(1000);
    
      Serial.print("RPH:");
      Serial.print(Rph_Current);
      Serial.print("\tYPH:");
      Serial.print(Yph_Current);
      Serial.print("\tBPH:");
      Serial.print(Bph_Current);
      Serial.print("\t OL_Point_Float:");
      Serial.println(OL_Point_Float);
      Serial.print("RPH-float:");
      Serial.print(Rph_Current_Float);
      Serial.print("\tYPH-float:");
      Serial.print(Yph_Current_Float);
      Serial.print("\tBPH-float:");
      Serial.println(Bph_Current_Float);
      Serial.print("Per1_Cal:");
      Serial.print(Per1_Cal);
      Serial.print("\tPer5_Cal:");
      Serial.print(Per5_Cal);
      Serial.print("\tPer10_Cal :");
      Serial.print(Per10_Cal);
      Serial.print("\tPer15_Cal:");
      Serial.print(Per15_Cal);
      Serial.print("\tPer20_Cal:");
      Serial.println(Per20_Cal);
      Serial.print("OL_Time:");
      Serial.println(OL_Time);
      Serial.println("...............................");
      delay(1000);
    }
    
    void function()
    {
      if (Counter_OCRT >= 5) {
        // OverLoad_Flag = 1;
        Counter_OCRT = 0;
    
        if ((Rph_Current_Float >= Per1_Cal) && (Per5_Cal <= Rph_Current_Float)
            || (Yph_Current_Float >= Per1_Cal) && (Per5_Cal <= Yph_Current_Float)
            || (Bph_Current_Float >= Per1_Cal) && (Per5_Cal <= Bph_Current_Float)) {
          OL_Time = 20;
          OverLoad_Flag = 1;
        } else if ((Rph_Current_Float >= Per5_Cal)
                   && (Per10_Cal <= Rph_Current_Float)
                   || (Yph_Current_Float >= Per5_Cal)
                   && (Per10_Cal <= Yph_Current_Float)
                   || (Bph_Current_Float >= Per5_Cal)
                   && (Per10_Cal <= Bph_Current_Float)) {
          OL_Time = 15;
          OverLoad_Flag = 1;
        } else if ((Rph_Current_Float >= Per10_Cal) && (Per15_Cal <= Rph_Current_Float)
               || (Yph_Current_Float >= Per10_Cal)
               && (Per15_Cal <= Yph_Current_Float)
               || (Bph_Current_Float >= Per10_Cal)
               && (Per15_Cal <= Bph_Current_Float)) {
          OL_Time = 10;
          OverLoad_Flag = 1;
        } else if ((Rph_Current_Float >= Per20_Cal)
                   || (Yph_Current_Float >= Per20_Cal)
                   && (Bph_Current_Float >= Per20_Cal)) {
          OL_Time = 5;
          OverLoad_Flag = 1;
        }
      }
    }
    Step 2, remove all those unused variables (like Pgm_mode, Cntrl_flag) and anything else which clutters the program.

    Step 3, All the Rph,Yph,Bph variables need to be placed into a structure, and then 3 instances of that struct created.
    Eg.
    Code:
    struct readings {
        unsigned int Current;
        unsigned char OLFlag;
        float Current_Float;  //!! how does this differ from Current?
    };
    // Now create three instances
    // So you can say things like Rph.OLFlag = 0; 
    struct readings Rph, Yph, Bph;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. son() function output range in ISO/IEC 9899:1999(E)?
    By Vanhapolle in forum C Programming
    Replies: 10
    Last Post: 12-08-2013, 07:32 PM
  2. C and Arduino
    By bamwels in forum C Programming
    Replies: 11
    Last Post: 08-21-2013, 09:20 PM
  3. Check entered address range is valid
    By mrapoc in forum C Programming
    Replies: 4
    Last Post: 08-04-2012, 07:10 AM
  4. Check Values within a range
    By ianrice in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2010, 08:50 AM
  5. Check if a value is in a particular range.
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-15-2002, 04:06 AM

Tags for this Thread