Advertisement
pleasedontcode

"Servo Lock" rev_07

May 20th, 2024
1,087
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: "Servo Lock"
  13.     - Source Code compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-05-20 22:56:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The servo motor installed in the door will lock or */
  21.     /* unlock based on the correct PIN code received by */
  22.     /* the button. If the door is unlocked and the PIN is */
  23.     /* correct, it will lock. If the door is locked and */
  24.     /* the PIN is correct, it will unlock. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* The webserver provides a web page about lock or */
  27.     /* unlock state of the door. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  33. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  34. #include <WiFi.h>
  35. #include <WebServer.h>
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void updateOutputs(void);
  41. void onButtonPressed(void);
  42. void handleRoot(void);
  43. void handleNotFound(void);
  44.  
  45. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  46. const uint8_t codeButton_PushButton_PIN_D9 = 9;
  47.  
  48. /***** DEFINITION OF PWM OUTPUT PINS *****/
  49. const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. /***** used to store raw data *****/
  53. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. /***** used to store data after characteristic curve transformation *****/
  57. float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. EasyButton button(codeButton_PushButton_PIN_D9);  // Initialize EasyButton instance
  61. Servo myservo;  // Initialize Servo instance
  62. WebServer server(80);  // Initialize web server on port 80
  63.  
  64. /****** DEFINITION OF VARIABLES *****/
  65. bool isLocked = true;  // Variable to store the lock state
  66. const char* ssid = "your_SSID";  // Replace with your network SSID
  67. const char* password = "your_PASSWORD";  // Replace with your network password
  68.  
  69. void setup(void)
  70. {
  71.   // put your setup code here, to run once:
  72.   Serial.begin(115200);  // Initialize Serial for debugging purposes
  73.   Serial.println();
  74.   Serial.println(">>> EasyButton and Servo example <<<");
  75.  
  76.   pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
  77.   pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  78.  
  79.   button.begin();  // Initialize the button
  80.   button.onPressed(onButtonPressed);  // Add the callback function to be called when the button is pressed
  81.  
  82.   myservo.attach(servo_Servomotor_PWMSignal_PIN_D2);  // Attach the servo to pin D2
  83.   myservo.write(0);  // Initialize the servo to locked position
  84.  
  85.   // Connect to Wi-Fi
  86.   WiFi.begin(ssid, password);
  87.   while (WiFi.status() != WL_CONNECTED) {
  88.     delay(1000);
  89.     Serial.println("Connecting to WiFi...");
  90.   }
  91.   Serial.println("Connected to WiFi");
  92.  
  93.   // Start the web server
  94.   server.on("/", handleRoot);
  95.   server.onNotFound(handleNotFound);
  96.   server.begin();
  97.   Serial.println("Web server started");
  98. }
  99.  
  100. void loop(void)
  101. {
  102.   // put your main code here, to run repeatedly:
  103.   button.read();  // Continuously read the status of the button
  104.   updateOutputs();  // Refresh output data
  105.   server.handleClient();  // Handle web server client requests
  106. }
  107.  
  108. void updateOutputs()
  109. {
  110.   analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  111. }
  112.  
  113. void onButtonPressed()
  114. {
  115.   Serial.println("Button pressed");
  116.   // Toggle the lock state
  117.   isLocked = !isLocked;
  118.   int pos = isLocked ? 0 : 180;  // 0 degrees for locked, 180 degrees for unlocked
  119.   myservo.write(pos);  // Move the servo to the new position
  120.   Serial.println(isLocked ? "Door locked" : "Door unlocked");
  121. }
  122.  
  123. void handleRoot()
  124. {
  125.   String message = isLocked ? "Door is locked" : "Door is unlocked";
  126.   server.send(200, "text/plain", message);
  127. }
  128.  
  129. void handleNotFound()
  130. {
  131.   server.send(404, "text/plain", "404: Not found");
  132. }
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement