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;
}
}