Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Bluetooth Robot
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-30 06:19:50
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Implement a system to control an LED connected to */
- /* GPIO pin 2 of the ESP32 DevKit V1, allowing it to */
- /* turn on and off via serial commands. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <BluetoothSerial.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void executeVoiceCommand(String command);
- void moveForward();
- void moveBackward();
- void turnLeft();
- void turnRight();
- void stopMotors();
- /* Define motor driver pins */
- const int IN1 = 18;
- const int IN2 = 19;
- const int IN3 = 21;
- const int IN4 = 22;
- // Define LED pin
- const int LED_PIN = 2;
- BluetoothSerial bluetooth;
- void setup(void)
- {
- // Initialize Bluetooth
- bluetooth.begin("ESP32_Voice_Control"); // Bluetooth device name
- // Initialize motor control pins
- pinMode(IN1, OUTPUT);
- pinMode(IN2, OUTPUT);
- pinMode(IN3, OUTPUT);
- pinMode(IN4, OUTPUT);
- // Initialize LED pin
- pinMode(LED_PIN, OUTPUT);
- // Stop motors at startup
- stopMotors();
- }
- void loop(void)
- {
- if (bluetooth.available()) {
- String command = bluetooth.readStringUntil('\n'); // Read command from Bluetooth
- command.trim(); // Remove whitespace or newline
- executeVoiceCommand(command);
- }
- }
- void executeVoiceCommand(String command) {
- if (command.equalsIgnoreCase("moveforward")) {
- moveForward();
- } else if (command.equalsIgnoreCase("move backward")) {
- moveBackward();
- } else if (command.equalsIgnoreCase("turnleft")) {
- turnLeft();
- } else if (command.equalsIgnoreCase("turnright")) {
- turnRight();
- } else if (command.equalsIgnoreCase("stop")) {
- stopMotors();
- } else if (command.equalsIgnoreCase("ledon")) {
- digitalWrite(LED_PIN, HIGH);
- bluetooth.println("LED ON");
- } else if (command.equalsIgnoreCase("ledoff")) {
- digitalWrite(LED_PIN, LOW);
- bluetooth.println("LED OFF");
- } else {
- bluetooth.println("UnknownCommand: " + command);
- }
- }
- void moveForward() {
- analogWrite(IN1, 135);
- analogWrite(IN2, 0);
- analogWrite(IN3, 135);
- analogWrite(IN4, 0);
- bluetooth.println("MovingForward");
- }
- void moveBackward() {
- analogWrite(IN1, 0);
- analogWrite(IN2, 135);
- analogWrite(IN3, 0);
- analogWrite(IN4, 135);
- bluetooth.println("MovingBackward");
- }
- void turnRight() {
- analogWrite(IN1, 135);
- analogWrite(IN2, 0);
- analogWrite(IN3, 0);
- analogWrite(IN4, 135);
- bluetooth.println("TurningRight");
- }
- void turnLeft() {
- analogWrite(IN1, 0);
- analogWrite(IN2, 135);
- analogWrite(IN3, 135);
- analogWrite(IN4, 0);
- bluetooth.println("TurningLeft");
- }
- void stopMotors() {
- analogWrite(IN1, 0);
- analogWrite(IN2, 0);
- analogWrite(IN3, 0);
- analogWrite(IN4, 0);
- bluetooth.println("MotorsStopped");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment