Advertisement
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: **Voice Control**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-12-06 13:47:24
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Give me a code for esp 32 wroom formaking Wi-Fi */
- /* Controlled, voice controlled and obstacle avoiding */
- /* car with esp32 wroom, one l293d 3 channel l293D */
- /* motor driver, one ultrasonic sensor which is */
- /* joined at Shaft of servo motor and four 4v dc */
- /* motors. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <WebServerSessionManager.h> // https://github.com/Zhu-jiatong/WebServerSessionManager
- #include <WiFi.h> // Include necessary library for WiFi
- #include <Servo.h> // Include necessary library for Servo
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Function prototypes for user-defined functions
- void moveForward();
- void moveBackward();
- void turnRight();
- void turnLeft();
- void scanSurrounding();
- void measureDistance();
- void stopCar();
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Define pins for motor driver
- #define motor1A 2
- #define motor1B 3
- #define motor2A 4
- #define motor2B 5
- #define motor3A 6
- #define motor3B 7
- #define motor4A 8
- #define motor4B 9
- // Define pins for ultrasonic sensor
- #define trigPin 10
- #define echoPin 11
- // Define pins for servo motor
- #define servoPin 12
- // Define variables for ultrasonic sensor
- long duration;
- int distance;
- // Define variables for servo motor
- Servo servo;
- // Define variables for WiFi
- const char* ssid = "Your WiFi Hotspot Name"; // Replace with your WiFi SSID
- const char* password = "Your WiFi Hotspot Password"; // Replace with your WiFi Password
- // Define variables for voice commands
- String command;
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(9600);
- // Initialize motor driver pins as output
- pinMode(motor1A, OUTPUT);
- pinMode(motor1B, OUTPUT);
- pinMode(motor2A, OUTPUT);
- pinMode(motor2B, OUTPUT);
- pinMode(motor3A, OUTPUT);
- pinMode(motor3B, OUTPUT);
- pinMode(motor4A, OUTPUT);
- pinMode(motor4B, OUTPUT);
- // Initialize ultrasonic sensor pins as input
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- // Initialize servo motor
- servo.attach(servoPin);
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi!");
- }
- void loop(void)
- {
- // Check for incoming serial data
- if (Serial.available() > 0) {
- // Read the incoming data
- command = Serial.readStringUntil('\n');
- // Remove any extra spaces
- command.trim();
- // Convert the command to lowercase
- command.toLowerCase();
- // Execute the corresponding function based on the command
- if (command == "move forward") {
- moveForward();
- } else if (command == "move backward") {
- moveBackward();
- } else if (command == "turn right") {
- turnRight();
- } else if (command == "turn left") {
- turnLeft();
- } else if (command == "scan surrounding") {
- scanSurrounding();
- } else if (command == "measure distance") {
- measureDistance();
- } else if (command == "stop") {
- stopCar();
- } else {
- Serial.println("Invalid command!");
- }
- }
- }
- // Function to move the car forward
- void moveForward() {
- // Set motor directions
- digitalWrite(motor1A, HIGH);
- digitalWrite(motor1B, LOW);
- digitalWrite(motor2A, HIGH);
- digitalWrite(motor2B, LOW);
- digitalWrite(motor3A, HIGH);
- digitalWrite(motor3B, LOW);
- digitalWrite(motor4A, HIGH);
- digitalWrite(motor4B, LOW);
- // Print message
- Serial.println("Moving forward!");
- }
- // Function to move the car backward
- void moveBackward() {
- // Set motor directions
- digitalWrite(motor1A, LOW);
- digitalWrite(motor1B, HIGH);
- digitalWrite(motor2A, LOW);
- digitalWrite(motor2B, HIGH);
- digitalWrite(motor3A, LOW);
- digitalWrite(motor3B, HIGH);
- digitalWrite(motor4A, LOW);
- digitalWrite(motor4B, HIGH);
- // Print message
- Serial.println("Moving backward!");
- }
- // Function to turn the car right
- void turnRight() {
- // Set motor directions
- digitalWrite(motor1A, HIGH);
- digitalWrite(motor1B, LOW);
- digitalWrite(motor2A, LOW);
- digitalWrite(motor2B, HIGH);
- digitalWrite(motor3A, HIGH);
- digitalWrite(motor3B, LOW);
- digitalWrite(motor4A, LOW);
- digitalWrite(motor4B, HIGH);
- // Print message
- Serial.println("Turning right!");
- }
- // Function to turn the car left
- void turnLeft() {
- // Set motor directions
- digitalWrite(motor1A, LOW);
- digitalWrite(motor1B, HIGH);
- digitalWrite(motor2A, HIGH);
- digitalWrite(motor2B, LOW);
- digitalWrite(motor3A, LOW);
- digitalWrite(motor3B, HIGH);
- digitalWrite(motor4A, HIGH);
- digitalWrite(motor4B, LOW);
- // Print message
- Serial.println("Turning left!");
- }
- // Function to scan the surrounding using the servo motor
- void scanSurrounding() {
- // Implementation for scanning surrounding
- for (int pos = 0; pos <= 180; pos += 1) { // Sweep from 0 to 180 degrees
- servo.write(pos); // Tell servo to go to position in variable 'pos'
- delay(15); // Wait for the servo to reach the position
- measureDistance(); // Measure distance at this position
- }
- for (int pos = 180; pos >= 0; pos -= 1) { // Sweep from 180 to 0 degrees
- servo.write(pos); // Tell servo to go to position in variable 'pos'
- delay(15); // Wait for the servo to reach the position
- measureDistance(); // Measure distance at this position
- }
- }
- // Function to measure distance using the ultrasonic sensor
- void measureDistance() {
- // Trigger the ultrasonic sensor
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Read the echo pin
- duration = pulseIn(echoPin, HIGH);
- // Calculate the distance
- distance = duration * 0.034 / 2; // Distance in cm
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.println(" cm");
- }
- // Function to stop the car
- void stopCar() {
- // Stop all motors
- digitalWrite(motor1A, LOW);
- digitalWrite(motor1B, LOW);
- digitalWrite(motor2A, LOW);
- digitalWrite(motor2B, LOW);
- digitalWrite(motor3A, LOW);
- digitalWrite(motor3B, LOW);
- digitalWrite(motor4A, LOW);
- digitalWrite(motor4B, LOW);
- // Print message
- Serial.println("Car stopped!");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement