Hallo guys, i wanna ask you about my project. I make a function that return as a pointer. But when i use it two times the value will be the last value of calculation. If i make another function which same code with before the result will be good. Any one know what is the problem?
float * fTeg1;
float * fTeg2;
void loop()
{
Teg1 = Error ;
Teg2 = Derror ;
//Function
fTeg1 = fuzzy_TegLDR(Teg1, -0.322, -0.225, -0.113, 0, 0.0293, 0.0586, 0.087);
Serial.print("-fuzzy TegLDR1 = ");
Serial.print(fTeg1[0]); Serial.print('\t');
Serial.print(fTeg1[1]); Serial.print('\t');
Serial.print(fTeg1[2]); Serial.print('\t');
Serial.print(fTeg1[3]); Serial.print('\t');
Serial.println(fTeg1[4]); //Serial.print('\t');
fTeg2 = fuzzy_TegLDR(Teg2, -0.229, -0.152, -0.076, 0, 0.052, 0.104, 0.156);
Serial.print("-fuzzy TegLDR2 = ");
Serial.print(fTeg2[0]); Serial.print('\t');
Serial.print(fTeg2[1]); Serial.print('\t');
Serial.print(fTeg2[2]); Serial.print('\t');
Serial.print(fTeg2[3]); Serial.print('\t');
Serial.print(fTeg2[4]); // Serial.print('\t');
Serial.println();
//This code serial print is same like before but the value is different
printFuzz();
}
float* fuzzy_TegLDR (double Teg, double batas1, double batas2, double batas3, double batas4, double batas5, double batas6, double batas7)
{
static float fuzz [5] = {0, 0, 0, 0, 0};
// untuk sangat gelap
if (Teg <= batas1 )
{fuzz[0] = 1;}
else if ((Teg > batas1) && (Teg <= batas2))
{fuzz[0] = l_turun(Teg, batas1, batas2);}
else if (Teg > batas2)
{fuzz[0] = 0;}
// untuk gelap
if ((Teg < batas1) || (Teg > batas3))
{fuzz[1] = 0;}
else if (( Teg >= batas1) && (Teg <= batas2))
{fuzz[1]= l_naik(Teg, batas1, batas2);}
else if (( Teg > batas2) && (Teg <= batas3))
{fuzz[1] = l_turun(Teg, batas2, batas3);}
// untuk normal
if ((Teg < batas2) || (Teg > batas6))
{fuzz[2] = 0;}
else if (( Teg >= batas2) && (Teg <= batas4))
{fuzz[2]= l_naik(Teg, batas2, batas4);}
else if (( Teg > batas4) && (Teg <= batas6))
{fuzz[2] = l_turun(Teg, batas4, batas6);}
// untuk terang
if ((Teg < batas5) || (Teg > batas7))
{fuzz[3] = 0;}
else if (( Teg >= batas5) && (Teg <= batas6))
{fuzz[3]= l_naik(Teg, batas5, batas6);}
else if (( Teg > batas6) && (Teg <= batas7))
{fuzz[3] = l_turun(Teg, batas6, batas7);}
// untuk sangat terang
if ((Teg < batas6) )
{fuzz[4] = 0;}
else if ((Teg >= batas6) && (Teg < batas7))
{fuzz[4] = l_naik(Teg, batas6, batas7);}
else if ((Teg >= batas7))
{fuzz[4] = 1;}
Serial.print("fuzzy TegLDR1 = ");
Serial.print(fuzz[0]); Serial.print('\t');
Serial.print(fuzz[1]); Serial.print('\t');
Serial.print(fuzz[2]); Serial.print('\t');
Serial.print(fuzz[3]); Serial.print('\t');
Serial.println(fuzz[4]); //Serial.print('\t');
return fuzz;
}
void printFuzz()
{
// fuzzy input TegLDR1 - TegLDR2
Serial.print("fuzzy TegLDR1 = ");
Serial.print(fTeg1[0]); Serial.print('\t');
Serial.print(fTeg1[1]); Serial.print('\t');
Serial.print(fTeg1[2]); Serial.print('\t');
Serial.print(fTeg1[3]); Serial.print('\t');
Serial.println(fTeg1[4]); //Serial.print('\t');
Serial.print("fuzzy TegLDR2 = ");
Serial.print(fTeg2[0]); Serial.print('\t');
Serial.print(fTeg2[1]); Serial.print('\t');
Serial.print(fTeg2[2]); Serial.print('\t');
Serial.print(fTeg2[3]); Serial.print('\t');
Serial.print(fTeg2[4]); // Serial.print('\t');
Serial.println();
delay(500);
}
when i use this model the result for printFuzz() and Serial.print after fTeg1 = fuzzy_TegLDR(Teg1..) will show different value. But when i make the function for fuzzy_TegLDR1() and fuzzy_TegLDR2() the result is same. How come i got this problem? Help me to solve this please