pleasedontcode

Bluetooth Robot rev_01

Jul 30th, 2025
522
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 Robot
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-30 06:19:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a system to control an LED connected to */
  21.     /* GPIO pin 2 of the ESP32 DevKit V1, allowing it to */
  22.     /* turn on and off via serial commands. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  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. void executeVoiceCommand(String command);
  34. void moveForward();
  35. void moveBackward();
  36. void turnLeft();
  37. void turnRight();
  38. void stopMotors();
  39.  
  40. /* Define motor driver pins */
  41. const int IN1 = 18;
  42. const int IN2 = 19;
  43. const int IN3 = 21;
  44. const int IN4 = 22;
  45.  
  46. // Define LED pin
  47. const int LED_PIN = 2;
  48.  
  49. BluetoothSerial bluetooth;
  50.  
  51. void setup(void)
  52. {
  53.   // Initialize Bluetooth
  54.   bluetooth.begin("ESP32_Voice_Control"); // Bluetooth device name
  55.  
  56.   // Initialize motor control pins
  57.   pinMode(IN1, OUTPUT);
  58.   pinMode(IN2, OUTPUT);
  59.   pinMode(IN3, OUTPUT);
  60.   pinMode(IN4, OUTPUT);
  61.  
  62.   // Initialize LED pin
  63.   pinMode(LED_PIN, OUTPUT);
  64.  
  65.   // Stop motors at startup
  66.   stopMotors();
  67. }
  68.  
  69. void loop(void)
  70. {
  71.   if (bluetooth.available()) {
  72.     String command = bluetooth.readStringUntil('\n'); // Read command from Bluetooth
  73.     command.trim(); // Remove whitespace or newline
  74.     executeVoiceCommand(command);
  75.   }
  76. }
  77.  
  78. void executeVoiceCommand(String command) {
  79.   if (command.equalsIgnoreCase("moveforward")) {
  80.     moveForward();
  81.   } else if (command.equalsIgnoreCase("move backward")) {
  82.     moveBackward();
  83.   } else if (command.equalsIgnoreCase("turnleft")) {
  84.     turnLeft();
  85.   } else if (command.equalsIgnoreCase("turnright")) {
  86.     turnRight();
  87.   } else if (command.equalsIgnoreCase("stop")) {
  88.     stopMotors();
  89.   } else if (command.equalsIgnoreCase("ledon")) {
  90.     digitalWrite(LED_PIN, HIGH);
  91.     bluetooth.println("LED ON");
  92.   } else if (command.equalsIgnoreCase("ledoff")) {
  93.     digitalWrite(LED_PIN, LOW);
  94.     bluetooth.println("LED OFF");
  95.   } else {
  96.     bluetooth.println("UnknownCommand: " + command);
  97.   }
  98. }
  99.  
  100. void moveForward() {
  101.   analogWrite(IN1, 135);
  102.   analogWrite(IN2, 0);
  103.   analogWrite(IN3, 135);
  104.   analogWrite(IN4, 0);
  105.   bluetooth.println("MovingForward");
  106. }
  107.  
  108. void moveBackward() {
  109.   analogWrite(IN1, 0);
  110.   analogWrite(IN2, 135);
  111.   analogWrite(IN3, 0);
  112.   analogWrite(IN4, 135);
  113.   bluetooth.println("MovingBackward");
  114. }
  115.  
  116. void turnRight() {
  117.   analogWrite(IN1, 135);
  118.   analogWrite(IN2, 0);
  119.   analogWrite(IN3, 0);
  120.   analogWrite(IN4, 135);
  121.   bluetooth.println("TurningRight");
  122. }
  123.  
  124. void turnLeft() {
  125.   analogWrite(IN1, 0);
  126.   analogWrite(IN2, 135);
  127.   analogWrite(IN3, 135);
  128.   analogWrite(IN4, 0);
  129.   bluetooth.println("TurningLeft");
  130. }
  131.  
  132. void stopMotors() {
  133.   analogWrite(IN1, 0);
  134.   analogWrite(IN2, 0);
  135.   analogWrite(IN3, 0);
  136.   analogWrite(IN4, 0);
  137.   bluetooth.println("MotorsStopped");
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment