Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Fan Control
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-09-30 06:40:07
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esp32-wroom will control a 12v fan, using a */
- /* debounce delay function, when the button is */
- /* pressed it will power the fan 50% speed, when the */
- /* button is pressed again the fan will speed at 100% */
- /* speed, when it is pressed again it will power off. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t button_PushButton_PIN_D4 = 4; // Pin for the push button
- const uint8_t fan_PIN = 5; // Pin for controlling the fan (PWM capable pin)
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- // Instance of the EasyButton class for the button connected to pin D4
- EasyButton button(button_PushButton_PIN_D4);
- /****** CALLBACK FUNCTION PROTOTYPES *****/
- // Callback function to be called when the button is pressed
- void onPressed(void);
- // Variable to keep track of the fan speed state
- int fanSpeedState = 0; // 0 = OFF, 1 = 50% speed, 2 = 100% speed
- /****** SETUP FUNCTION *****/
- void setup(void)
- {
- // Initialize Serial for debugging purposes
- Serial.begin(115200);
- Serial.println();
- Serial.println(">>> EasyButton pressed example <<<");
- // Initialize the button
- button.begin();
- // Add the callback function to be called when the button is pressed
- button.onPressed(onPressed);
- // Initialize the fan pin as output
- pinMode(fan_PIN, OUTPUT);
- }
- /****** LOOP FUNCTION *****/
- void loop(void)
- {
- // Continuously read the status of the button
- button.read();
- }
- // Callback function implementation
- void onPressed(void)
- {
- // Change the fan speed state based on the current state
- fanSpeedState++;
- if (fanSpeedState > 2) {
- fanSpeedState = 0; // Reset to OFF after 100% speed
- }
- // Control the fan speed based on the state
- switch (fanSpeedState) {
- case 0: // OFF
- Serial.println("Fan turned OFF");
- analogWrite(fan_PIN, 0); // Turn off the fan
- break;
- case 1: // 50% speed
- Serial.println("Fan at 50% speed");
- analogWrite(fan_PIN, 128); // Set fan to 50% speed (assuming 0-255 PWM range)
- break;
- case 2: // 100% speed
- Serial.println("Fan at 100% speed");
- analogWrite(fan_PIN, 255); // Set fan to 100% speed
- break;
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement