Advertisement
pleasedontcode

**Servo Control** rev_01

Dec 7th, 2024
57
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 Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-07 09:36:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Create an arduino project that read info from an */
  21.     /* app on bluetooth and depending which of 3 options */
  22.     /* i choosen, starts one of the servo motor */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SoftwareSerial.h>
  29. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs();
  35. void readBluetoothData();
  36.  
  37. /***** DEFINITION OF PWM OUTPUT PINS *****/
  38. const uint8_t klapka1_Servomotor_PWMSignal_PIN_D3       = 3;
  39. const uint8_t klapka2_Servomotor_PWMSignal_PIN_D5       = 5;
  40. const uint8_t klapka3_Servomotor_PWMSignal_PIN_D6       = 6;
  41.  
  42. /***** DEFINITION OF Software Serial *****/
  43. const uint8_t Bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0      = A0;
  44. const uint8_t Bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1      = A1;
  45. SoftwareSerial Bluetooth_HC05_mySerial(Bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1, Bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0);
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. uint8_t klapka1_Servomotor_PWMSignal_PIN_D3_rawData     = 0;
  49. uint8_t klapka2_Servomotor_PWMSignal_PIN_D5_rawData     = 0;
  50. uint8_t klapka3_Servomotor_PWMSignal_PIN_D6_rawData     = 0;
  51.  
  52. /****** DEFINITION OF SERVO OBJECTS *****/
  53. Servo servo1; // Servo for klapka1
  54. Servo servo2; // Servo for klapka2
  55. Servo servo3; // Servo for klapka3
  56.  
  57. void setup(void)
  58. {
  59.     // Initialize servo motors
  60.     servo1.attach(klapka1_Servomotor_PWMSignal_PIN_D3);
  61.     servo2.attach(klapka2_Servomotor_PWMSignal_PIN_D5);
  62.     servo3.attach(klapka3_Servomotor_PWMSignal_PIN_D6);
  63.  
  64.     // Set pin modes
  65.     pinMode(klapka1_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  66.     pinMode(klapka2_Servomotor_PWMSignal_PIN_D5, OUTPUT);
  67.     pinMode(klapka3_Servomotor_PWMSignal_PIN_D6, OUTPUT);
  68.  
  69.     // Initialize Bluetooth communication
  70.     Bluetooth_HC05_mySerial.begin(9600);
  71. }
  72.  
  73. void loop(void)
  74. {
  75.     // Read data from Bluetooth
  76.     readBluetoothData();
  77.  
  78.     // Update outputs based on received data
  79.     updateOutputs();
  80. }
  81.  
  82. void readBluetoothData()
  83. {
  84.     if (Bluetooth_HC05_mySerial.available()) {
  85.         char command = Bluetooth_HC05_mySerial.read();
  86.         switch (command) {
  87.             case '1':
  88.                 klapka1_Servomotor_PWMSignal_PIN_D3_rawData = 255; // Full speed for servo 1
  89.                 klapka2_Servomotor_PWMSignal_PIN_D5_rawData = 0;   // Stop servo 2
  90.                 klapka3_Servomotor_PWMSignal_PIN_D6_rawData = 0;   // Stop servo 3
  91.                 break;
  92.             case '2':
  93.                 klapka1_Servomotor_PWMSignal_PIN_D3_rawData = 0;   // Stop servo 1
  94.                 klapka2_Servomotor_PWMSignal_PIN_D5_rawData = 255; // Full speed for servo 2
  95.                 klapka3_Servomotor_PWMSignal_PIN_D6_rawData = 0;   // Stop servo 3
  96.                 break;
  97.             case '3':
  98.                 klapka1_Servomotor_PWMSignal_PIN_D3_rawData = 0;   // Stop servo 1
  99.                 klapka2_Servomotor_PWMSignal_PIN_D5_rawData = 0;   // Stop servo 2
  100.                 klapka3_Servomotor_PWMSignal_PIN_D6_rawData = 255; // Full speed for servo 3
  101.                 break;
  102.             default:
  103.                 // Invalid command, stop all servos
  104.                 klapka1_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  105.                 klapka2_Servomotor_PWMSignal_PIN_D5_rawData = 0;
  106.                 klapka3_Servomotor_PWMSignal_PIN_D6_rawData = 0;
  107.                 break;
  108.         }
  109.     }
  110. }
  111.  
  112. void updateOutputs()
  113. {
  114.     // Write PWM signals to servos
  115.     analogWrite(klapka1_Servomotor_PWMSignal_PIN_D3, klapka1_Servomotor_PWMSignal_PIN_D3_rawData);
  116.     analogWrite(klapka2_Servomotor_PWMSignal_PIN_D5, klapka2_Servomotor_PWMSignal_PIN_D5_rawData);
  117.     analogWrite(klapka3_Servomotor_PWMSignal_PIN_D6, klapka3_Servomotor_PWMSignal_PIN_D6_rawData);
  118. }
  119.  
  120. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement