Skip to content

Instantly share code, notes, and snippets.

@mu-hun
Created December 7, 2025 07:14
Show Gist options
  • Select an option

  • Save mu-hun/0b4d65448098de4f36fea9abee3a5ba2 to your computer and use it in GitHub Desktop.

Select an option

Save mu-hun/0b4d65448098de4f36fea9abee3a5ba2 to your computer and use it in GitHub Desktop.
퍼지 로직 아두이노 시뮬레이션
#include <Fuzzy.h> // Please install eFLL (Embedded Fuzzy Logic Library) from https://github.com/alvesoaj/eFLL
// Fuzzy
Fuzzy *fuzzy = new Fuzzy();
// FuzzyInput Temperature
FuzzySet *VeryCold = new FuzzySet(0, 0, 10, 20);
FuzzySet *Cold = new FuzzySet(10, 20, 20, 30);
FuzzySet *Good = new FuzzySet(20, 30, 30, 40);
FuzzySet *Hot = new FuzzySet(30, 40, 50, 50);
// FuzzyInput Humidity
FuzzySet *VeryLow = new FuzzySet(0, 0, 20, 40);
FuzzySet *Low = new FuzzySet(20, 40, 40, 60);
FuzzySet *Normal = new FuzzySet(40, 60, 60, 80);
FuzzySet *High = new FuzzySet(60, 80, 80, 100);
FuzzySet *VeryHigh = new FuzzySet(80, 100, 120, 120);
// FuzzyOutput Execution Time
FuzzySet *VeryShort = new FuzzySet(0, 1, 1, 2);
FuzzySet *Short = new FuzzySet(1, 2, 2, 3);
FuzzySet *General = new FuzzySet(2, 3, 3, 4);
FuzzySet *Long = new FuzzySet(3, 4, 4, 5);
FuzzySet *VeryLong = new FuzzySet(4, 5, 5, 6);
void addFuzzyRule(int id, FuzzySet *temperatureSet, FuzzySet *humiditySet, FuzzySet *outputSet)
{
FuzzyRuleAntecedent *antecedent = new FuzzyRuleAntecedent();
antecedent->joinWithAND(temperatureSet, humiditySet);
FuzzyRuleConsequent *consequent = new FuzzyRuleConsequent();
consequent->addOutput(outputSet);
FuzzyRule *rule = new FuzzyRule(id, antecedent, consequent);
fuzzy->addFuzzyRule(rule);
}
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
// FuzzyInput for Temperature
FuzzyInput *temperature = new FuzzyInput(1);
temperature->addFuzzySet(VeryCold);
temperature->addFuzzySet(Cold);
temperature->addFuzzySet(Good);
temperature->addFuzzySet(Hot);
fuzzy->addFuzzyInput(temperature);
// FuzzyInput for Humidity
FuzzyInput *humidity = new FuzzyInput(2);
humidity->addFuzzySet(VeryLow);
humidity->addFuzzySet(Low);
humidity->addFuzzySet(Normal);
humidity->addFuzzySet(High);
humidity->addFuzzySet(VeryHigh);
fuzzy->addFuzzyInput(humidity);
// FuzzyOutput for Execution Time
FuzzyOutput *execution = new FuzzyOutput(1);
execution->addFuzzySet(VeryShort);
execution->addFuzzySet(Short);
execution->addFuzzySet(General);
execution->addFuzzySet(Long);
execution->addFuzzySet(VeryLong);
fuzzy->addFuzzyOutput(execution);
// Building Fuzzy Rules
addFuzzyRule(1, VeryCold, VeryLow, VeryShort);
addFuzzyRule(2, VeryCold, Low, Short);
addFuzzyRule(3, VeryCold, Normal, General);
addFuzzyRule(4, VeryCold, High, Long);
addFuzzyRule(5, VeryCold, VeryHigh, VeryLong);
addFuzzyRule(6, Cold, VeryLow, VeryShort);
addFuzzyRule(7, Cold, Low, Short);
addFuzzyRule(8, Cold, Normal, General);
addFuzzyRule(9, Cold, High, Long);
addFuzzyRule(10, Cold, VeryHigh, VeryLong);
addFuzzyRule(11, Good, VeryLow, Short);
addFuzzyRule(12, Good, Low, General);
addFuzzyRule(13, Good, Normal, Long);
addFuzzyRule(14, Good, High, VeryLong);
addFuzzyRule(15, Good, VeryHigh, VeryLong);
addFuzzyRule(16, Hot, VeryLow, Short);
addFuzzyRule(17, Hot, Low, General);
addFuzzyRule(18, Hot, Normal, Long);
addFuzzyRule(19, Hot, High, VeryLong);
addFuzzyRule(20, Hot, VeryHigh, VeryLong);
}
void loop()
{
// Generating random crisp values for Temperature and Humidity
float crispTemperature = random(0, 50); // Temperature between 0 and 50
float crispHumidity = random(0, 100); // Humidity between 0 and 100
// Setting the crisp values to Fuzzy Inputs
fuzzy->setInput(1, crispTemperature);
fuzzy->setInput(2, crispHumidity);
// Fuzzification
fuzzy->fuzzify();
// Defuzzification
float executionTime = fuzzy->defuzzify(1);
// Printing the results
Serial.print("Crisp Temperature: ");
Serial.print(crispTemperature);
Serial.print(" | Crisp Humidity: ");
Serial.print(crispHumidity);
Serial.print(" | Defuzzified Execution Time: ");
Serial.println(executionTime);
// Wait for 3 seconds before next iteration
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment