Advertisement
pleasedontcode

"Voice Control" rev_02

Dec 17th, 2024
46
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-17 16:48:49
  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. void avoidObstacle();
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50.  
  51. // Define pins for motor driver
  52. #define motor1A 2
  53. #define motor1B 3
  54. #define motor2A 4
  55. #define motor2B 5
  56. #define motor3A 6
  57. #define motor3B 7
  58. #define motor4A 8
  59. #define motor4B 9
  60.  
  61. // Define pins for ultrasonic sensor
  62. #define trigPin 10
  63. #define echoPin 11
  64.  
  65. // Define pins for servo motor
  66. #define servoPin 12
  67.  
  68. // Define variables for ultrasonic sensor
  69. long duration;
  70. int distance;
  71.  
  72. // Define variables for servo motor
  73. Servo servo;
  74.  
  75. // Define variables for WiFi
  76. const char* ssid = "Your WiFi Hotspot Name"; // Replace with your WiFi SSID
  77. const char* password = "Your WiFi Hotspot Password"; // Replace with your WiFi Password
  78.  
  79. // Define variables for voice commands
  80. String command;
  81.  
  82. void setup(void)
  83. {
  84.     // Initialize serial communication
  85.     Serial.begin(9600);
  86.    
  87.     // Initialize motor driver pins as output
  88.     pinMode(motor1A, OUTPUT);
  89.     pinMode(motor1B, OUTPUT);
  90.     pinMode(motor2A, OUTPUT);
  91.     pinMode(motor2B, OUTPUT);
  92.     pinMode(motor3A, OUTPUT);
  93.     pinMode(motor3B, OUTPUT);
  94.     pinMode(motor4A, OUTPUT);
  95.     pinMode(motor4B, OUTPUT);
  96.    
  97.     // Initialize ultrasonic sensor pins as input
  98.     pinMode(trigPin, OUTPUT);
  99.     pinMode(echoPin, INPUT);
  100.    
  101.     // Initialize servo motor
  102.     servo.attach(servoPin);
  103.    
  104.     // Connect to WiFi
  105.     WiFi.begin(ssid, password);
  106.     while (WiFi.status() != WL_CONNECTED) {
  107.         delay(500);
  108.         Serial.println("Connecting to WiFi...");
  109.     }
  110.     Serial.println("Connected to WiFi!");
  111. }
  112.  
  113. void loop(void)
  114. {
  115.     // Check for incoming serial data
  116.     if (Serial.available() > 0) {
  117.         // Read the incoming data
  118.         command = Serial.readStringUntil('\n');
  119.         // Remove any extra spaces
  120.         command.trim();
  121.         // Convert the command to lowercase
  122.         command.toLowerCase();
  123.         // Execute the corresponding function based on the command
  124.         if (command == "move forward") {
  125.             moveForward();
  126.         } else if (command == "move backward") {
  127.             moveBackward();
  128.         } else if (command == "turn right") {
  129.             turnRight();
  130.         } else if (command == "turn left") {
  131.             turnLeft();
  132.         } else if (command == "scan surrounding") {
  133.             scanSurrounding();
  134.         } else if (command == "measure distance") {
  135.             measureDistance();
  136.         } else if (command == "stop") {
  137.             stopCar();
  138.         } else {
  139.             Serial.println("Invalid command!");
  140.         }
  141.     }
  142.  
  143.     // Call avoidObstacle function to check for obstacles
  144.     avoidObstacle();
  145. }
  146.  
  147. // Function to move the car forward
  148. void moveForward() {
  149.     // Set motor directions
  150.     digitalWrite(motor1A, HIGH);
  151.     digitalWrite(motor1B, LOW);
  152.     digitalWrite(motor2A, HIGH);
  153.     digitalWrite(motor2B, LOW);
  154.     digitalWrite(motor3A, HIGH);
  155.     digitalWrite(motor3B, LOW);
  156.     digitalWrite(motor4A, HIGH);
  157.     digitalWrite(motor4B, LOW);
  158.     // Print message
  159.     Serial.println("Moving forward!");
  160. }
  161.  
  162. // Function to move the car backward
  163. void moveBackward() {
  164.     // Set motor directions
  165.     digitalWrite(motor1A, LOW);
  166.     digitalWrite(motor1B, HIGH);
  167.     digitalWrite(motor2A, LOW);
  168.     digitalWrite(motor2B, HIGH);
  169.     digitalWrite(motor3A, LOW);
  170.     digitalWrite(motor3B, HIGH);
  171.     digitalWrite(motor4A, LOW);
  172.     digitalWrite(motor4B, HIGH);
  173.     // Print message
  174.     Serial.println("Moving backward!");
  175. }
  176.  
  177. // Function to turn the car right
  178. void turnRight() {
  179.     // Set motor directions
  180.     digitalWrite(motor1A, HIGH);
  181.     digitalWrite(motor1B, LOW);
  182.     digitalWrite(motor2A, LOW);
  183.     digitalWrite(motor2B, HIGH);
  184.     digitalWrite(motor3A, HIGH);
  185.     digitalWrite(motor3B, LOW);
  186.     digitalWrite(motor4A, LOW);
  187.     digitalWrite(motor4B, HIGH);
  188.     // Print message
  189.     Serial.println("Turning right!");
  190. }
  191.  
  192. // Function to turn the car left
  193. void turnLeft() {
  194.     // Set motor directions
  195.     digitalWrite(motor1A, LOW);
  196.     digitalWrite(motor1B, HIGH);
  197.     digitalWrite(motor2A, HIGH);
  198.     digitalWrite(motor2B, LOW);
  199.     digitalWrite(motor3A, LOW);
  200.     digitalWrite(motor3B, HIGH);
  201.     digitalWrite(motor4A, HIGH);
  202.     digitalWrite(motor4B, LOW);
  203.     // Print message
  204.     Serial.println("Turning left!");
  205. }
  206.  
  207. // Function to scan the surrounding using the servo motor
  208. void scanSurrounding() {
  209.     // Implementation for scanning surrounding
  210.     for (int pos = 0; pos <= 180; pos += 1) { // Sweep from 0 to 180 degrees
  211.         servo.write(pos);              // Tell servo to go to position in variable 'pos'
  212.         delay(15);                     // Wait for the servo to reach the position
  213.         measureDistance();             // Measure distance at this position
  214.     }
  215.     for (int pos = 180; pos >= 0; pos -= 1) { // Sweep from 180 to 0 degrees
  216.         servo.write(pos);              // Tell servo to go to position in variable 'pos'
  217.         delay(15);                     // Wait for the servo to reach the position
  218.         measureDistance();             // Measure distance at this position
  219.     }
  220. }
  221.  
  222. // Function to measure distance using the ultrasonic sensor
  223. void measureDistance() {
  224.     // Trigger the ultrasonic sensor
  225.     digitalWrite(trigPin, LOW);
  226.     delayMicroseconds(2);
  227.     digitalWrite(trigPin, HIGH);
  228.     delayMicroseconds(10);
  229.     digitalWrite(trigPin, LOW);
  230.    
  231.     // Read the echo pin
  232.     duration = pulseIn(echoPin, HIGH);
  233.    
  234.     // Calculate the distance
  235.     distance = duration * 0.034 / 2; // Distance in cm
  236.     Serial.print("Distance: ");
  237.     Serial.print(distance);
  238.     Serial.println(" cm");
  239. }
  240.  
  241. // Function to stop the car
  242. void stopCar() {
  243.     // Stop all motors
  244.     digitalWrite(motor1A, LOW);
  245.     digitalWrite(motor1B, LOW);
  246.     digitalWrite(motor2A, LOW);
  247.     digitalWrite(motor2B, LOW);
  248.     digitalWrite(motor3A, LOW);
  249.     digitalWrite(motor3B, LOW);
  250.     digitalWrite(motor4A, LOW);
  251.     digitalWrite(motor4B, LOW);
  252.     // Print message
  253.     Serial.println("Car stopped!");
  254. }
  255.  
  256. // Function to avoid obstacles
  257. void avoidObstacle() {
  258.     measureDistance(); // Measure distance to check for obstacles
  259.     if (distance < 20) { // If an obstacle is detected within 20 cm
  260.         Serial.println("Obstacle detected! Stopping the car.");
  261.         stopCar(); // Stop the car
  262.         delay(1000); // Wait for a second
  263.         // Optionally, you can add logic to turn or move backward
  264.         moveBackward(); // Move backward to avoid the obstacle
  265.         delay(1000); // Move backward for a second
  266.         stopCar(); // Stop again
  267.     }
  268. }
  269.  
  270. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement