Documente Academic
Documente Profesional
Documente Cultură
ino
#include <FuzzyRule.h>
#include <FuzzyComposition.h>
#include <Fuzzy.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzyOutput.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzySet.h>
#include <FuzzyRuleAntecedent.h>
#define VOLTAGEINPUTVALUE 5.0 // I'm using 5 Volts
int temperatureSensorPin = 0; // Analog pin 0
int buzzerPin = 9; // Digital pin 9
float temperature; // Hold temperature value
float pertinence;
unsigned long timeNow;
unsigned long timeLast;
Fuzzy* fuzzy = new Fuzzy();// Instantiating an object library
// Creating the FuzzySets to compond FuzzyInput temperature
FuzzySet* cold = new FuzzySet(0, 0, 20, 40); // cold temperature
FuzzySet* fresh = new FuzzySet(30, 50, 50, 70); // fresh temperature
FuzzySet* hot = new FuzzySet(60, 80, 80, 80); // hot temperature
// Creating FuzzySets to compond FuzzyOutput danger
FuzzySet* freezingDanger = new FuzzySet(0, 20, 20, 40); // freezingDanger danger
FuzzySet* small = new FuzzySet(30, 50, 50, 70); // good small
FuzzySet* meltingDanger = new FuzzySet(60, 80, 80, 100); // meltingDanger danger
//FuzzyRule* fuzzyRule01;
//FuzzyRule* fuzzyRule02;
//FuzzyRule* fuzzyRule03;
void setup(){
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
FuzzyInput* temperature = new FuzzyInput(1);// Creating a FuzzyInput
temperature, (as param, its ID)
temperature->addFuzzySet(cold); // Add FuzzySet cold to FuzzyInput temperature
temperature->addFuzzySet(fresh); // Add FuzzySet fresh to FuzzyInput
temperature
temperature->addFuzzySet(hot); // Add FuzzySet hot to FuzzyInput temperature
fuzzy->addFuzzyInput(temperature); // Add FuzzyInput to Fuzzy object
FuzzyOutput* danger = new FuzzyOutput(1);// Creating FuzzyOutput danger, With
its ID in param
danger->addFuzzySet(freezingDanger); // Add FuzzySet freezingDanger to danger
danger->addFuzzySet(small); // Add FuzzySet small to danger
danger->addFuzzySet(meltingDanger); // Add FuzzySet meltingDanger to danger
fuzzy->addFuzzyOutput(danger); // Add FuzzyOutput to Fuzzy object
//Assembly the Fuzzy rules
// FuzzyRule "IF temperature = hot THEN danger = meltingDanger"
FuzzyRuleAntecedent* ifTemperatureHot = new FuzzyRuleAntecedent(); //
Instantiating an Antecedent to expression
ifTemperatureHot->joinSingle(hot); // Adding corresponding FuzzySet to
Antecedent object
FuzzyRuleConsequent* thenDangerMeltingDanger = new FuzzyRuleConsequent(); //
Instantiating a Consequent to expression
buzzerAlert(buzzerDelay);
}
}
}
float getCurrent(float input){
return ((input * VOLTAGEINPUTVALUE) / 1023.0);
}
float getTemperature(int pin){
float lecture = analogRead(pin);
float current = getCurrent(lecture);
return current * 100;
}
void buzzerAlert(int buzzerDelay){
digitalWrite(buzzerPin, HIGH);
delay(buzzerDelay);
digitalWrite(buzzerPin, LOW);
}