Don't know why, Please help find the problems.

I have a code which writing a fuzzy logic controlling method. The code is this :

const double NB = -0.03;
const double NS = -0.005;// changed from -0.1 
const double ZE = 0;
const double PS = 0.005;
const double PB = 0.03;
const double Vref= 5.25;
const double Ratio = 0.22;  // The ratio of the voltage divider 0.216608,0.2074655389
const int analogPin = 0;     // pin that the sensor is attached to
const int OutputPin = 10;  //output pin 
boolean initialize = true;
double averagek;
double e = 0;
double D ;
// expert rules
double Rules[5][5] = 
{
  {NB,NB,NS,NS,ZE},
  {NB,NS,NS,ZE,PS},
  {NS,NS,ZE,PS,PS},
  {NS,ZE,PS,PS,PB},
  {ZE,PS,PS,PB,PB},
};

// fuzzy operators
double AND(double u1, double u2)
{
      return min(u1, u2);
}

double OR(double u1, double u2)
{
      return max(u1, u2);
}

// Membership functions for error
double ErrorNegativeBig(double x)
{      
        double a = -0.5;
        double b = -0.25;
        if (x<=(a)) return 1;
        if (x>=(b)) return 0;
      else return ((b)-x)/(b-(a));
}

double ErrorNegativeSmall(double x)
{
        double a = -0.5;
        double b = -0.25;
        double c = 0;
        if (x<=(a) || x>=c) return 0;
      if (x==(b)) return 1;
      if (x<(b)) return(a-x)/(a-b);
      else return (c-x)/(c-b);
}

double ErrorZeroEqual(double x)
{
        double a = -0.25;
        double b = 0;
        double c = 0.25;
        if (x<=(a) || x>=c) return 0;
      if (x==(b)) return 1;
      if (x<(b)) return(a-x)/(a-b);
      else return (c-x)/(c-b);
}

double ErrorPositiveSmall(double x)
{
        double a = 0;
        double b = 0.25;
        double c = 0.5;
        if (x<=(a) || x>=c) return 0;
      if (x==(b)) return 1;
      if (x<(b)) return(a-x)/(a-b);
      else return (c-x)/(c-b);
}

double ErrorPositiveBig(double x)
{
        double a = 0.25;
        double b = 0.5;
      if (x<=a) return 0;
      if (x>=b) return 1;
      else return (a-x)/(a-b);
}

// Membership functions for Difference in Error(Derror)
double DerrorNegativeBig(double x)
{      
        double a = -0.5;
        double b = -0.25;
        if (x<=(a)) return 1;
        if (x>=(b)) return 0;
      else return ((b)-x)/(b-(a));
}

double DerrorNegativeSmall(double x)
{
        double a = -0.5;
        double b = -0.25;
        double c = 0;
        if (x<=(a) || x>=c) return 0;
      if (x==(b)) return 1;
      if (x<(b)) return(a-x)/(a-b);
      else return (c-x)/(c-b);
}

double DerrorZeroEqual(double x)
{
      double a = -0.25;
        double b = 0;
        double c = 0.25;
        if (x<=(a) || x>=c) return 0;
      if (x==(b)) return 1;
      if (x<(b)) return(a-x)/(a-b);
      else return (c-x)/(c-b);
}

double DerrorPositiveSmall(double x)
{ 
      double a = 0;
        double b = 0.25;
        double c = 0.5;
        if (x<=(a) || x>=c) return 0;
      if (x==(b)) return 1;
      if (x<(b)) return(a-x)/(a-b);
      else return (c-x)/(c-b);
}

double DerrorPositiveBig(double x)
{
      double a = 0.25;
        double b = 0.5;
      if (x<=a) return 0;
      if (x>=b) return 1;
      else return (a-x)/(a-b);
}

void setup()  { 
  TCCR1B = TCCR1B & 0b11111000 | 0x01;  // set the pwm output pin 9 frequency as 31.45kHz
  pinMode(OutputPin, OUTPUT);  // sets the digital pin9 as output
   Serial.begin(9600); 
} 

void loop()  { 
   int i;
   int j;
   double Error[5];
   double Derror[5];
   double actualvout;
   double sumu;
   double percent;
   double action;
   double tmpu;
   double lastE;
   double averagelastk;
   double LD; //last D
   double de;
   if (initialize == true)
  {
    //Serial.println("#$%^&*@#%$^&*@$^%&$*&@*&$@*$%*@%$*&@^%$*&^%@*&*§_DISCOURSE_HOISTED_CODE_0_§@^%$*@");
    initialize = false;
    averagelastk = 0;
    lastE = 0;
    LD = 0.5;
    D = 0.5;
 // averagek = 0;
  } 
  else {
 //   Serial.println("error i - 1");
  //  Serial.println(e);
    averagelastk = averagek;
    lastE = e;
    LD = D;
  }
      double vout = analogRead(analogPin);// read the value of the potentiometer
        actualvout = (vout/1023)*5/Ratio;  // Multiply the ratio which the output voltage after passing through the voltage divider        e = Vref-actualvout;
       Serial.println("actualvout");
        Serial.println(actualvout);
        float a = 1 / 11 *  actualvout;
         Serial.println("a");
        Serial.println(a);
        double b = 10/11*averagelastk;
          Serial.println("b");
        Serial.println(b);
         averagek = a+b;
     //  averagek = ((10/11)*(averagelastk))+((1/11)* actualvout);
         Serial.println("averagek");
        Serial.println(averagek);
        averagelastk = averagek;
           Serial.println("averagelastk");
        Serial.println(averagelastk);
      //  e = Vref- averagek;
         e = Vref- actualvout;
        Serial.println("error");
        Serial.println(e);
        Serial.println("lastEEEEEEEEEEE");
        Serial.println(lastE);
        de = e - lastE;
       // Serial.println("Derror");
       // Serial.println(de);
       // e = -0.477;// for testing
       // de= 2 ;//for testing
// fuzzification
      Error[0]=ErrorNegativeBig(e);
        Error[1]=ErrorNegativeSmall(e);
      Error[2]=ErrorZeroEqual(e);
      Error[3]=ErrorPositiveSmall(e);
      Error[4]=ErrorPositiveBig(e);
//      for (int i=0; i<5; i++) {
//      Serial.print("Error");
//      Serial.println(i);
//      Serial.println(Error[i]);
//        }
      Derror[0]=DerrorNegativeBig(de);
      Derror[1]=DerrorNegativeSmall(de);
      Derror[2]=DerrorZeroEqual(de);
      Derror[3]=DerrorPositiveSmall(de);
      Derror[4]=DerrorPositiveBig(de);
//        for (int i=0; i<5; i++) {
//        Serial.print("DError");
//        Serial.println(i);
//        Serial.println(Derror[i]);
//        }
// fuzzy inference
      sumu=0;
        percent=0;
      for (i=0; i<5; i++)
          {
        for(j=0; j<5; j++)
        {
              tmpu = AND(Error[i],Derror[j]); //temporary Degree of membership of 
             // Serial.println("tmpu");
                 // Serial.println(tmpu);
                  percent = percent + Rules[j][i]*tmpu;
                  //Serial.println("percent");
                 // Serial.println(percent);
              sumu = sumu + tmpu;
                 // Serial.println("sumu");
                 // Serial.println(sumu);
        }
      }
//defuzzification
          percent = percent / sumu;
         // Serial.println("LD");
         // Serial.println(LD);
          D = LD + percent; 
          LD = D;
          if (D >= 0.85){ D = 0.85;}
          else if (D <= 0) {D = 0;}
          //Serial.println("percent");
         // Serial.println(percent);
         // Serial.println("D");
          //Serial.println(D);
          action = 255 * D ;
      //   Serial.println("action");
        //  Serial.println(action);
          analogWrite(OutputPin, action);     //Do the action which generates from the rules  
         //analogWrite(10, 128);
       
}

The corresponding results are here:

actualvout
5.55
a
0.00
b
0.00
averagek
0.00
averagelastk
0.00
error
-0.30
lastEEEEEEEEEEE
2.81

I just want to know why the “error” and the “LD” can store the last looping result while the “averagelastk” cannot.
I also don’t know why while I let a variable “a” and “b” to observe the equation " averagek = ((10/11)(averagelastk))+((1/11) actualvout);"
I found that both the a and b are 0. The problem is why the actualvout cannot do the multiplication 1/11*actualvout.

I need to solve this as soon as possible. Many thanks!!

float a = 1 / 11 *  actualvout;

The integer value 1 divided by the integer value 11 is 0. 0 times anything is 0, so a will always contain 0, regardless of the value in actualvout.

You have similar mistakes throughout the code. If you want to divide the actualvout value by 11, there is no reason to first compute 1.0/11.0. (which is a hint how to solve the problem).