# Problem Fuzzy

i had problem about my defuzzification process… when input1 & 2 =0, output should be 0. when i change input1 & 2 it give correct output value…but when i change back to input1&2 to 0, the output hold the previous value…it did not give 0 anymore…

viva.txt (11.2 KB)

``````byte getvalue ()
{
const byte returnVal [32] = {3, 6, 9, 13, 16, 19, 22, 25, 28, 31, 34,  38, 41, 44, 47,
50, 53, 56, ...etc };
return returnVal [index];
}
``````

Why write seven lines, when you can write the same thing in 140?

Why write seven lines, when you can write the same thing in 140?

LOL!

AWOL:

``````byte getvalue ()
``````

{
const byte returnVal [32] = {3, 6, 9, 13, 16, 19, 22, 25, 28, 31, 34,  38, 41, 44, 47,
50, 53, 56, …etc };
return returnVal [index];
}

``````

Why write seven lines, when you can write the same thing in 140?
``````

Or return map(index, 0, 31, 3, 100);

i had made changes using your solution but the output still had a same problem like previous programming… i did not no where the wrong this program…

#include <Fuzzy.h>
#include <FuzzyComposition.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzyOutput.h>
#include <FuzzyRule.h>
#include <FuzzyRuleAntecedent.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzySet.h>

# define DS_R 9 //5

float val = 0;
float lastError = 0;

// Instantiating an object of library
Fuzzy* fuzzy = new Fuzzy();

FuzzySet* negative = new FuzzySet(-0.44, -0.44, -0.44, 0);
FuzzySet* zero = new FuzzySet(-0.44, 0, 0, 0.53);
FuzzySet* positive = new FuzzySet(0, 0.53, 0.53, 0.53);

FuzzySet* negatif = new FuzzySet(-0.97, -0.97, -0.97, 0);
FuzzySet* kosong = new FuzzySet(-0.97, 0, 0, 0.97);
FuzzySet* positif = new FuzzySet(0, 0.97, 0.97, 0.97);

void setup(){
pinMode(DS_L,INPUT);
pinMode(DS_ML,INPUT);
pinMode(DS_M,INPUT);
pinMode(DS_MR,INPUT);
pinMode(DS_R,INPUT);

Serial.begin(9600);

// FuzzyInput
FuzzyInput* error = new FuzzyInput(1);

// FuzzyInput
FuzzyInput* derror = new FuzzyInput(2);

// FuzzyOutput
FuzzyOutput* motors = new FuzzyOutput(1);

FuzzySet* HLeft = new FuzzySet(-1.50, -1.00, -1.00, -0.50);
FuzzySet* SLeft = new FuzzySet(-1.00, -0.50, -0.50, 0);
FuzzySet* Straight = new FuzzySet(-0.50, 0, 0, 0.50);
FuzzySet* SRight = new FuzzySet(0, 0.50, 0.50, 1.00);
FuzzySet* HRight = new FuzzySet(0.50, 1.00, 1.00, 1.50);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrornegativeAndderrornegatif = new FuzzyRuleAntecedent();
iferrornegativeAndderrornegatif->joinWithAND(negative, negatif);

FuzzyRuleConsequent* thenmotorsHRight = new FuzzyRuleConsequent();

FuzzyRule* fuzzyRule1 = new FuzzyRule(1, iferrornegativeAndderrornegatif, thenmotorsHRight);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrornegativeAndderrorkosong = new FuzzyRuleAntecedent();
iferrornegativeAndderrorkosong->joinWithAND(negative, kosong);
FuzzyRuleAntecedent* iferrornegativeAndderrorpositif = new FuzzyRuleAntecedent();
iferrornegativeAndderrorpositif->joinWithAND(negative, positif);
FuzzyRuleAntecedent* iferrorzeroAndderrornegatif = new FuzzyRuleAntecedent();
iferrorzeroAndderrornegatif->joinWithAND(zero, negatif);

FuzzyRule* fuzzyRule2 = new FuzzyRule(2, iferrornegativeAndderrorkosong, thenmotorsSRight);
FuzzyRule* fuzzyRule3 = new FuzzyRule(3, iferrornegativeAndderrorpositif, thenmotorsSRight);
FuzzyRule* fuzzyRule4 = new FuzzyRule(4, iferrorzeroAndderrornegatif, thenmotorsSRight);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrorzeroAndderrorkosong = new FuzzyRuleAntecedent();
iferrorzeroAndderrorkosong->joinWithAND(zero, kosong);

FuzzyRule* fuzzyRule5 = new FuzzyRule(5, iferrorzeroAndderrorkosong, thenmotorsStraight);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrorzeroAndderrorpositif = new FuzzyRuleAntecedent();
iferrorzeroAndderrorpositif->joinWithAND(zero, positif);
FuzzyRuleAntecedent* iferrorpositiveAndderrornegatif = new FuzzyRuleAntecedent();
iferrorpositiveAndderrornegatif->joinWithAND(positive, negatif);
FuzzyRuleAntecedent* iferrorpositiveAndderrorkosong = new FuzzyRuleAntecedent();
iferrorpositiveAndderrorkosong->joinWithAND(positive, kosong);

FuzzyRule* fuzzyRule6 = new FuzzyRule(6, iferrorzeroAndderrorpositif, thenmotorsSLeft);
FuzzyRule* fuzzyRule7 = new FuzzyRule(7, iferrorpositiveAndderrornegatif, thenmotorsSLeft);
FuzzyRule* fuzzyRule8 = new FuzzyRule(8, iferrorpositiveAndderrorkosong, thenmotorsSLeft);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrorpositiveAndderrorpositif = new FuzzyRuleAntecedent();
iferrorpositiveAndderrorpositif->joinWithAND(positive, positif);

FuzzyRuleConsequent* thenmotorsHLeft = new FuzzyRuleConsequent();

FuzzyRule* fuzzyRule9 = new FuzzyRule(9, iferrorpositiveAndderrorpositif, thenmotorsHLeft);
}

void loop()
{
getvalue();
float error = val - 0.4688;
float derror = error - lastError;
lastError = error;

Serial.print(“Value:”);
Serial.println(val);
Serial.print(“Error:”);
Serial.println(error);
Serial.print(“Derror:”);
Serial.println(derror);

fuzzy->setInput(1,error);
fuzzy->setInput(2,derror);

fuzzy->fuzzify();

Serial.print("Error: “);
Serial.print(negative->getPertinence());
Serial.print(”, “);
Serial.print(zero->getPertinence());
Serial.print(”, ");
Serial.println(positive->getPertinence());

Serial.print("Derror: “);
Serial.print(negatif->getPertinence());
Serial.print(”, “);
Serial.print(kosong->getPertinence());
Serial.print(”, ");
Serial.println(positif->getPertinence());

float motors = fuzzy->defuzzify(1);

Serial.print("Motors: ");
Serial.println(motors);

delay(1000);
}

void getvalue()
{
float returnVal [32] = {0.0313, 0.0625, 0.0938, 0.1250, 0.1563, 0.1875, 0.2188, 0.2500, 0.2813, 0.3125, 0.3438, 0.3750, 0.4063, 0.4375, 0.4688,
0.5000, 0.5313, 0.5625, 0.5938, 0.6250, 0.6563, 0.6875, 0.7188, 0.7500, 0.7813, 0.8125, 0.8438, 0.8750, 0.9063, 0.9375, 0.9688, 1.0000};
val = returnVal [index];
}

anybody know how to use this http://www.makeproto.com/projects/fuzzy/matlab_arduino_FIST/index.php i did not know how to put it into programming.. maybe some tutorial could be good for me..

Shorter still.
uncompiled, untested

``````float getvalue()
{