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;