Advertisement
pleasedontcode

Button Control rev_01

Apr 15th, 2024
66
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: Button Control
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-04-15 10:18:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Use esp now to turn on anoutput as long as the */
  21.     /* button is pressed add power saving */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <EasyButton.h>    // Library for handling buttons
  26. #include <esp_now.h>      // Library for ESP Now communication
  27. #include <WiFi.h>         // Library for WiFi functionality
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void onButtonPressed(); // Callback function for button press event
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Up_PushButton_PIN_D4 = 4;
  36. const uint8_t Down_PushButton_PIN_D13 = 13;
  37. const uint8_t Lift_PushButton_PIN_D14 = 14;
  38. const uint8_t Drop_PushButton_PIN_D16 = 16;
  39.  
  40. /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  41. EasyButton upButton(Up_PushButton_PIN_D4);
  42. EasyButton downButton(Down_PushButton_PIN_D13);
  43. EasyButton liftButton(Lift_PushButton_PIN_D14);
  44. EasyButton dropButton(Drop_PushButton_PIN_D16);
  45.  
  46. /****** DEFINITION OF ESP NOW VARIABLES *****/
  47. const uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Broadcast address for ESP Now
  48. uint8_t outputPinState = LOW; // Initial state of the output pin
  49. const uint8_t outputPin = 2; // Output pin to control
  50.  
  51. void setup(void)
  52. {
  53.     // put your setup code here, to run once:
  54.     Serial.begin(115200);
  55.  
  56.     // Initialize ESP Now
  57.     if (esp_now_init() != ESP_OK) {
  58.         Serial.println("ESP Now initialization failed");
  59.         while (true);
  60.     }
  61.  
  62.     // Register callback function for ESP Now data reception
  63.     esp_now_register_recv_cb([](const uint8_t* mac, const uint8_t* data, int len) {
  64.         // Handle received data if needed
  65.     });
  66.  
  67.     // Set ESP Now peer address to broadcast address
  68.     esp_now_peer_info_t peerInfo;
  69.     memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  70.     peerInfo.channel = 0;
  71.     peerInfo.encrypt = false;
  72.  
  73.     // Add ESP Now peer
  74.     if (esp_now_add_peer(&peerInfo) != ESP_OK) {
  75.         Serial.println("Failed to add ESP Now peer");
  76.         while (true);
  77.     }
  78.  
  79.     // Set output pin as output
  80.     pinMode(outputPin, OUTPUT);
  81.  
  82.     // Set callback function for button press event
  83.     upButton.onPressed(onButtonPressed);
  84.     downButton.onPressed(onButtonPressed);
  85.     liftButton.onPressed(onButtonPressed);
  86.     dropButton.onPressed(onButtonPressed);
  87. }
  88.  
  89. void loop(void)
  90. {
  91.     // put your main code here, to run repeatedly:
  92.     upButton.read();
  93.     downButton.read();
  94.     liftButton.read();
  95.     dropButton.read();
  96.  
  97.     // Send ESP Now message to control the output pin
  98.     esp_now_send(broadcastAddress, &outputPinState, sizeof(outputPinState));
  99.  
  100.     delay(100);
  101. }
  102.  
  103. void onButtonPressed()
  104. {
  105.     // Toggle the output pin state
  106.     outputPinState = !outputPinState;
  107.     digitalWrite(outputPin, outputPinState);
  108.  
  109.     // Print button pressed message
  110.     Serial.println("Button pressed");
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement