Advertisement
pleasedontcode

"Fuzzy Control" rev_02

Mar 17th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Fuzzy Control"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-03-17 17:03:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Using fuzzy logic and the fuzzy library i have to */
  21.     /* write a specific code where i have an output led */
  22.     /* (pin 6) and input photorsistor (pin A0), and */
  23.     /* ultrasonic for the detection of motion (pin 2 for */
  24.     /* triger and 3 for echo) the fuzzy rule are Motion */
  25.     /* detecte */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
  30. #include <Fuzzy.h> //https://github.com/zerokol/eFLL
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t ultrasonic_sensor_HC_SR04_Echo_PIN_D3 = 3;
  39.  
  40. /***** DEFINITION OF ANALOG INPUT PINS *****/
  41. const uint8_t Photoresistor_PIN_A0 = A0;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t LED_LEDRGB_Blue_PIN_D6 = 6;
  45. const uint8_t ultrasonic_sensor_HC_SR04_Trigger_PIN_D2 = 2;
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. /***** used to store raw data *****/
  49. bool LED_LEDRGB_Blue_PIN_D6_rawData = 0;
  50. bool ultrasonic_sensor_HC_SR04_Trigger_PIN_D2_rawData = 0;
  51.  
  52. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  53. /***** used to store data after characteristic curve transformation *****/
  54. float LED_LEDRGB_Blue_PIN_D6_phyData = 0.0;
  55. float ultrasonic_sensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  58. Ultrasonic ultrasonic_sensor_HC_SR04(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, ultrasonic_sensor_HC_SR04_Echo_PIN_D3);
  59.  
  60. /****** FUZZY *****/
  61. Fuzzy *fuzzy = new Fuzzy();
  62.  
  63. // FuzzyInput
  64. FuzzySet *photoresistor_low = new FuzzySet(0, 0, 0, 100);
  65. FuzzySet *photoresistor_mid = new FuzzySet(0, 100, 100, 200);
  66. FuzzySet *photoresistor_high = new FuzzySet(100, 200, 1023, 1023);
  67.  
  68. // FuzzyOutput
  69. FuzzySet *led_off = new FuzzySet(0, 0, 0, 0);
  70. FuzzySet *led_on = new FuzzySet(1, 1, 1, 1);
  71.  
  72. void setup(void)
  73. {
  74.   // put your setup code here, to run once:
  75.   pinMode(ultrasonic_sensor_HC_SR04_Echo_PIN_D3, INPUT);
  76.   pinMode(Photoresistor_PIN_A0, INPUT);
  77.  
  78.   pinMode(LED_LEDRGB_Blue_PIN_D6, OUTPUT);
  79.   pinMode(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  80.  
  81.   // Add FuzzyInput and FuzzyOutput objects to the fuzzy logic system
  82.   FuzzyInput *photoresistor = new FuzzyInput(1);
  83.   photoresistor->addFuzzySet(photoresistor_low);
  84.   photoresistor->addFuzzySet(photoresistor_mid);
  85.   photoresistor->addFuzzySet(photoresistor_high);
  86.   fuzzy->addFuzzyInput(photoresistor);
  87.  
  88.   FuzzyOutput *ledState = new FuzzyOutput(1);
  89.   ledState->addFuzzySet(led_off);
  90.   ledState->addFuzzySet(led_on);
  91.   fuzzy->addFuzzyOutput(ledState);
  92.  
  93.   // Define fuzzy rules here
  94.   FuzzyRuleAntecedent *motionDetected = new FuzzyRuleAntecedent();
  95.   motionDetected->joinSingle(photoresistor_high);
  96.  
  97.   FuzzyRuleConsequent *outputLedOn = new FuzzyRuleConsequent();
  98.   outputLedOn->addOutput(led_on);
  99.  
  100.   FuzzyRule *fuzzyRule = new FuzzyRule(1, motionDetected, outputLedOn);
  101.   fuzzy->addFuzzyRule(fuzzyRule);
  102.  
  103.   // Set initial input values for the fuzzy logic system
  104.   fuzzy->setInput(1, analogRead(Photoresistor_PIN_A0));
  105.  
  106.   // Set initial output values for the physical outputs
  107.   LED_LEDRGB_Blue_PIN_D6_rawData = false;
  108.   ultrasonic_sensor_HC_SR04_Trigger_PIN_D2_rawData = false;
  109.  
  110.   // Start serial communication
  111.   Serial.begin(9600);
  112. }
  113.  
  114. void loop(void)
  115. {
  116.   // put your main code here, to run repeatedly:
  117.   // Perform ultrasonic sensor measurements
  118.   float distance = getUltrasonicDistance();
  119.  
  120.   // Set input values for the fuzzy logic system
  121.   fuzzy->setInput(1, analogRead(Photoresistor_PIN_A0));
  122.  
  123.   // Perform fuzzy logic calculations
  124.   fuzzy->fuzzify();
  125.  
  126.   // Obtain output value from fuzzy logic system
  127.   float ledState = fuzzy->defuzzify(1);
  128.  
  129.   // Update physical outputs based on fuzzy logic outputs
  130.   updateOutputs(ledState);
  131.   delay(1000);
  132. }
  133.  
  134. float getUltrasonicDistance()
  135. {
  136.   unsigned int distance = ultrasonic_sensor_HC_SR04.read(CM);
  137.   if (distance == 0) {
  138.     return 100; // If no object detected, set distance to maximum value
  139.   } else {
  140.     return distance;
  141.   }
  142. }
  143.  
  144. void updateOutputs(float ledState)
  145. {
  146.   if (ledState > 0.5) {
  147.     LED_LEDRGB_Blue_PIN_D6_rawData = true;
  148.   } else {
  149.     LED_LEDRGB_Blue_PIN_D6_rawData = false;
  150.   }
  151.  
  152.   // Update physical output states
  153.   digitalWrite(LED_LEDRGB_Blue_PIN_D6, LED_LEDRGB_Blue_PIN_D6_rawData);
  154.   digitalWrite(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, ultrasonic_sensor_HC_SR04_Trigger_PIN_D2_rawData);
  155.  
  156.   // Print the output state
  157.   Serial.print("LED State: ");
  158.   Serial.println(LED_LEDRGB_Blue_PIN_D6_rawData);
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement