Percentage of variable Displaying

Dear all.

I have variable . I wanted to display its percentage value using this formula

How should i get value in percentage ?

what it wil do percentage1_value declared as float and compare with unsigned int Overload value??

I dont want to use float value for percentage1_value

Overload=30
percentage1_value=30.3A
percentage5_value=31.5A
percentage10_value=33A

unsigned  int  Overload=30;
unsigned int percentage1_value,percentage2_value;percentage2_value;

percentage1_value=Overload+((Overload*1)/100);
percentage5_value=Overload+((Overload*5)/100);
percentage10_value=Overload+((Overload*10)/100);

if(Overload>percentage1_value && Overload< percentage5_value)
  {
    TRIP TIME=30; Relay should trip after 30S
  }else

  { TRIP TIME=1;; Relay should trip after 1S

   }

You want to display the percentage of 30 out of 30,3, 31,5 and 33? Of what do you want? Some example would be nice.

My concept was if overload value within limit of 1% to 5% load the time value with default value.

I dont want to display percentage1_value. i am using it only for comparison.

But My overload value is declared as unsigned int

What does it do if percentage1_value declared as float and compare with unsigned int value.

Does the overload value change or is it a constant? Are the limits adjusted when the overload value gets updated or are they fixed? I am not entirely sure, but the way your code looks right now overload will never be greater than percentage1_value. If overload is lower than 100, percentage1_value will equal overload and in all other cases it should be greater than overload.

If you compare an integer with a float, the integer should be converted to float before comparison by the compiler. If you don't want to use floats maybe take a look at fixed point numbers? Or you could multiply your overload value by 100 (assuming you adjust its type) and omit the division by 100 in your percentage computation (note: you also need to use the result of the multiplication for the computation of the percentage).

i am pasting part of code i am testing

Yes overload value is constant

I have Rph,yph,bph current coming . which declared as unsigned int. I am getting Rph ,yph,bph properly in integer format.

if any of phase current cross Overload_limit(OL_point) which is constant. check condition

weather (Rph,yph,bph)Current > 1% of OL_point value and (Rph,yph,bph)Current < 5%
Then only load timer value

In below code (Rph,yph,bph)Current not compared
How unsigned value compared with float

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 = 0;
unsigned int Yph_Current = 0;
unsigned int Bph_Current = 0;
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 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;

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);

    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 ((Per1_Cal >= OL_Point_Float) && (OL_Point_Float <= Per5_Cal)) {
        OL_Time = 20;
      } else

      if ((Per5_Cal >= OL_Point_Float) && (OL_Point_Float <= Per10_Cal)) {
        OL_Time = 15;
      } else

      if ((Per10_Cal >= OL_Point_Float) && (OL_Point_Float <= Per15_Cal)) {
        OL_Time = 10;
      } else

      if ((Per15_Cal >= OL_Point_Float) && (OL_Point_Float <= Per20_Cal)) {
        OL_Time = 5;
      }

      Counter_OCRT = 0;
      Hold = 0;
      Hold_control = 0;

    }

  }

} else {

  Counter_OCRT = 0;
  OverLoad_Flag = 0;
  Rph_OLFlag = 0;
  Bph_OLFlag = 0;
  Yph_OLFlag = 0;
}

A quick note on your code: It is horribly formated. Please use the ide's auto format tool (press ctrl+T) before posting. Also this does not compile, a minimal working example is always helpful.

So if I understand you correctly you have three currents as an unsigned integer and want to test, if they are in range of 1% and 5% of a fixed overload value?

If so, write yourself a function that checks if the value is within a certain range, this will make thinks a lot more cleaner. Then, in your main code you call that function for each current and check if it returns true. Since you are testing against constant values, why not remove all the calculation code and replace it with a comparison against those constants?

I have made below changes . Is it correct

float OL_Point_Float = 0.0;
float Rph_Current_Float = 0.0;
float Yph_Current_Float = 0.0;
float Bph_Current_Float = 0.0;

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);

    OL_Point_Float = (double)OL_Point * 1.0;
    Rph_Current_Float = Rph_Current * 1.0;
    Yph_Current_Float = Yph_Current * 1.0;
    Bph_Current_Float = Bph_Current * 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) && (Rph_Current_Float < Per5_Cal) || (Yph_Current_Float > Per1_Cal) && (Yph_Current_Float < Per5_Cal) || (Bph_Current_Float > Per1_Cal) && (Bph_Current_Float < Per5_Cal))
      {
        OL_Time = 20;
      } else if ((Rph_Current_Float > Per5_Cal) && (Rph_Current_Float < Per10_Cal) || (Yph_Current_Float > Per5_Cal) && (Yph_Current_Float < Per10_Cal) || (Bph_Current_Float > Per5_Cal) && (Bph_Current_Float < Per10_Cal))
      {
        OL_Time = 15;
      } else

        if ((Rph_Current_Float > Per10_Cal) && (Rph_Current_Float < Per15_Cal) || (Yph_Current_Float > Per10_Cal) && (Yph_Current_Float < Per15_Cal) || (Bph_Current_Float > Per10_Cal) && (Bph_Current_Float < Per15_Cal))
        {
          OL_Time = 10;
        } else if ((Rph_Current_Float > Per15_Cal) && (Rph_Current_Float < Per20_Cal) || (Yph_Current_Float > Per15_Cal) && (Yph_Current_Float < Per20_Cal) || (Bph_Current_Float > Per15_Cal) && (Bph_Current_Float < Per20_Cal))
        {
          OL_Time = 5;
        } else


          /*if((Per1_Cal>=OL_Point_Float)&&(OL_Point_Float<=Per5_Cal))
            {
            OL_Time=20;
            }else

            if((Per5_Cal>=OL_Point_Float)&&(OL_Point_Float<=Per10_Cal))
            {
            OL_Time=15;
            }
            else

            if((Per10_Cal>=OL_Point_Float)&&(OL_Point_Float<=Per15_Cal))
            {
            OL_Time=10;
            }
            else

            if((Per15_Cal>=OL_Point_Float)&&(OL_Point_Float<=Per20_Cal))
            {
            OL_Time=5;
            }*/

          Counter_OCRT = 0;
      Hold = 0;
      Hold_control = 0;

    }



  }

}



else
{


  Counter_OCRT = 0;
  OverLoad_Flag = 0;
  Rph_OLFlag = 0;
  Bph_OLFlag = 0;
  Yph_OLFlag = 0;
}

AJITnayak:
I have made below changes . Is it correct[?]

You didn't format your code (do it and edit your posts!). Also I don't see any significant changes in your code, apart from you multiplying something with 1.0. I don't know if its correct since I can't test it, but I'm sure you can.

This code i have done . with this code it will always write time=30s default value.

In my actual code Rph_current,Yph_current,Bph_current are change based on sensor value.

to simpilfy my code i am pasting part of it.

Below syntax its changing OL_time value to20 from 30

if ((Per1_Cal >= OL_Point_Float) && (OL_Point_Float <= Per5_Cal)) {
          OL_Time = 20;
          } else

          if ((Per5_Cal >= OL_Point_Float) && (OL_Point_Float <= Per10_Cal)) {
            OL_Time = 15;
          } else

            if ((Per10_Cal >= OL_Point_Float) && (OL_Point_Float <= Per15_Cal)) {
              OL_Time = 10;
            } else

              if ((Per15_Cal >= OL_Point_Float) && (OL_Point_Float <= Per20_Cal)) {
                OL_Time = 5;
              }

Here it wont change

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 = 12;
unsigned int Yph_Current = 15;
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 ((Per1_Cal >= OL_Point_Float) && (OL_Point_Float <= Per5_Cal)) {
          OL_Time = 20;
          } else

          if ((Per5_Cal >= OL_Point_Float) && (OL_Point_Float <= Per10_Cal)) {
            OL_Time = 15;
          } else

            if ((Per10_Cal >= OL_Point_Float) && (OL_Point_Float <= Per15_Cal)) {
              OL_Time = 10;
            } else

              if ((Per15_Cal >= OL_Point_Float) && (OL_Point_Float <= Per20_Cal)) {
                OL_Time = 5;
              }*/

        if ((Rph_Current_Float > Per1_Cal) && (Rph_Current_Float < Per5_Cal) || (Yph_Current_Float > Per1_Cal) && (Yph_Current_Float < Per5_Cal) || (Bph_Current_Float > Per1_Cal) && (Bph_Current_Float < Per5_Cal))
        {
          OL_Time = 20;
        } else if ((Rph_Current_Float > Per5_Cal) && (Rph_Current_Float < Per10_Cal) || (Yph_Current_Float > Per5_Cal) && (Yph_Current_Float < Per10_Cal) || (Bph_Current_Float > Per5_Cal) && (Bph_Current_Float < Per10_Cal))
        {
          OL_Time = 15;
        } else

          if ((Rph_Current_Float > Per10_Cal) && (Rph_Current_Float < Per15_Cal) || (Yph_Current_Float > Per10_Cal) && (Yph_Current_Float < Per15_Cal) || (Bph_Current_Float > Per10_Cal) && (Bph_Current_Float < Per15_Cal))
          {
            OL_Time = 10;
          } else if ((Rph_Current_Float > Per15_Cal) && (Rph_Current_Float < Per20_Cal) || (Yph_Current_Float > Per15_Cal) && (Yph_Current_Float < Per20_Cal) || (Bph_Current_Float > Per15_Cal) && (Bph_Current_Float < Per20_Cal))
          {
            OL_Time = 5;
          }



        Counter_OCRT = 0;

      }

    }

  } 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);
}

With Rph=12, Yph=15 and Bph=16 the test will fail, because they are greater than Per1_Cal (=10.10) but not smaller than Per5_Cal (=10.50). Your code is extremely difficult to read, because it is not formatted and your variable names doesn't really mean anything. Also break up your program into smaller sections by introducing functions.

Could you explain in more detail what it is, that you are trying to do?

You could also reduce the code size by around 50% by using an array with 3 members for RYB, as they are all processed in the same way.

Here is code .
my code always load time=20s after validating 5 error.

I would like to load Timing value based on range
Rph,yph,bph are changing in actual code.currently i have put default value.
how can i meet condition

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;

      }

    }

  } 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);
}

my code always load time=20s after validating 5 error.

Check your comparisons, this does not mean Rph_Current_Float is between Per1_Cal and Per5_Cal

((Rph_Current_Float > Per1_Cal) && (Per5_Cal < Rph_Current_Float)