pleasedontcode

Dynamic Control rev_01

Nov 9th, 2025
139
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: Dynamic Control
  13.     - Source Code NOT compiled for: XIAO ESP32S3
  14.     - Source Code created on: 2025-11-09 21:57:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* ovládá šest servomotorů na G1 G2 G5 G6 G7 G8 */
  21.     /* pomocí Bluetooth aplikace s tím že při startu se */
  22.     /* všechny serva srovnají na 90 stupňů */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. #include <BluetoothSerial.h>
  29. #include <Servo.h>
  30.  
  31. // Define pins for servos G1, G2, G5, G6, G7, G8
  32. const int servoPins[] = { G1, G2, G5, G6, G7, G8 };
  33. // Initialize servo objects
  34. Servo servos[6];
  35.  
  36. // Bluetooth serial object
  37. BluetoothSerial SerialBT;
  38.  
  39. // Create an array for target angles
  40. int targetAngles[6] = {90, 90, 90, 90, 90, 90};
  41.  
  42. // Buffer for incoming Bluetooth data
  43. String buffer = "";
  44.  
  45. // Function to attach servos to pins and initialize positions
  46. void initializeServos() {
  47.     for (int i = 0; i < 6; i++) {
  48.         servos[i].attach(servoPins[i]);
  49.         servos[i].write(90); // set to 90 degrees
  50.     }
  51. }
  52.  
  53. // Function to process received Bluetooth commands
  54. void processCommand(String command) {
  55.     // Expect commands in the format: Gx:angle, e.g., G1:45
  56.     int separatorIndex = command.indexOf(":");
  57.     if (separatorIndex == -1) return; // Invalid command
  58.  
  59.     String servoId = command.substring(0, separatorIndex);
  60.     String angleStr = command.substring(separatorIndex + 1);
  61.     int angle = angleStr.toInt();
  62.  
  63.     // Map servo ID to index
  64.     int index = -1;
  65.     if (servoId == "G1") index = 0;
  66.     else if (servoId == "G2") index = 1;
  67.     else if (servoId == "G5") index = 2;
  68.     else if (servoId == "G6") index = 3;
  69.     else if (servoId == "G7") index = 4;
  70.     else if (servoId == "G8") index = 5;
  71.  
  72.     if (index != -1 && angle >= 0 && angle <= 180) {
  73.         servos[index].write(angle);
  74.         targetAngles[index] = angle;
  75.     }
  76. }
  77.  
  78. void setup() {
  79.     initializeServos();
  80.     SerialBT.begin("ESP32ServoController"); // Bluetooth device name
  81.     // Initialize buffer
  82.     buffer = "";
  83. }
  84.  
  85. void loop() {
  86.     if (SerialBT.available()) {
  87.         char incomingChar = SerialBT.read();
  88.         if (incomingChar == '\n') {
  89.             processCommand(buffer);
  90.             buffer = "";
  91.         } else {
  92.             buffer += incomingChar;
  93.         }
  94.     }
  95. }
  96.  
  97. /* END CODE */
  98.  
Advertisement
Add Comment
Please, Sign In to add comment