Anybody know how to connect output value from fuzzy to dc motor

I use 3 input ultrasonic sensor and 1 output that the value going to 2 DC motor. I use L298N for DC motors. But im stuck in connecting fuzzy output value to my DC motor.
Here is code:

#include <Wire.h>

#include <Fuzzy.h>

// Motor DC

#define ENA 7

#define ENB 6

#define IN1 33

#define IN2 32

#define IN3 35

#define IN4 34

// Pin ultrasonik kiri

#define echo1 22

#define trig1 23

// Pin ultrasonik tengah

#define echo2 24

#define trig2 25

// Pin ultrasonik kanan

#define echo3 26

#define trig3 27

// Instantiating a Fuzzy object

Fuzzy *fuzzy = new Fuzzy();

// Fuzzy Input Jarak Ultrasonik 1

FuzzySet *d1 = new FuzzySet(0, 0, 50, 100);

FuzzySet *s1 = new FuzzySet(50, 100, 200, 250);

FuzzySet *j1 = new FuzzySet(200, 250, 400, 400);

// Fuzzy Input Jarak Ultrasonik 2

FuzzySet *d2 = new FuzzySet(0, 0, 50, 100);

FuzzySet *s2 = new FuzzySet(50, 100, 200, 250);

FuzzySet *j2 = new FuzzySet(200, 250, 400, 400);

// Fuzzy Input Jarak Ultrasonik 3

FuzzySet *d3 = new FuzzySet(0, 0, 50, 100);

FuzzySet *s3 = new FuzzySet(50, 100, 200, 250);

FuzzySet *j3 = new FuzzySet(200, 250, 400, 400);

// Fuzzy Output servo

FuzzySet *l = new FuzzySet(0, 0, 40, 80);

FuzzySet *s = new FuzzySet(40, 80, 120, 160);

FuzzySet *c = new FuzzySet(120, 160, 200, 255);

void setup() {

// put your setup code here, to run once:

// Set the Serial output

Serial.begin(9600);

pinMode(ENA, OUTPUT);

pinMode(ENB, OUTPUT);

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

pinMode(IN3, OUTPUT);

pinMode(IN4, OUTPUT);

// Setup Ultrasonik

pinMode(trig1, OUTPUT);

pinMode(trig2, OUTPUT);

pinMode(trig3, OUTPUT);

pinMode(echo1, INPUT);

pinMode(echo2, INPUT);

pinMode(echo3, INPUT);

Wire.begin();

// FuzzyInput jarak1

FuzzyInput *jarak1 = new FuzzyInput(1);

jarak1->addFuzzySet(d1);

jarak1->addFuzzySet(s1);

jarak1->addFuzzySet(j1);

fuzzy->addFuzzyInput(jarak1);

// FuzzyInput jarak2

FuzzyInput *jarak2 = new FuzzyInput(2);

jarak2->addFuzzySet(d2);

jarak2->addFuzzySet(s2);

jarak2->addFuzzySet(j2);

fuzzy->addFuzzyInput(jarak2);

// FuzzyInput jarak3

FuzzyInput *jarak3 = new FuzzyInput(3);

jarak3->addFuzzySet(d3);

jarak3->addFuzzySet(s3);

jarak3->addFuzzySet(j3);

fuzzy->addFuzzyInput(jarak3);

// FuzzyOutput kecepatan motor

FuzzyOutput *motor = new FuzzyOutput(1);

motor->addFuzzySet(l);

motor->addFuzzySet(s);

motor->addFuzzySet(c);

fuzzy->addFuzzyOutput(motor);

// Declaration Antecedent

FuzzyRuleAntecedent *d1_d2 = new FuzzyRuleAntecedent();

d1_d2->joinWithAND(d1, d2);

FuzzyRuleAntecedent *d1_s2 = new FuzzyRuleAntecedent();

d1_s2->joinWithAND(d1, s2);

FuzzyRuleAntecedent *d1_j2 = new FuzzyRuleAntecedent();

d1_j2->joinWithAND(d1, j2);

FuzzyRuleAntecedent *s1_d2 = new FuzzyRuleAntecedent();

s1_d2->joinWithAND(s1, d2);

FuzzyRuleAntecedent *s1_s2 = new FuzzyRuleAntecedent();

s1_s2->joinWithAND(s1, s2);

FuzzyRuleAntecedent *s1_j2 = new FuzzyRuleAntecedent();

s1_j2->joinWithAND(s1, j2);

FuzzyRuleAntecedent *j1_d2 = new FuzzyRuleAntecedent();

j1_d2->joinWithAND(j1, d2);

FuzzyRuleAntecedent *j1_s2 = new FuzzyRuleAntecedent();

j1_s2->joinWithAND(j1, s2);

FuzzyRuleAntecedent *j1_j2 = new FuzzyRuleAntecedent();

j1_j2->joinWithAND(j1, j2);

FuzzyRuleAntecedent *d3_antecedent = new FuzzyRuleAntecedent();

d3_antecedent->joinSingle(d3);

FuzzyRuleAntecedent *s3_antecedent = new FuzzyRuleAntecedent();

s3_antecedent->joinSingle(s3);

FuzzyRuleAntecedent *j3_antecedent = new FuzzyRuleAntecedent();

j3_antecedent->joinSingle(j3);

// Declaration Consequent

FuzzyRuleConsequent *thenmotor_l = new FuzzyRuleConsequent();

thenmotor_l->addOutput(l);

FuzzyRuleConsequent *thenmotor_s = new FuzzyRuleConsequent();

thenmotor_s->addOutput(s);

FuzzyRuleConsequent *thenmotor_c = new FuzzyRuleConsequent();

thenmotor_c->addOutput(c);

// FuzzyRule 1

FuzzyRuleAntecedent *d1_d2_d3 = new FuzzyRuleAntecedent();

d1_d2_d3->joinWithOR(d1_d2, d3);

FuzzyRule *fuzzyRule1 = new FuzzyRule(1, d1_d2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule1);

// FuzzyRule 2

FuzzyRuleAntecedent *d1_d2_s3 = new FuzzyRuleAntecedent();

d1_d2_s3->joinWithOR(d1_d2, s3);

FuzzyRule *fuzzyRule2 = new FuzzyRule(2, d1_d2_s3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule2);

// FuzzyRule 3

FuzzyRuleAntecedent *d1_d2_j3 = new FuzzyRuleAntecedent();

d1_d2_j3->joinWithOR(d1_d2, j3);

FuzzyRule *fuzzyRule3 = new FuzzyRule(3, d1_d2_j3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule3);

// FuzzyRule 4

FuzzyRuleAntecedent *d1_s2_d3 = new FuzzyRuleAntecedent();

d1_s2_d3->joinWithAND(d1_s2, d3);

FuzzyRule *fuzzyRule4 = new FuzzyRule(4, d1_s2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule4);

// FuzzyRule 5

FuzzyRuleAntecedent *d1_s2_s3 = new FuzzyRuleAntecedent();

d1_s2_s3->joinWithAND(d1_s2, s3);

FuzzyRule *fuzzyRule5 = new FuzzyRule(5, d1_s2_s3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule5);

// FuzzyRule 6

FuzzyRuleAntecedent *d1_s2_j3 = new FuzzyRuleAntecedent();

d1_s2_j3->joinWithAND(d1_s2, j3);

FuzzyRule *fuzzyRule6 = new FuzzyRule(6, d1_s2_j3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule6);

// FuzzyRule 7

FuzzyRuleAntecedent *d1_j2_d3 = new FuzzyRuleAntecedent();

d1_j2_d3->joinWithAND(d1_j2, d3);

FuzzyRule *fuzzyRule7 = new FuzzyRule(7, d1_j2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule7);

// FuzzyRule 8

FuzzyRuleAntecedent *d1_j2_s3 = new FuzzyRuleAntecedent();

d1_j2_s3->joinWithAND(d1_j2, s3);

FuzzyRule *fuzzyRule8 = new FuzzyRule(8, d1_j2_s3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule8);

// FuzzyRule 9

FuzzyRuleAntecedent *d1_j2_j3 = new FuzzyRuleAntecedent();

d1_j2_j3->joinWithAND(d1_j2, j3);

FuzzyRule *fuzzyRule9 = new FuzzyRule(9, d1_j2_j3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule9);

// FuzzyRule 10

FuzzyRuleAntecedent *s1_d2_d3 = new FuzzyRuleAntecedent();

s1_d2_d3->joinWithAND(s1_d2, d3);

FuzzyRule *fuzzyRule10 = new FuzzyRule(10, s1_d2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule10);

// FuzzyRule 11

FuzzyRuleAntecedent *s1_d2_s3 = new FuzzyRuleAntecedent();

s1_d2_s3->joinWithAND(s1_d2, s3);

FuzzyRule *fuzzyRule11 = new FuzzyRule(11, s1_d2_s3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule11);

// FuzzyRule 12

FuzzyRuleAntecedent *s1_d2_j3 = new FuzzyRuleAntecedent();

s1_d2_j3->joinWithAND(s1_d2, j3);

FuzzyRule *fuzzyRule12 = new FuzzyRule(12, s1_d2_j3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule12);

// FuzzyRule 13

FuzzyRuleAntecedent *s1_s2_d3 = new FuzzyRuleAntecedent();

s1_s2_d3->joinWithAND(s1_s2, d3);

FuzzyRule *fuzzyRule13 = new FuzzyRule(13, s1_s2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule13);

// FuzzyRule 14

FuzzyRuleAntecedent *s1_s2_s3 = new FuzzyRuleAntecedent();

s1_s2_s3->joinWithAND(s1_s2, s3);

FuzzyRule *fuzzyRule14 = new FuzzyRule(14, s1_s2_s3, thenmotor_s);

fuzzy->addFuzzyRule(fuzzyRule14);

// FuzzyRule 15

FuzzyRuleAntecedent *s1_s2_j3 = new FuzzyRuleAntecedent();

s1_s2_j3->joinWithAND(s1_s2, j3);

FuzzyRule *fuzzyRule15 = new FuzzyRule(15, s1_s2_j3, thenmotor_s);

fuzzy->addFuzzyRule(fuzzyRule15);

// FuzzyRule 16

FuzzyRuleAntecedent *s1_j2_d3 = new FuzzyRuleAntecedent();

s1_j2_d3->joinWithAND(s1_j2, d3);

FuzzyRule *fuzzyRule16 = new FuzzyRule(16, s1_j2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule16);

// FuzzyRule 17

FuzzyRuleAntecedent *s1_j2_s3 = new FuzzyRuleAntecedent();

s1_j2_s3->joinWithAND(s1_j2, s3);

FuzzyRule *fuzzyRule17 = new FuzzyRule(17, s1_j2_s3, thenmotor_s);

fuzzy->addFuzzyRule(fuzzyRule17);

// FuzzyRule 18

FuzzyRuleAntecedent *s1_j2_j3 = new FuzzyRuleAntecedent();

s1_j2_j3->joinWithAND(s1_j2, j3);

FuzzyRule *fuzzyRule18 = new FuzzyRule(18, s1_j2_j3, thenmotor_s);

fuzzy->addFuzzyRule(fuzzyRule18);

// FuzzyRule 19

FuzzyRuleAntecedent *j1_d2_d3 = new FuzzyRuleAntecedent();

j1_d2_d3->joinWithAND(j1_d2, d3);

FuzzyRule *fuzzyRule19 = new FuzzyRule(19, j1_d2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule19);

// FuzzyRule 20

FuzzyRuleAntecedent *j1_d2_s3 = new FuzzyRuleAntecedent();

j1_d2_s3->joinWithAND(j1_d2, s3);

FuzzyRule *fuzzyRule20 = new FuzzyRule(20, j1_d2_s3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule20);

// FuzzyRule 21

FuzzyRuleAntecedent *j1_d2_j3 = new FuzzyRuleAntecedent();

j1_d2_j3->joinWithAND(j1_d2, j3);

FuzzyRule *fuzzyRule21 = new FuzzyRule(21, j1_d2_j3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule21);

// FuzzyRule 22

FuzzyRuleAntecedent *j1_s2_d3 = new FuzzyRuleAntecedent();

j1_s2_d3->joinWithAND(j1_s2, d3);

FuzzyRule *fuzzyRule22 = new FuzzyRule(22, j1_s2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule22);

// FuzzyRule 23

FuzzyRuleAntecedent *j1_s2_s3 = new FuzzyRuleAntecedent();

j1_s2_s3->joinWithAND(j1_s2, s3);

FuzzyRule *fuzzyRule23 = new FuzzyRule(23, j1_s2_s3, thenmotor_s);

fuzzy->addFuzzyRule(fuzzyRule23);

// FuzzyRule 24

FuzzyRuleAntecedent *j1_s2_j3 = new FuzzyRuleAntecedent();

j1_s2_j3->joinWithAND(j1_s2, j3);

FuzzyRule *fuzzyRule24 = new FuzzyRule(24, j1_s2_j3, thenmotor_s);

fuzzy->addFuzzyRule(fuzzyRule24);

// FuzzyRule 25

FuzzyRuleAntecedent *j1_j2_d3 = new FuzzyRuleAntecedent();

j1_j2_d3->joinWithAND(j1_j2, d3);

FuzzyRule *fuzzyRule25 = new FuzzyRule(25, j1_j2_d3, thenmotor_l);

fuzzy->addFuzzyRule(fuzzyRule25);

// FuzzyRule 26

FuzzyRuleAntecedent *j1_j2_s3 = new FuzzyRuleAntecedent();

j1_j2_s3->joinWithAND(j1_j2, s3);

FuzzyRule *fuzzyRule26 = new FuzzyRule(26, j1_j2_s3, thenmotor_s);

fuzzy->addFuzzyRule(fuzzyRule26);

// FuzzyRule 27

FuzzyRuleAntecedent *j1_j2_j3 = new FuzzyRuleAntecedent();

j1_j2_j3->joinWithAND(j1_j2, j3);

FuzzyRule *fuzzyRule27 = new FuzzyRule(27, j1_j2_j3, thenmotor_c);

fuzzy->addFuzzyRule(fuzzyRule27);

}

void loop() {

// put your main code here, to run repeatedly:

long waktu1, waktu2, waktu3, jarak1, jarak2, jarak3;

// Motor DC

analogWrite(ENA, output);

analogWrite(ENB, output);

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

digitalWrite(IN3, HIGH);

digitalWrite(IN4, LOW);

// Sensor Ultrasonik Kiri

digitalWrite(trig1, LOW);

delayMicroseconds(2);

digitalWrite(trig1, HIGH);

delayMicroseconds(10);

digitalWrite(trig1, LOW);

waktu1=pulseIn(echo1, HIGH);

jarak1=(waktu1 / 2) / 29.1;

Serial.print(jarak1);

Serial.println(" cm");

delay(500);

// Sensor Ultrasonik Tengah

digitalWrite(trig2, LOW);

delayMicroseconds(2);

digitalWrite(trig2, HIGH);

delayMicroseconds(10);

digitalWrite(trig2, LOW);

waktu2=pulseIn(echo2, HIGH);

jarak2=(waktu2 / 2) / 29.1;

Serial.print(jarak2);

Serial.println(" cm");

delay(500);

// Sensor Ultrasonik Kanan

digitalWrite(trig3, LOW);

delayMicroseconds(2);

digitalWrite(trig3, HIGH);

delayMicroseconds(10);

digitalWrite(trig3, LOW);

waktu3=pulseIn(echo3, HIGH);

jarak3=(waktu3 / 2) / 29.1;

Serial.print(jarak3);

Serial.println(" cm");

delay(500);

fuzzy->setInput(1, jarak1);

fuzzy->setInput(2, jarak2);

fuzzy->setInput(3, jarak3);

fuzzy->fuzzify();

// Running the Defuzzification

float output = fuzzy->defuzzify(1);

Serial.println(output);

if (jarak1 < 20 or jarak2 < 20 or jarak3 < 20 ){

output = 0;

}

}

I highly recommend a newer version of the driver, you will lose about 3 volts to the motor using that. I have not clue what Fuzzy is and the link did not work.

As @gilshultz says, what is this "Fuzzy" that you're using?

Have you studied its documentation? What does it tell you?

1 Like

have you verified with prints the output value to the motor(s)?
and if zero, have you verified the value to the fuzzy object that generate the output value?

I use eFLL latest version in arduino IDE. I followed from the example in the library to make structure of Fuzzy Logic Controller


Its error when im doing verify. So, i can't print it in serial monitor.
But, the sensors is working, and so does the Fuzzy Logic Control. The problem is how to connect the 'output' value to DC motors.

I use the latest eFLL arduino IDE library.
The Fuzzy is working, and have 'output' value. But im confused how to connect it to DC motors to have same value to Fuzzy Logic Controller.

Sorry your screen capture is not readable. Read the forum guidelines and they will explain how to post the information.

Oh sorry, im new here so i didn't know about it.
Im just posting the error massage. It says "Compilation error 'output' was not declared in this scope" at analogWrite(ENA, output); line.

Your definition of output is at end of loop. But the analogWrite is at the start.
Move the analogWrite after the point where you calculated ouput.
What value does fuzzy give?
AnalogWrite takes an integer (0-255). A float will be truncated to the nearest integer...
Values lower than 1.0 will be rounded to 0.
Values higher than 255 will cause an overflow...

as build_1971 said
the output should be declared above these lines. like float output=0
but you have declared it in line 589 float output = fuzzy->defuzzify(1); which is causing problem

I have asked my lecturer about my problem, and he said that i have to use 'if' and 'else if' command to control DC motors speed. The DC motor speed have to be as same as output value. The problem is now my dc motor is not following my code. The motor speed remains constant. Here the new code.

if (jarak1 < 20  or jarak2 < 20 or jarak3 < 20 ){
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
output = 0;
}
else if ((jarak1 > 20 or jarak1 < 100) and (jarak2 > 20 or jarak2 < 100) and (jarak3 >20 or jarak3 < 100)){
analogWrite(ENA, 50);
analogWrite(ENB, 50);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if ((jarak1 > 100 or jarak1 < 200) and (jarak2 > 100 or jarak2 < 200) and (jarak3 >100 or jarak3 < 200)){
analogWrite(ENA, 120);
analogWrite(ENB, 120);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if ((jarak1 > 200 or jarak1 < 300) and (jarak2 > 200 or jarak2 < 300) and (jarak3 >200 or jarak3 < 300)){
analogWrite(ENA, 200);
analogWrite(ENB, 200);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else{
analogWrite(ENA, 255);
analogWrite(ENB, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

Did i make a mistake or something? I think my code is right but idk, it doesn't really work.

I don't really understand about that. I just followed the instructions from the example. I need to learn more about the commands on arduino.

Where is the rest of your code?
Please do not make logical calcs like that. It is very hard to follow.
If the logic really needs to be that complicated: split your logic...

if (digitalRead(somePin)==HIGH) {
   cond1=true:
}
Etcetera....
bool allOK = cond1 and cond2 and cond3;
bool overrideAll = cond4 and cond5;
If (allOK or overrideAll) {
   DoWhatever;
}

What is yarak?
I suggest you start makung functions...
Like:
setSpeedM1(int speed)
It will save you a lot of copy paste...

Here is all of the code

#include <Wire.h>

#include <Fuzzy.h>

// Motor DC
#define ENA 7
#define ENB 6
#define IN1 33
#define IN2 32
#define IN3 35
#define IN4 34

// Pin ultrasonik kiri
#define echo1 22
#define trig1 23

// Pin ultrasonik tengah
#define echo2 24
#define trig2 25

// Pin ultrasonik kanan
#define echo3 26
#define trig3 27

// Instantiating a Fuzzy object
Fuzzy *fuzzy = new Fuzzy();

// Fuzzy Input Jarak Ultrasonik 1
FuzzySet *d1 = new FuzzySet(0, 0, 50, 100);
FuzzySet *s1 = new FuzzySet(50, 100, 150, 200);
FuzzySet *j1 = new FuzzySet(150, 200, 250, 300);

// Fuzzy Input Jarak Ultrasonik 2
FuzzySet *d2 = new FuzzySet(0, 0, 50, 100);
FuzzySet *s2 = new FuzzySet(50, 100, 150, 200);
FuzzySet *j2 = new FuzzySet(150, 200, 250, 300);

// Fuzzy Input Jarak Ultrasonik 3
FuzzySet *d3 = new FuzzySet(0, 0, 50, 100);
FuzzySet *s3 = new FuzzySet(50, 100, 150, 200);
FuzzySet *j3 = new FuzzySet(150, 200, 250, 300);

// Fuzzy Output Motor DC
FuzzySet *l = new FuzzySet(0, 0, 40, 80);
FuzzySet *s = new FuzzySet(40, 80, 120, 160);
FuzzySet *c = new FuzzySet(120, 160, 200, 255);

void setup() {
// put your setup code here, to run once:
// Set the Serial output
Serial.begin(9600);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

// Setup Ultrasonik
pinMode(trig1, OUTPUT);
pinMode(trig2, OUTPUT);
pinMode(trig3, OUTPUT);
pinMode(echo1, INPUT);
pinMode(echo2, INPUT);
pinMode(echo3, INPUT);
Wire.begin();

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

  jarak1->addFuzzySet(d1);
  jarak1->addFuzzySet(s1);
  jarak1->addFuzzySet(j1);
  fuzzy->addFuzzyInput(jarak1);

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

  jarak2->addFuzzySet(d2);
  jarak2->addFuzzySet(s2);
  jarak2->addFuzzySet(j2);
  fuzzy->addFuzzyInput(jarak2);

// FuzzyInput jarak3
FuzzyInput *jarak3 = new FuzzyInput(3);

  jarak3->addFuzzySet(d3);
  jarak3->addFuzzySet(s3);
  jarak3->addFuzzySet(j3);
  fuzzy->addFuzzyInput(jarak3);

// FuzzyOutput kecepatan motor
FuzzyOutput *motor = new FuzzyOutput(1);

  motor->addFuzzySet(l);
  motor->addFuzzySet(s);
  motor->addFuzzySet(c);
  fuzzy->addFuzzyOutput(motor);

// Declaration Antecedent
FuzzyRuleAntecedent *d1_d2 = new FuzzyRuleAntecedent();
  d1_d2->joinWithAND(d1, d2);

FuzzyRuleAntecedent *d1_s2 = new FuzzyRuleAntecedent();
  d1_s2->joinWithAND(d1, s2);

FuzzyRuleAntecedent *d1_j2 = new FuzzyRuleAntecedent();
  d1_j2->joinWithAND(d1, j2);

FuzzyRuleAntecedent *s1_d2 = new FuzzyRuleAntecedent();
  s1_d2->joinWithAND(s1, d2);

FuzzyRuleAntecedent *s1_s2 = new FuzzyRuleAntecedent();
  s1_s2->joinWithAND(s1, s2);

FuzzyRuleAntecedent *s1_j2 = new FuzzyRuleAntecedent();
  s1_j2->joinWithAND(s1, j2);

FuzzyRuleAntecedent *j1_d2 = new FuzzyRuleAntecedent();
  j1_d2->joinWithAND(j1, d2);

FuzzyRuleAntecedent *j1_s2 = new FuzzyRuleAntecedent();
  j1_s2->joinWithAND(j1, s2);

FuzzyRuleAntecedent *j1_j2 = new FuzzyRuleAntecedent();
  j1_j2->joinWithAND(j1, j2);

FuzzyRuleAntecedent *d3_antecedent = new FuzzyRuleAntecedent();
  d3_antecedent->joinSingle(d3);

FuzzyRuleAntecedent *s3_antecedent = new FuzzyRuleAntecedent();
  s3_antecedent->joinSingle(s3);

FuzzyRuleAntecedent *j3_antecedent = new FuzzyRuleAntecedent();
  j3_antecedent->joinSingle(j3);

// Declaration Consequent
FuzzyRuleConsequent *thenmotor_l = new FuzzyRuleConsequent();
  thenmotor_l->addOutput(l);

FuzzyRuleConsequent *thenmotor_s = new FuzzyRuleConsequent();
  thenmotor_s->addOutput(s);

FuzzyRuleConsequent *thenmotor_c = new FuzzyRuleConsequent();
  thenmotor_c->addOutput(c);

// FuzzyRule 1
FuzzyRuleAntecedent *d1_d2_d3 = new FuzzyRuleAntecedent();
  d1_d2_d3->joinWithOR(d1_d2, d3_antecedent);

FuzzyRule *fuzzyRule1 = new FuzzyRule(1, d1_d2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule1);

// FuzzyRule 2
FuzzyRuleAntecedent *d1_d2_s3 = new FuzzyRuleAntecedent();
  d1_d2_s3->joinWithOR(d1_d2, s3_antecedent);

FuzzyRule *fuzzyRule2 = new FuzzyRule(2, d1_d2_s3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule2);

// FuzzyRule 3
FuzzyRuleAntecedent *d1_d2_j3 = new FuzzyRuleAntecedent();
  d1_d2_j3->joinWithOR(d1_d2, j3_antecedent);

FuzzyRule *fuzzyRule3 = new FuzzyRule(3, d1_d2_j3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule3);

// FuzzyRule 4
FuzzyRuleAntecedent *d1_s2_d3 = new FuzzyRuleAntecedent();
  d1_s2_d3->joinWithAND(d1_s2, d3_antecedent);

FuzzyRule *fuzzyRule4 = new FuzzyRule(4, d1_s2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule4);

// FuzzyRule 5
FuzzyRuleAntecedent *d1_s2_s3 = new FuzzyRuleAntecedent();
  d1_s2_s3->joinWithAND(d1_s2, s3_antecedent);

FuzzyRule *fuzzyRule5 = new FuzzyRule(5, d1_s2_s3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule5);

// FuzzyRule 6
FuzzyRuleAntecedent *d1_s2_j3 = new FuzzyRuleAntecedent();
  d1_s2_j3->joinWithAND(d1_s2, j3_antecedent);

FuzzyRule *fuzzyRule6 = new FuzzyRule(6, d1_s2_j3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule6);

// FuzzyRule 7
FuzzyRuleAntecedent *d1_j2_d3 = new FuzzyRuleAntecedent();
  d1_j2_d3->joinWithAND(d1_j2, d3_antecedent);

FuzzyRule *fuzzyRule7 = new FuzzyRule(7, d1_j2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule7);

// FuzzyRule 8
FuzzyRuleAntecedent *d1_j2_s3 = new FuzzyRuleAntecedent();
  d1_j2_s3->joinWithAND(d1_j2, s3_antecedent);

FuzzyRule *fuzzyRule8 = new FuzzyRule(8, d1_j2_s3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule8);

// FuzzyRule 9
FuzzyRuleAntecedent *d1_j2_j3 = new FuzzyRuleAntecedent();
  d1_j2_j3->joinWithAND(d1_j2, j3_antecedent);

FuzzyRule *fuzzyRule9 = new FuzzyRule(9, d1_j2_j3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule9);

// FuzzyRule 10
FuzzyRuleAntecedent *s1_d2_d3 = new FuzzyRuleAntecedent();
  s1_d2_d3->joinWithAND(s1_d2, d3_antecedent);

FuzzyRule *fuzzyRule10 = new FuzzyRule(10, s1_d2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule10);

// FuzzyRule 11
FuzzyRuleAntecedent *s1_d2_s3 = new FuzzyRuleAntecedent();
  s1_d2_s3->joinWithAND(s1_d2, s3_antecedent);

FuzzyRule *fuzzyRule11 = new FuzzyRule(11, s1_d2_s3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule11);

// FuzzyRule 12
FuzzyRuleAntecedent *s1_d2_j3 = new FuzzyRuleAntecedent();
  s1_d2_j3->joinWithAND(s1_d2, j3_antecedent);

FuzzyRule *fuzzyRule12 = new FuzzyRule(12, s1_d2_j3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule12);

// FuzzyRule 13
FuzzyRuleAntecedent *s1_s2_d3 = new FuzzyRuleAntecedent();
  s1_s2_d3->joinWithAND(s1_s2, d3_antecedent);

FuzzyRule *fuzzyRule13 = new FuzzyRule(13, s1_s2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule13);

// FuzzyRule 14
FuzzyRuleAntecedent *s1_s2_s3 = new FuzzyRuleAntecedent();
  s1_s2_s3->joinWithAND(s1_s2, s3_antecedent);

FuzzyRule *fuzzyRule14 = new FuzzyRule(14, s1_s2_s3, thenmotor_s);
  fuzzy->addFuzzyRule(fuzzyRule14);

// FuzzyRule 15
FuzzyRuleAntecedent *s1_s2_j3 = new FuzzyRuleAntecedent();
  s1_s2_j3->joinWithAND(s1_s2, j3_antecedent);

FuzzyRule *fuzzyRule15 = new FuzzyRule(15, s1_s2_j3, thenmotor_s);
  fuzzy->addFuzzyRule(fuzzyRule15);

// FuzzyRule 16
FuzzyRuleAntecedent *s1_j2_d3 = new FuzzyRuleAntecedent();
  s1_j2_d3->joinWithAND(s1_j2, d3_antecedent);

FuzzyRule *fuzzyRule16 = new FuzzyRule(16, s1_j2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule16);

// FuzzyRule 17
FuzzyRuleAntecedent *s1_j2_s3 = new FuzzyRuleAntecedent();
  s1_j2_s3->joinWithAND(s1_j2, s3_antecedent);

FuzzyRule *fuzzyRule17 = new FuzzyRule(17, s1_j2_s3, thenmotor_s);
  fuzzy->addFuzzyRule(fuzzyRule17);

// FuzzyRule 18
FuzzyRuleAntecedent *s1_j2_j3 = new FuzzyRuleAntecedent();
  s1_j2_j3->joinWithAND(s1_j2, j3_antecedent);

FuzzyRule *fuzzyRule18 = new FuzzyRule(18, s1_j2_j3, thenmotor_s);
  fuzzy->addFuzzyRule(fuzzyRule18);

// FuzzyRule 19
FuzzyRuleAntecedent *j1_d2_d3 = new FuzzyRuleAntecedent();
  j1_d2_d3->joinWithAND(j1_d2, d3_antecedent);

FuzzyRule *fuzzyRule19 = new FuzzyRule(19, j1_d2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule19);

// FuzzyRule 20
FuzzyRuleAntecedent *j1_d2_s3 = new FuzzyRuleAntecedent();
  j1_d2_s3->joinWithAND(j1_d2, s3_antecedent);

FuzzyRule *fuzzyRule20 = new FuzzyRule(20, j1_d2_s3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule20);

// FuzzyRule 21
FuzzyRuleAntecedent *j1_d2_j3 = new FuzzyRuleAntecedent();
  j1_d2_j3->joinWithAND(j1_d2, j3_antecedent);

FuzzyRule *fuzzyRule21 = new FuzzyRule(21, j1_d2_j3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule21);

// FuzzyRule 22
FuzzyRuleAntecedent *j1_s2_d3 = new FuzzyRuleAntecedent();
  j1_s2_d3->joinWithAND(j1_s2, d3_antecedent);

FuzzyRule *fuzzyRule22 = new FuzzyRule(22, j1_s2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule22);

// FuzzyRule 23
FuzzyRuleAntecedent *j1_s2_s3 = new FuzzyRuleAntecedent();
  j1_s2_s3->joinWithAND(j1_s2, s3_antecedent);

FuzzyRule *fuzzyRule23 = new FuzzyRule(23, j1_s2_s3, thenmotor_s);
  fuzzy->addFuzzyRule(fuzzyRule23);

// FuzzyRule 24
FuzzyRuleAntecedent *j1_s2_j3 = new FuzzyRuleAntecedent();
  j1_s2_j3->joinWithAND(j1_s2, j3_antecedent);

FuzzyRule *fuzzyRule24 = new FuzzyRule(24, j1_s2_j3, thenmotor_s);
  fuzzy->addFuzzyRule(fuzzyRule24);

// FuzzyRule 25
FuzzyRuleAntecedent *j1_j2_d3 = new FuzzyRuleAntecedent();
  j1_j2_d3->joinWithAND(j1_j2, d3_antecedent);

FuzzyRule *fuzzyRule25 = new FuzzyRule(25, j1_j2_d3, thenmotor_l);
  fuzzy->addFuzzyRule(fuzzyRule25);

// FuzzyRule 26
FuzzyRuleAntecedent *j1_j2_s3 = new FuzzyRuleAntecedent();
  j1_j2_s3->joinWithAND(j1_j2, s3_antecedent);

FuzzyRule *fuzzyRule26 = new FuzzyRule(26, j1_j2_s3, thenmotor_s);
  fuzzy->addFuzzyRule(fuzzyRule26);

// FuzzyRule 27
FuzzyRuleAntecedent *j1_j2_j3 = new FuzzyRuleAntecedent();
  j1_j2_j3->joinWithAND(j1_j2, j3_antecedent);

FuzzyRule *fuzzyRule27 = new FuzzyRule(27, j1_j2_j3, thenmotor_c);
  fuzzy->addFuzzyRule(fuzzyRule27);
}

void loop() {
// put your main code here, to run repeatedly:
long waktu1, waktu2, waktu3, jarak1, jarak2, jarak3;

// Sensor Ultrasonik Kiri
digitalWrite(trig1, LOW);
delayMicroseconds(2);
digitalWrite(trig1, HIGH);
delayMicroseconds(10);
digitalWrite(trig1, LOW);
waktu1=pulseIn(echo1, HIGH);
jarak1=(waktu1 / 2) / 29.1;
Serial.print(jarak1);
Serial.println(" cm");
delay(500);

// Sensor Ultrasonik Tengah
digitalWrite(trig2, LOW);
delayMicroseconds(2);
digitalWrite(trig2, HIGH);
delayMicroseconds(10);
digitalWrite(trig2, LOW);
waktu2=pulseIn(echo2, HIGH);
jarak2=(waktu2 / 2) / 29.1;
Serial.print(jarak2);
Serial.println(" cm");
delay(500);

// Sensor Ultrasonik Kanan
digitalWrite(trig3, LOW);
delayMicroseconds(2);
digitalWrite(trig3, HIGH);
delayMicroseconds(10);
digitalWrite(trig3, LOW);
waktu3=pulseIn(echo3, HIGH);
jarak3=(waktu3 / 2) / 29.1;
Serial.print(jarak3);
Serial.println(" cm");
delay(500);

fuzzy->setInput(1, jarak1);
fuzzy->setInput(2, jarak2);
fuzzy->setInput(3, jarak3);
fuzzy->fuzzify();
 
// Running the Defuzzification
float output = fuzzy->defuzzify(1);
Serial.println(output);
if (jarak1 < 20  or jarak2 < 20 or jarak3 < 20 ){
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
output = 0;
}
else if ((jarak1 > 20 or jarak1 < 100) and (jarak2 > 20 or jarak2 < 100) and (jarak3 >20 or jarak3 < 100)){
analogWrite(ENA, 50);
analogWrite(ENB, 50);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if ((jarak1 > 100 or jarak1 < 200) and (jarak2 > 100 or jarak2 < 200) and (jarak3 >100 or jarak3 < 200)){
analogWrite(ENA, 120);
analogWrite(ENB, 120);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else if ((jarak1 > 200 or jarak1 < 300) and (jarak2 > 200 or jarak2 < 300) and (jarak3 >200 or jarak3 < 300)){
analogWrite(ENA, 200);
analogWrite(ENB, 200);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
else{
analogWrite(ENA, 255);
analogWrite(ENB, 255);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
}

"jarak1", "jarak2" and "jarak3" is output of ultrasonic sensors that im going to use it for "if" and "else if" conditions.
Im confused how to split that logic anymore. I think i have made a simplest logic but it doesn't really work. The DC motor is spinning, but it's not changing the condition as it is in the code.

Hello sandhika17

Welcome to the worldbest Arduino forum ever.

I assume that you have written the programme by yourself, then it is quite easy to find the error:

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code to see the values of variables, especially ones that control the motors, and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

Thanks for the welcome

I have tried make code like this

Serial.print(ENA);
Serial.print(ENB);
Serial.print(IN1);
Serial.print(IN2);
Serial.print(IN3);
Serial.print(IN4);

And it printing their pin location. It really make me confuse.

That are the results given as coded by you.

So, is there any solution for this one sir?

Yes.

Read this chapters to gain the knowledge.

https://www.learncpp.com/cpp-tutorial/a-strategy-for-debugging/