Advertisement
pleasedontcode

Fan Control rev_01

Sep 30th, 2024
112
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: Fan Control
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-30 06:40:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32-wroom will control a 12v fan, using a */
  21.     /* debounce delay function, when the button is */
  22.     /* pressed it will power the fan 50% speed, when the */
  23.     /* button is pressed again the fan will speed at 100% */
  24.     /* speed, when it is pressed again it will power off. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t button_PushButton_PIN_D4 = 4; // Pin for the push button
  36. const uint8_t fan_PIN = 5; // Pin for controlling the fan (PWM capable pin)
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  39. // Instance of the EasyButton class for the button connected to pin D4
  40. EasyButton button(button_PushButton_PIN_D4);
  41.  
  42. /****** CALLBACK FUNCTION PROTOTYPES *****/
  43. // Callback function to be called when the button is pressed
  44. void onPressed(void);
  45.  
  46. // Variable to keep track of the fan speed state
  47. int fanSpeedState = 0; // 0 = OFF, 1 = 50% speed, 2 = 100% speed
  48.  
  49. /****** SETUP FUNCTION *****/
  50. void setup(void)
  51. {
  52.     // Initialize Serial for debugging purposes
  53.     Serial.begin(115200);
  54.     Serial.println();
  55.     Serial.println(">>> EasyButton pressed example <<<");
  56.  
  57.     // Initialize the button
  58.     button.begin();
  59.     // Add the callback function to be called when the button is pressed
  60.     button.onPressed(onPressed);
  61.    
  62.     // Initialize the fan pin as output
  63.     pinMode(fan_PIN, OUTPUT);
  64. }
  65.  
  66. /****** LOOP FUNCTION *****/
  67. void loop(void)
  68. {
  69.     // Continuously read the status of the button
  70.     button.read();
  71. }
  72.  
  73. // Callback function implementation
  74. void onPressed(void)
  75. {
  76.     // Change the fan speed state based on the current state
  77.     fanSpeedState++;
  78.     if (fanSpeedState > 2) {
  79.         fanSpeedState = 0; // Reset to OFF after 100% speed
  80.     }
  81.  
  82.     // Control the fan speed based on the state
  83.     switch (fanSpeedState) {
  84.         case 0: // OFF
  85.             Serial.println("Fan turned OFF");
  86.             analogWrite(fan_PIN, 0); // Turn off the fan
  87.             break;
  88.         case 1: // 50% speed
  89.             Serial.println("Fan at 50% speed");
  90.             analogWrite(fan_PIN, 128); // Set fan to 50% speed (assuming 0-255 PWM range)
  91.             break;
  92.         case 2: // 100% speed
  93.             Serial.println("Fan at 100% speed");
  94.             analogWrite(fan_PIN, 255); // Set fan to 100% speed
  95.             break;
  96.     }
  97. }
  98.  
  99. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement