pleasedontcode

Bluetooth Servos rev_02

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