pleasedontcode

Bluetooth RC rev_01

Aug 27th, 2025
266
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 RC
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-08-27 12:18:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* rc car which uses bluetooth to control and need 2 */
  21.     /* L298N motor driver ,4x4 */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <BluetoothSerial.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /****** PIN CONFIGURATION *******/
  35. // Left side (Motor 1 and Motor 2 on L298N #1)
  36. const int ENA_LEFT = 5;      // PWM for Motor 1 (LEFT FRONT)
  37. const int IN1_LEFT = 18;     // Motor 1 input A
  38. const int IN2_LEFT = 19;     // Motor 1 input B
  39. const int IN3_LEFT = 21;     // Motor 2 input A
  40. const int IN4_LEFT = 22;     // Motor 2 input B
  41. const int ENB_LEFT = 23;     // PWM for Motor 2 (LEFT REAR)
  42.  
  43. // Right side (Motor 3 and Motor 4 on L298N #2)
  44. const int ENA_RIGHT = 25;    // PWM for Motor 3 (RIGHT FRONT)
  45. const int IN1_RIGHT = 26;    // Motor 3 input A
  46. const int IN2_RIGHT = 27;    // Motor 3 input B
  47. const int IN3_RIGHT = 32;    // Motor 4 input A
  48. const int IN4_RIGHT = 33;    // Motor 4 input B
  49. const int ENB_RIGHT = 14;    // PWM for Motor 4 (RIGHT REAR)
  50.  
  51. /****** PWM CONFIGURATION *******/
  52. const int PWM_FREQ = 5000;   // 5 kHz PWM frequency
  53. const int PWM_RES  = 8;       // 8-bit resolution (0-255)
  54. const int PWM_CH_LEFT_A  = 0; // PWM channel for LEFT M1
  55. const int PWM_CH_LEFT_B  = 1; // PWM channel for LEFT M2
  56. const int PWM_CH_RIGHT_A = 2; // PWM channel for RIGHT M3
  57. const int PWM_CH_RIGHT_B = 3; // PWM channel for RIGHT M4
  58.  
  59. BluetoothSerial SerialBT;
  60. int currentSpeed = 150; // default speed (0-255)
  61.  
  62. /****** PROTOTYPES FOR MOTOR CONTROL *******/
  63. void stopAllMotors();
  64. void driveLeftMotor(int motorIndex, int speed, int dir);   // motorIndex: 0 -> M1, 1 -> M2
  65. void driveRightMotor(int motorIndex, int speed, int dir);  // motorIndex: 0 -> M3, 1 -> M4
  66. void setLeftMotors(int dir, int speed);
  67. void setRightMotors(int dir, int speed);
  68. void driveAll(int dir, int speed);
  69.  
  70. /****** IMPLEMENTATION ******/
  71. void setup(void)
  72. {
  73.   // Initialize Bluetooth SPP
  74.   SerialBT.begin("ESP32_RCCar");
  75.  
  76.   // Initialize pins
  77.   pinMode(IN1_LEFT, OUTPUT);
  78.   pinMode(IN2_LEFT, OUTPUT);
  79.   pinMode(IN3_LEFT, OUTPUT);
  80.   pinMode(IN4_LEFT, OUTPUT);
  81.   pinMode(ENA_LEFT, OUTPUT);
  82.   pinMode(ENB_LEFT, OUTPUT);
  83.  
  84.   pinMode(IN1_RIGHT, OUTPUT);
  85.   pinMode(IN2_RIGHT, OUTPUT);
  86.   pinMode(IN3_RIGHT, OUTPUT);
  87.   pinMode(IN4_RIGHT, OUTPUT);
  88.   pinMode(ENA_RIGHT, OUTPUT);
  89.   pinMode(ENB_RIGHT, OUTPUT);
  90.  
  91.   // Setup PWM channels for ESC-like motor controllers
  92.   ledcSetup(PWM_CH_LEFT_A,  PWM_FREQ, PWM_RES);
  93.   ledcAttachPin(ENA_LEFT,   PWM_CH_LEFT_A);
  94.   ledcSetup(PWM_CH_LEFT_B,  PWM_FREQ, PWM_RES);
  95.   ledcAttachPin(ENB_LEFT,    PWM_CH_LEFT_B);
  96.  
  97.   ledcSetup(PWM_CH_RIGHT_A, PWM_FREQ, PWM_RES);
  98.   ledcAttachPin(ENA_RIGHT,  PWM_CH_RIGHT_A);
  99.   ledcSetup(PWM_CH_RIGHT_B, PWM_FREQ, PWM_RES);
  100.   ledcAttachPin(ENB_RIGHT,   PWM_CH_RIGHT_B);
  101.  
  102.   // Stop all motors at startup
  103.   stopAllMotors();
  104. }
  105.  
  106. void loop(void)
  107. {
  108.   if (SerialBT.available())
  109.   {
  110.     String cmd = SerialBT.readStringUntil('\n');
  111.     cmd.trim();
  112.     if (cmd.length() == 0) return;
  113.     char c = cmd.charAt(0);
  114.  
  115.     if (c == 'S' || c == 's') // set speed: S<0-255>
  116.     {
  117.       String num = cmd.substring(1);
  118.       int sp = num.toInt();
  119.       if (sp < 0) sp = 0;
  120.       if (sp > 255) sp = 255;
  121.       currentSpeed = sp;
  122.     }
  123.     else if (c == 'F' || c == 'f') // move forward
  124.     {
  125.       driveAll(1, currentSpeed);
  126.     }
  127.     else if (c == 'B' || c == 'b') // move backward
  128.     {
  129.       driveAll(-1, currentSpeed);
  130.     }
  131.     else if (c == 'L' || c == 'l') // rotate left
  132.     {
  133.       setLeftMotors(-1, currentSpeed);
  134.       setRightMotors(1, currentSpeed);
  135.     }
  136.     else if (c == 'R' || c == 'r') // rotate right
  137.     {
  138.       setLeftMotors(1, currentSpeed);
  139.       setRightMotors(-1, currentSpeed);
  140.     }
  141.     else if (c == 'X' || c == 'x') // stop
  142.     {
  143.       stopAllMotors();
  144.     }
  145.   }
  146.   delay(5);
  147. }
  148.  
  149. void stopAllMotors()
  150. {
  151.   setLeftMotors(0, 0);
  152.   setRightMotors(0, 0);
  153. }
  154.  
  155. void driveLeftMotor(int motorIndex, int speed, int dir)
  156. {
  157.   int channel = (motorIndex == 0) ? PWM_CH_LEFT_A : PWM_CH_LEFT_B;
  158.   int in1 = (motorIndex == 0) ? IN1_LEFT : IN3_LEFT;
  159.   int in2 = (motorIndex == 0) ? IN2_LEFT : IN4_LEFT;
  160.  
  161.   if (dir > 0) {
  162.     digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  163.   } else if (dir < 0) {
  164.     digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
  165.   } else {
  166.     digitalWrite(in1, LOW); digitalWrite(in2, LOW);
  167.   }
  168.   ledcWrite(channel, (speed < 0) ? 0 : speed);
  169. }
  170.  
  171. void driveRightMotor(int motorIndex, int speed, int dir)
  172. {
  173.   int channel = (motorIndex == 0) ? PWM_CH_RIGHT_A : PWM_CH_RIGHT_B;
  174.   int in1 = (motorIndex == 0) ? IN1_RIGHT : IN3_RIGHT;
  175.   int in2 = (motorIndex == 0) ? IN2_RIGHT : IN4_RIGHT;
  176.  
  177.   if (dir > 0) {
  178.     digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  179.   } else if (dir < 0) {
  180.     digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
  181.   } else {
  182.     digitalWrite(in1, LOW); digitalWrite(in2, LOW);
  183.   }
  184.   ledcWrite(channel, (speed < 0) ? 0 : speed);
  185. }
  186.  
  187. void setLeftMotors(int dir, int speed)
  188. {
  189.   driveLeftMotor(0, speed, dir);
  190.   driveLeftMotor(1, speed, dir);
  191. }
  192.  
  193. void setRightMotors(int dir, int speed)
  194. {
  195.   driveRightMotor(0, speed, dir);
  196.   driveRightMotor(1, speed, dir);
  197. }
  198.  
  199. void driveAll(int dir, int speed)
  200. {
  201.   setLeftMotors(dir, speed);
  202.   setRightMotors(dir, speed);
  203. }
  204.  
  205. /* END CODE */
  206.  
Advertisement
Add Comment
Please, Sign In to add comment