#include <Fuzzy.h>
#include <Ticker.h>
const int ledChannel = 0;
const int resolution = 8;
const int frequency = 4000; // 4,000 Hz
float targetTemperature = 45; // อุณหภูมิที่ต้องการ
float hysteresis = 1;
const float BETA = 3950;
Ticker timer;
Fuzzy *fuzzy = new Fuzzy();
FuzzySet *cold = new FuzzySet(0, 10, 10, 20); // Define Fuzzy Sets for temperature
FuzzySet *warm = new FuzzySet(15, 25, 25, 35);
FuzzySet *hot = new FuzzySet(30, 40, 40, 50);
FuzzySet *lowError = new FuzzySet(-10, -5, -5, 0); // Define Fuzzy Sets for error
FuzzySet *mediumError = new FuzzySet(-7.5, 0, 0, 7.5);
FuzzySet *highError = new FuzzySet(0, 5, 5, 10);
FuzzySet *lowOutput = new FuzzySet(0, 50, 50, 100); // Define Fuzzy Sets for output
FuzzySet *mediumOutput = new FuzzySet(50, 100, 100, 150);
FuzzySet *highOutput = new FuzzySet(100, 150, 150, 255);
FuzzyInput *temperature = new FuzzyInput(1); // Create Fuzzy Inputs
FuzzyInput *error = new FuzzyInput(2);
FuzzyOutput *output = new FuzzyOutput(1); // Create Fuzzy Output
// Increase the gain of the fuzzy logic system
fuzzy->setGain(2.0f);
// Increase the hysteresis
fuzzy->setHysteresis(0.5f);
void toggleLED() {
int analogValue = analogRead(A0);
float temperatureValue = calculateTemperature(analogValue); // Calculate temperature
// Calculate error
float errorValue = targetTemperature - temperatureValue;
// Set input values
fuzzy->setInput(1, temperatureValue);
fuzzy->setInput(2, errorValue);
// Perform Fuzzy Logic
fuzzy->fuzzify();
// Get the output
float outputValue = fuzzy->defuzzify(1);
// Scale the output value to PWM range
float scaledOutput = constrain(map(outputValue, 0, 255, 0, 100) / 100.0f, 0, 1.0f);
// Set the output to control the LED
ledcWrite(ledChannel, scaledOutput);
// Print
Serial.print("Temperature: ");
Serial.print(temperatureValue, 2); // Print temperature with 2 decimal places
Serial.print(" °C, Error: ");
Serial.print(errorValue, 2); // Print error with 2 decimal places
Serial.print(" °C, PWM: ");
Serial.print(scaledOutput, 2); // Print scaled output with 2 decimal places
Serial.println();
}
float calculateTemperature(int analogValue) {
float temperature = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return temperature;
}
void setup() {
Serial.begin(9600);
analogReadResolution(10);
pinMode(A0, INPUT);
pinMode(5, OUTPUT);
ledcSetup(ledChannel, frequency, resolution);
ledcAttachPin(5, ledChannel);
timer.attach(0.1, toggleLED); // 0.1 วินาที
// Add Fuzzy Sets to Fuzzy Inputs
temperature->addFuzzySet(cold);
temperature->addFuzzySet(warm);
temperature->addFuzzySet(hot);
error->addFuzzySet(lowError);
error->addFuzzySet(mediumError);
error->addFuzzySet(highError);
// Add Fuzzy Sets to Fuzzy Output
output->addFuzzySet(lowOutput);
output->addFuzzySet(mediumOutput);
output->addFuzzySet(highOutput);
// Add Fuzzy Inputs and Output to the Fuzzy instance
fuzzy->addFuzzyInput(temperature);
fuzzy->addFuzzyInput(error);
fuzzy->addFuzzyOutput(output);
}
void loop() {
// Leave loop empty, the control is done in toggleLED()
}