Advertisement
pleasedontcode

**Voice Control** rev_01

Dec 6th, 2024
69
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: **Voice Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-06 13:47:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Give me a code for esp 32 wroom formaking Wi-Fi */
  21.     /* Controlled, voice controlled and obstacle avoiding */
  22.     /* car with esp32 wroom, one l293d 3 channel l293D */
  23.     /* motor driver, one ultrasonic sensor which is */
  24.     /* joined at Shaft of servo motor and four 4v dc */
  25.     /* motors. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <WebServerSessionManager.h>  // https://github.com/Zhu-jiatong/WebServerSessionManager
  32. #include <WiFi.h>                     // Include necessary library for WiFi
  33. #include <Servo.h>                    // Include necessary library for Servo
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. // Function prototypes for user-defined functions
  40. void moveForward();
  41. void moveBackward();
  42. void turnRight();
  43. void turnLeft();
  44. void scanSurrounding();
  45. void measureDistance();
  46. void stopCar();
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49.  
  50. // Define pins for motor driver
  51. #define motor1A 2
  52. #define motor1B 3
  53. #define motor2A 4
  54. #define motor2B 5
  55. #define motor3A 6
  56. #define motor3B 7
  57. #define motor4A 8
  58. #define motor4B 9
  59.  
  60. // Define pins for ultrasonic sensor
  61. #define trigPin 10
  62. #define echoPin 11
  63.  
  64. // Define pins for servo motor
  65. #define servoPin 12
  66.  
  67. // Define variables for ultrasonic sensor
  68. long duration;
  69. int distance;
  70.  
  71. // Define variables for servo motor
  72. Servo servo;
  73.  
  74. // Define variables for WiFi
  75. const char* ssid = "Your WiFi Hotspot Name"; // Replace with your WiFi SSID
  76. const char* password = "Your WiFi Hotspot Password"; // Replace with your WiFi Password
  77.  
  78. // Define variables for voice commands
  79. String command;
  80.  
  81. void setup(void)
  82. {
  83.     // Initialize serial communication
  84.     Serial.begin(9600);
  85.    
  86.     // Initialize motor driver pins as output
  87.     pinMode(motor1A, OUTPUT);
  88.     pinMode(motor1B, OUTPUT);
  89.     pinMode(motor2A, OUTPUT);
  90.     pinMode(motor2B, OUTPUT);
  91.     pinMode(motor3A, OUTPUT);
  92.     pinMode(motor3B, OUTPUT);
  93.     pinMode(motor4A, OUTPUT);
  94.     pinMode(motor4B, OUTPUT);
  95.    
  96.     // Initialize ultrasonic sensor pins as input
  97.     pinMode(trigPin, OUTPUT);
  98.     pinMode(echoPin, INPUT);
  99.    
  100.     // Initialize servo motor
  101.     servo.attach(servoPin);
  102.    
  103.     // Connect to WiFi
  104.     WiFi.begin(ssid, password);
  105.     while (WiFi.status() != WL_CONNECTED) {
  106.         delay(500);
  107.         Serial.println("Connecting to WiFi...");
  108.     }
  109.     Serial.println("Connected to WiFi!");
  110. }
  111.  
  112. void loop(void)
  113. {
  114.     // Check for incoming serial data
  115.     if (Serial.available() > 0) {
  116.         // Read the incoming data
  117.         command = Serial.readStringUntil('\n');
  118.         // Remove any extra spaces
  119.         command.trim();
  120.         // Convert the command to lowercase
  121.         command.toLowerCase();
  122.         // Execute the corresponding function based on the command
  123.         if (command == "move forward") {
  124.             moveForward();
  125.         } else if (command == "move backward") {
  126.             moveBackward();
  127.         } else if (command == "turn right") {
  128.             turnRight();
  129.         } else if (command == "turn left") {
  130.             turnLeft();
  131.         } else if (command == "scan surrounding") {
  132.             scanSurrounding();
  133.         } else if (command == "measure distance") {
  134.             measureDistance();
  135.         } else if (command == "stop") {
  136.             stopCar();
  137.         } else {
  138.             Serial.println("Invalid command!");
  139.         }
  140.     }
  141. }
  142.  
  143. // Function to move the car forward
  144. void moveForward() {
  145.     // Set motor directions
  146.     digitalWrite(motor1A, HIGH);
  147.     digitalWrite(motor1B, LOW);
  148.     digitalWrite(motor2A, HIGH);
  149.     digitalWrite(motor2B, LOW);
  150.     digitalWrite(motor3A, HIGH);
  151.     digitalWrite(motor3B, LOW);
  152.     digitalWrite(motor4A, HIGH);
  153.     digitalWrite(motor4B, LOW);
  154.     // Print message
  155.     Serial.println("Moving forward!");
  156. }
  157.  
  158. // Function to move the car backward
  159. void moveBackward() {
  160.     // Set motor directions
  161.     digitalWrite(motor1A, LOW);
  162.     digitalWrite(motor1B, HIGH);
  163.     digitalWrite(motor2A, LOW);
  164.     digitalWrite(motor2B, HIGH);
  165.     digitalWrite(motor3A, LOW);
  166.     digitalWrite(motor3B, HIGH);
  167.     digitalWrite(motor4A, LOW);
  168.     digitalWrite(motor4B, HIGH);
  169.     // Print message
  170.     Serial.println("Moving backward!");
  171. }
  172.  
  173. // Function to turn the car right
  174. void turnRight() {
  175.     // Set motor directions
  176.     digitalWrite(motor1A, HIGH);
  177.     digitalWrite(motor1B, LOW);
  178.     digitalWrite(motor2A, LOW);
  179.     digitalWrite(motor2B, HIGH);
  180.     digitalWrite(motor3A, HIGH);
  181.     digitalWrite(motor3B, LOW);
  182.     digitalWrite(motor4A, LOW);
  183.     digitalWrite(motor4B, HIGH);
  184.     // Print message
  185.     Serial.println("Turning right!");
  186. }
  187.  
  188. // Function to turn the car left
  189. void turnLeft() {
  190.     // Set motor directions
  191.     digitalWrite(motor1A, LOW);
  192.     digitalWrite(motor1B, HIGH);
  193.     digitalWrite(motor2A, HIGH);
  194.     digitalWrite(motor2B, LOW);
  195.     digitalWrite(motor3A, LOW);
  196.     digitalWrite(motor3B, HIGH);
  197.     digitalWrite(motor4A, HIGH);
  198.     digitalWrite(motor4B, LOW);
  199.     // Print message
  200.     Serial.println("Turning left!");
  201. }
  202.  
  203. // Function to scan the surrounding using the servo motor
  204. void scanSurrounding() {
  205.     // Implementation for scanning surrounding
  206.     for (int pos = 0; pos <= 180; pos += 1) { // Sweep from 0 to 180 degrees
  207.         servo.write(pos);              // Tell servo to go to position in variable 'pos'
  208.         delay(15);                     // Wait for the servo to reach the position
  209.         measureDistance();             // Measure distance at this position
  210.     }
  211.     for (int pos = 180; pos >= 0; pos -= 1) { // Sweep from 180 to 0 degrees
  212.         servo.write(pos);              // Tell servo to go to position in variable 'pos'
  213.         delay(15);                     // Wait for the servo to reach the position
  214.         measureDistance();             // Measure distance at this position
  215.     }
  216. }
  217.  
  218. // Function to measure distance using the ultrasonic sensor
  219. void measureDistance() {
  220.     // Trigger the ultrasonic sensor
  221.     digitalWrite(trigPin, LOW);
  222.     delayMicroseconds(2);
  223.     digitalWrite(trigPin, HIGH);
  224.     delayMicroseconds(10);
  225.     digitalWrite(trigPin, LOW);
  226.    
  227.     // Read the echo pin
  228.     duration = pulseIn(echoPin, HIGH);
  229.    
  230.     // Calculate the distance
  231.     distance = duration * 0.034 / 2; // Distance in cm
  232.     Serial.print("Distance: ");
  233.     Serial.print(distance);
  234.     Serial.println(" cm");
  235. }
  236.  
  237. // Function to stop the car
  238. void stopCar() {
  239.     // Stop all motors
  240.     digitalWrite(motor1A, LOW);
  241.     digitalWrite(motor1B, LOW);
  242.     digitalWrite(motor2A, LOW);
  243.     digitalWrite(motor2B, LOW);
  244.     digitalWrite(motor3A, LOW);
  245.     digitalWrite(motor3B, LOW);
  246.     digitalWrite(motor4A, LOW);
  247.     digitalWrite(motor4B, LOW);
  248.     // Print message
  249.     Serial.println("Car stopped!");
  250. }
  251.  
  252. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement